Skip to content

Commit

Permalink
Generate _key methods for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
eiskrenkov committed Apr 16, 2024
1 parent cf3ae63 commit dc5b4d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (7.1.1)
activemodel (7.1.3.2)
activesupport (= 7.1.3.2)
activerecord (7.1.3.2)
activemodel (= 7.1.3.2)
activesupport (= 7.1.3.2)
timeout (>= 0.4.0)
activesupport (7.1.3.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
Expand Down Expand Up @@ -35,6 +41,7 @@ GEM
json (2.6.3)
language_server-protocol (3.17.0.3)
method_source (1.0.0)
mini_portile2 (2.8.6)
minitest (5.20.0)
mutex_m (0.1.2)
parallel (1.23.0)
Expand Down Expand Up @@ -88,7 +95,10 @@ GEM
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sqlite3 (1.7.3)
mini_portile2 (~> 2.8.0)
thor (1.2.2)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand All @@ -98,6 +108,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord
appraisal
bundler
enumerate_it!
Expand All @@ -107,6 +118,7 @@ DEPENDENCIES
rubocop
rubocop-rake
rubocop-rspec
sqlite3
wwtd

BUNDLED WITH
Expand Down
2 changes: 2 additions & 0 deletions enumerate_it.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |gem|

gem.add_dependency 'activesupport', '>= 5.0.7.2'

gem.add_development_dependency 'activerecord'
gem.add_development_dependency 'appraisal'
gem.add_development_dependency 'bundler'
gem.add_development_dependency 'pry'
Expand All @@ -30,5 +31,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rubocop'
gem.add_development_dependency 'rubocop-rake'
gem.add_development_dependency 'rubocop-rspec'
gem.add_development_dependency 'sqlite3'
gem.add_development_dependency 'wwtd'
end
11 changes: 11 additions & 0 deletions lib/enumerate_it/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def has_enumeration_for(attribute, options = {})

define_enumeration_class(attribute, options)
create_enumeration_humanize_method(options[:with], attribute)
create_enumeration_key_method(options[:with], attribute)
store_enumeration(options[:with], attribute)

handle_options(attribute, options)
Expand Down Expand Up @@ -38,6 +39,16 @@ def create_enumeration_humanize_method(klass, attribute_name)
end
end

def create_enumeration_key_method(klass, attribute_name)
class_eval do
define_method "#{attribute_name}_key" do
value = public_send(attribute_name)

value ? klass.key_for(value) : nil
end
end
end

def create_helper_methods(klass, attribute_name, helpers)
prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]

Expand Down
9 changes: 9 additions & 0 deletions spec/enumerate_it_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def initialize(foobar)
expect(target.foobar_humanize).to be_nil
end

it 'creates the key method' do
expect(target.foobar_key).to eq(:value_2)
end

it 'if the attribute is blank, the key method returns nil' do
target.foobar = nil
expect(target.foobar_key).to be_nil
end

it 'defaults to not creating helper methods' do
expect(target).not_to respond_to(:value_1?)
end
Expand Down

0 comments on commit dc5b4d2

Please sign in to comment.