Skip to content

Commit

Permalink
[ISSUE-68] - Add linter, remove support ruby < 3.0 and rails < 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jlurena committed Jul 9, 2024
1 parent 9693b7a commit 56e5e97
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 222 deletions.
88 changes: 13 additions & 75 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,37 @@
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Test
name: Code Coverage

on: [push]
on: [push, workflow_dispatch, pull_request]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-20.04

strategy:
matrix:
ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3']
rails-version: ['4.2', '5.0', '5.1', '6.0', '6.1', '7.0', '7.1']
exclude:
# activesupport (~> 6.0.0) was resolved to 6.0.6.1, which depends on ruby (>= 2.5.0)
# activesupport (~> 6.1.0) was resolved to 6.1.7.2, which depends on ruby (>= 2.5.0)
- ruby-version: '2.3'
rails-version: '6.0'
- ruby-version: '2.3'
rails-version: '6.1'
- ruby-version: '2.4'
rails-version: '6.0'
- ruby-version: '2.4'
rails-version: '6.1'
# activesupport (~> 7.0.0) was resolved to 7.0.4.2, which depends on Ruby (>= 2.7.0)
- ruby-version: '2.3'
rails-version: '7.0'
- ruby-version: '2.4'
rails-version: '7.0'
- ruby-version: '2.5'
rails-version: '7.0'
- ruby-version: '2.6'
rails-version: '7.0'
# incompatbility with BigDecimal.new
- ruby-version: '2.7'
rails-version: '4.2'
- ruby-version: '3.0'
rails-version: '4.2'
- ruby-version: '3.1'
rails-version: '4.2'
- ruby-version: '3.2'
rails-version: '4.2'
# ArgumentError: expected attributes to be able to convert to Hash, got "#<Thing:0x000055d208b2e258>"
# probably keyword argument delegation different in Ruby 3
# https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
- ruby-version: '3.0'
rails-version: '5.0'
- ruby-version: '3.0'
rails-version: '5.1'
- ruby-version: '3.1'
rails-version: '5.0'
- ruby-version: '3.1'
rails-version: '5.1'
- ruby-version: '3.2'
rails-version: '5.0'
- ruby-version: '3.2'
rails-version: '5.1'
# rails (~> 7.1.0) was resolved to 7.1.3.2, which depends on Ruby (>= 2.7.0)
- ruby-version: '2.3'
rails-version: '7.1'
- ruby-version: '2.4'
rails-version: '7.1'
- ruby-version: '2.5'
rails-version: '7.1'
- ruby-version: '2.6'
rails-version: '7.1'
# Because rails >= 4.0.0.beta1, < 5.0.5.rc1 depends on bundler >= 1.3.0, < 2.0
# and the current Bundler version (2.5.9) does not satisfy bundler >= 1.3.0, < 2.0,
# rails >= 4.0.0.beta1, < 5.0.5.rc1 cannot be used.
# So, because Gemfile depends on rails ~> 4.2.0,
# version solving has failed.
- ruby-version: '3.3'
rails-version: '4.2'

rails-version: ["6.1", "7.1"]
steps:
- uses: actions/checkout@v4
- name: Set up Ruby ${{ matrix.ruby-version }}
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Install bundler 1.x.x
if: matrix.rails-version == '4.2'
run: gem uninstall -aIx bundler && gem install bundler -v 1.17.3
ruby-version: 3.0
- name: Install dependencies
run: bundle install
env:
TEST_RAILS_VERSION: ${{ matrix.rails-version }}
DEBUG: true
- name: Run tests
run: bundle exec rake
lint:
runs-on: ubuntu-20.04
permissions:
checks: write
contents: write
steps:
- name: Standard Ruby
uses: standardrb/standard-ruby-action@v1
3 changes: 3 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fix: true
parallel: true
ruby_version: 3.0
22 changes: 11 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ gemspec
def eval_gemfile(path)
gemfile_local = File.expand_path(path, __FILE__)
if File.readable?(gemfile_local)
puts "Loading #{gemfile_local}..." if ENV['DEBUG']
puts "Loading #{gemfile_local}..." if ENV["DEBUG"]
instance_eval(File.read(gemfile_local))
end
end

