Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/versionci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ jobs:
matrix:
ruby: ["3.2.0", "3.2.7", "3.3.0", "3.3.7"]
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rspec
- name: Run Rubocop
run: bundle exec rubocop --force-exclusion --autocorrect --fail-level autocorrect
- name: Run Tests
run: bundle exec rspec
50 changes: 50 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
AllCops:
TargetRubyVersion: 3.3.0
NewCops: enable
Exclude:
- 'bin/**/*'
- 'vendor/**/*'
- 'tmp/**/*'
- 'coverage/**/*'

Gemspec/RequiredRubyVersion:
Enabled: false

Layout/LineLength:
Max: 120
Exclude:
- 'decanter.gemspec'

Lint/UnderscorePrefixedVariableName:
Exclude:
- 'lib/decanter/core.rb'
Comment on lines +18 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to mess with functionality in this PR, so I opted to exclude certain rubocop configs on core.rb that would require more invasive changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the general premise of not doing those refactorings within this PR, but I might prefer to actually address the Lint/UnderscorePrefixedVariableName errors within this PR, since it just involves renaming variables without changing any logic, rather than disabling the Rubocop rule. I'm good with handling the other violations separately in this new issue.


Metrics/AbcSize:
Max: 20
Exclude:
- 'lib/decanter/core.rb'

Metrics/BlockLength:
Exclude:
- 'spec/**/*_spec.rb'
- 'decanter.gemspec'

Metrics/CyclomaticComplexity:
Exclude:
- 'lib/decanter/core.rb'
Comment on lines +22 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to disable the specific methods/blocks that are violating the rules. Then, if that code is modified before #158 is resolved, any new code will be subject to the linting rules.


Metrics/MethodLength:
Max: 25

Metrics/ModuleLength:
Max: 205

Naming/PredicateName:
Exclude:
- 'lib/decanter/core.rb'

Style/Documentation:
Enabled: false

Style/StringLiterals:
EnforcedStyle: single_quotes
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in decanter.gemspec
gemspec