puts "\e[93mUsing TEST_RAILS_VERSION #{ENV['TEST_RAILS_VERSION']}\e[0m" if ENV['DEBUG']
case ENV['TEST_RAILS_VERSION']
puts "\e[93mUsing TEST_RAILS_VERSION #{ENV["TEST_RAILS_VERSION"]}\e[0m" if ENV["DEBUG"]
case ENV["TEST_RAILS_VERSION"]
when "4.2"
eval_gemfile('../gemfiles/4.2.gemfile')
eval_gemfile("../gemfiles/4.2.gemfile")
when "5.0"
eval_gemfile('../gemfiles/5.0.gemfile')
eval_gemfile("../gemfiles/5.0.gemfile")
when "5.1"
eval_gemfile('../gemfiles/5.1.gemfile')
eval_gemfile("../gemfiles/5.1.gemfile")
when "6.0"
eval_gemfile('../gemfiles/6.0.gemfile')
eval_gemfile("../gemfiles/6.0.gemfile")
when "6.1"
eval_gemfile('../gemfiles/6.1.gemfile')
eval_gemfile("../gemfiles/6.1.gemfile")
when "7.0"
eval_gemfile('../gemfiles/7.0.gemfile')
eval_gemfile("../gemfiles/7.0.gemfile")
when "7.1"
eval_gemfile('../gemfiles/7.1.gemfile')
eval_gemfile("../gemfiles/7.1.gemfile")
else
puts "\e[93mNo TEST_RAILS_VERSION present, letting dependency manager decide what's best.\e[0m" if ENV['DEBUG']
puts "\e[93mNo TEST_RAILS_VERSION present, letting dependency manager decide what's best.\e[0m" if ENV["DEBUG"]
end
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
require "rspec/core/rake_task"

desc "Run all examples"
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
26 changes: 12 additions & 14 deletions cached_resource.gemspec
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "cached_resource/version"

Gem::Specification.new do |s|
s.name = "cached_resource"
s.version = CachedResource::VERSION
s.authors = "Morgan Brown"
s.email = "[email protected]"
s.homepage = "https://github.com/mhgbrown/cached_resource"
s.summary = %q{Caching for ActiveResource}
s.description = %q{Enables request-based caching for ActiveResource}
s.licenses = ["MIT"]
s.name = "cached_resource"
s.version = CachedResource::VERSION
s.authors = "Morgan Brown"
s.email = "[email protected]"
s.homepage = "https://github.com/mhgbrown/cached_resource"
s.summary = "Caching for ActiveResource"
s.description = "Enables request-based caching for ActiveResource"
s.licenses = ["MIT"]

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ["lib"]

s.required_ruby_version = ">= 1.9.0"
s.required_ruby_version = ">= 3.0"

s.add_runtime_dependency "activeresource", ">= 4.0"
s.add_runtime_dependency "activesupport", ">= 4.0"
Expand All @@ -29,5 +27,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec"
s.add_development_dependency "simplecov", "~> 0.22.0"
s.add_development_dependency "timecop", "~> 0.9.10"

s.add_development_dependency "standard", "~> 1.39", ">= 1.39.1"
end
20 changes: 10 additions & 10 deletions lib/cached_resource.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'ostruct'
require "ostruct"

require 'nilio'
require 'active_support/cache'
require 'active_support/concern'
require 'active_support/logger'
require 'cached_resource/cached_resource'
require 'cached_resource/configuration'
require 'cached_resource/caching'
require 'cached_resource/version'
require 'active_resource'
require "nilio"
require "active_support/cache"
require "active_support/concern"
require "active_support/logger"
require "cached_resource/cached_resource"
require "cached_resource/configuration"
require "cached_resource/caching"
require "cached_resource/version"
require "active_resource"

module CachedResource
# nada
Expand Down
6 changes: 3 additions & 3 deletions lib/cached_resource/cached_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class << self
attr_accessor :cached_resource

# Initialize cached resource or retrieve the current cached resource configuration.
def cached_resource(options={})
def cached_resource(options = {})
defined?(@cached_resource) && @cached_resource || setup_cached_resource!(options)
end