group :development, :test do
gem 'bundler', '~> 2.4.22'
gem 'dotenv'
gem 'rake', '~> 12.0'
gem 'rspec-rails', '~> 3.9'
gem 'rubocop', '~> 1.59'
gem 'simplecov', '~> 0.15.1'
end
31 changes: 30 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
decanter (5.0.0)
decanter (5.1.0)
actionpack (>= 7.1.3.2)
activesupport
rails (>= 7.1.3.2)
Expand Down Expand Up @@ -84,6 +84,7 @@ GEM
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
ast (2.4.3)
base64 (0.2.0)
bigdecimal (3.1.7)
builder (3.2.4)
Expand All @@ -105,6 +106,8 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -131,6 +134,11 @@ GEM
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.7.4)
ast (~> 2.4.1)
racc
prism (1.4.0)
psych (5.1.2)
stringio
racc (1.7.3)
Expand Down Expand Up @@ -171,9 +179,11 @@ GEM
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (12.3.3)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.5.5)
io-console (~> 0.5)
rspec-core (3.9.3)
Expand All @@ -193,6 +203,21 @@ GEM
rspec-mocks (~> 3.9.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.4)
rubocop (1.75.2)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.44.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.44.0)
parser (>= 3.3.7.2)
prism (~> 1.4)
ruby-progressbar (1.13.0)
simplecov (0.15.1)
docile (~> 1.1.0)
json (>= 1.8, < 3)
Expand All @@ -203,6 +228,9 @@ GEM
timeout (0.4.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
webrick (1.8.1)
websocket-driver (0.7.7)
base64
Expand All @@ -221,6 +249,7 @@ DEPENDENCIES
dotenv
rake (~> 12.0)
rspec-rails (~> 3.9)
rubocop (~> 1.59)
simplecov (~> 0.15.1)

BUNDLED WITH
Expand Down
11 changes: 10 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new

task default: %i[spec rubocop]
14 changes: 7 additions & 7 deletions decanter.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'decanter/version'
Expand All @@ -20,19 +22,17 @@ Gem::Specification.new do |spec|

spec.metadata['allowed_push_host'] = 'https://rubygems.org'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'rails', '>= 7.1.3.2'
spec.add_dependency 'actionpack', '>= 7.1.3.2'
spec.add_dependency 'activesupport'
spec.add_dependency 'rails', '>= 7.1.3.2'
spec.add_dependency 'rails-html-sanitizer', '>= 1.0.4'

spec.add_development_dependency 'bundler', '~> 2.4.22'
spec.add_development_dependency 'dotenv'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec-rails', '~> 3.9'
spec.add_development_dependency 'simplecov', '~> 0.15.1'
spec.metadata['rubygems_mfa_required'] = 'true'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoledow not sure if MFA is already required for publishing this gem but rubocop is requiring it. Seems like a good idea.

Ref: https://guides.rubygems.org/mfa-requirement-opt-in/

end
32 changes: 16 additions & 16 deletions lib/decanter.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# frozen_string_literal: true

require 'active_support/all'

module Decanter

class << self

def decanter_for(klass_or_sym)
decanter_name =
case klass_or_sym
when Class
klass_or_sym.name
"#{klass_or_sym.name}Decanter"
when Symbol
klass_or_sym.to_s.singularize.camelize
"#{klass_or_sym.to_s.singularize.camelize}Decanter"
Comment on lines +11 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - why did you have to add "Decanter" to the end of this string?

else
raise ArgumentError.new("cannot lookup decanter for #{klass_or_sym} with class #{klass_or_sym.class}")
end + 'Decanter'
raise ArgumentError,
"cannot lookup decanter for #{klass_or_sym} with class #{klass_or_sym.class}"
end
begin
decanter_name.constantize
rescue
raise NameError.new("uninitialized constant #{decanter_name}")
rescue StandardError
raise NameError, "uninitialized constant #{decanter_name}"
end
end

Expand All @@ -29,22 +30,21 @@ def decanter_from(klass_or_string)
when String
begin
klass_or_string.constantize
rescue
raise NameError.new("uninitialized constant #{klass_or_string}")
rescue StandardError
raise NameError, "uninitialized constant #{klass_or_string}"
end
else
raise ArgumentError.new("cannot find decanter from #{klass_or_string} with class #{klass_or_string.class}")
raise ArgumentError,
"cannot find decanter from #{klass_or_string} with class #{klass_or_string.class}"
end

unless constant.ancestors.include? Decanter::Base
raise ArgumentError.new("#{constant.name} is not a decanter")
end
raise ArgumentError, "#{constant.name} is not a decanter" unless constant.ancestors.include? Decanter::Base

constant
end

def configuration
@config ||= Decanter::Configuration.new
@configuration ||= Decanter::Configuration.new
end

def config
Expand All @@ -61,4 +61,4 @@ def config
require 'decanter/extensions'
require 'decanter/exceptions'
require 'decanter/parser'
require 'decanter/railtie' if defined?(::Rails)
require 'decanter/railtie' if defined?(Rails)
2 changes: 2 additions & 0 deletions lib/decanter/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'decanter/core'
require 'decanter/collection_detection'

Expand Down
9 changes: 8 additions & 1 deletion lib/decanter/collection_detection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Decanter
module CollectionDetection
def self.included(base)
Expand All @@ -15,7 +17,12 @@ def decant(args, **options)

# leveraging the approach used in the [fast JSON API gem](https://github.com/Netflix/fast_jsonapi#collection-serialization)
def collection?(args, collection_option = nil)
raise(ArgumentError, "#{name}: Unknown collection option value: #{collection_option}") unless [true, false, nil].include? collection_option
unless [
true, false, nil
].include? collection_option
raise(ArgumentError,
"#{name}: Unknown collection option value: #{collection_option}")
end

return collection_option unless collection_option.nil?

Expand Down
3 changes: 2 additions & 1 deletion lib/decanter/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Decanter
class Configuration

attr_accessor :strict, :log_unhandled_keys

def initialize
Expand Down
Loading