Expand All @@ -20,7 +20,7 @@ def setup_cached_resource!(options)
@cached_resource = CachedResource::Configuration.new(options)
if @cached_resource.concurrent_write
begin
require 'concurrent/promise'
require "concurrent/promise"
rescue LoadError
@cached_resource.logger.error(
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
Expand All @@ -40,7 +40,7 @@ module ClassMethods
# that wants an independent configuration will need to execute:
# self.cached_resource = CachedResource::Configuration.new(options={})
def inherited(child)
child.cached_resource = self.cached_resource if defined?(@cached_resource)
child.cached_resource = cached_resource if defined?(@cached_resource)
super
end
end
Expand Down
41 changes: 21 additions & 20 deletions lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def find_with_cache(*arguments)
end

# Clear the cache.
def clear_cache(options=nil)
def clear_cache(options = nil)
if cached_resource.concurrent_write
Concurrent::Promise.execute { cache_clear(options) }
else
Expand Down Expand Up @@ -79,7 +79,9 @@ def update_collection_cache(updates, *arguments)
collection = cache_read(cache_key(cached_resource.collection_arguments))

if collection && !updates.empty?
index = collection.inject({}) { |hash, object| hash[object.send(primary_key)] = object; hash }
index = collection.each_with_object({}) { |object, hash|
hash[object.send(primary_key)] = object
}
updates.each { |object| index[object.send(primary_key)] = object }
cache_write(cache_key(cached_resource.collection_arguments), index.values, *arguments)
end
Expand All @@ -100,13 +102,12 @@ def is_collection?(*arguments)
# Determine if the given arguments represent
# any collection of objects
def is_any_collection?(*arguments)
cached_resource.collection_arguments.all?{ |arg| arguments.include?(arg) } || arguments.include?(:all)
cached_resource.collection_arguments.all? { |arg| arguments.include?(arg) } || arguments.include?(:all)
end

# Read a entry from the cache for the given key.
def cache_read(key)
object = cached_resource.cache.read(key).try do |json_cache|

json = ActiveSupport::JSON.decode(json_cache)

unless json.nil?
Expand All @@ -116,7 +117,7 @@ def cache_read(key)
next restored unless respond_to?(:collection_parser)
collection_parser.new(restored).tap do |parser|
parser.resource_class = self
parser.original_params = json['original_params'].deep_symbolize_keys
parser.original_params = json["original_params"].deep_symbolize_keys
end
else
full_dup(cache)
Expand All @@ -141,15 +142,15 @@ def _cache_write(key, object, *arguments)
params = options[:params]
prefix_options, query_options = split_options(params)

result = cached_resource.cache.write(key, object_to_json(object, prefix_options, query_options), :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
result = cached_resource.cache.write(key, object_to_json(object, prefix_options, query_options), race_condition_ttl: cached_resource.race_condition_ttl, expires_in: cached_resource.generate_ttl)
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
result
end

# Clear the cache.
def cache_clear(options=nil)
def cache_clear(options = nil)
# Memcache doesn't support delete_matched, which can also be computationally expensive
if cached_resource.cache.class.to_s == 'ActiveSupport::Cache::MemCacheStore' || options.try(:fetch,:all)
if (Object.const_defined?(:Dalli) && cached_resource.cache.class.instance_of?("ActiveSupport::Cache::MemCacheStore")) || options.try(:fetch, :all)
cached_resource.cache.clear.tap do |result|
cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} CLEAR ALL")
end
Expand All @@ -171,13 +172,13 @@ def cache_key_delete_pattern

# Generate the request cache key.
def cache_key(*arguments)
"#{name_key}/#{arguments.join('/')}".downcase.delete(' ')
"#{name_key}/#{arguments.join("/")}".downcase.delete(" ")
end

def name_key
@name_key ||= begin
prefix = cached_resource.cache_key_prefix.nil? ? "" : "#{cached_resource.cache_key_prefix}/"
prefix + name.parameterize.gsub("-", "/")
prefix + name.parameterize.tr("-", "/")
end
end

Expand All @@ -190,31 +191,31 @@ def full_dup(record)
end

def json_to_object(json)
resource = json['resource']
resource = json["resource"]
if resource.is_a? Array
resource.map do |attrs|
self.new(attrs["object"], attrs["persistence"]).tap do |resource|
resource.prefix_options = json['prefix_options']
new(attrs["object"], attrs["persistence"]).tap do |resource|
resource.prefix_options = json["prefix_options"]
end
end
else
self.new(resource["object"], resource["persistence"]).tap do |resource|
resource.prefix_options = json['prefix_options']
new(resource["object"], resource["persistence"]).tap do |resource|
resource.prefix_options = json["prefix_options"]
end
end
end

def object_to_json(object, prefix_options, query_options)
if object.is_a? Enumerable
{
:resource => object.map { |o| { :object => o, :persistence => o.persisted? } },
:prefix_options => prefix_options,
:original_params => query_options
resource: object.map { |o| {object: o, persistence: o.persisted?} },
prefix_options: prefix_options,
original_params: query_options
}.to_json
else
{
:resource => { :object => object, :persistence => object.persisted? },
:prefix_options => prefix_options
resource: {object: object, persistence: object.persisted?},
prefix_options: prefix_options
}.to_json
end
end
Expand Down
Loading

0 comments on commit 56e5e97

Please sign in to comment.