Skip to content

Commit

Permalink
Merge pull request #109 from eiskrenkov/main
Browse files Browse the repository at this point in the history
Generate _key methods for attributes
  • Loading branch information
lucascaton authored Apr 21, 2024
2 parents d435453 + 46a0b69 commit bac5aa5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
10 changes: 9 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 @@ -92,6 +98,7 @@ GEM
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 @@ -100,6 +107,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord
appraisal
bundler
enumerate_it!
Expand Down
1 change: 1 addition & 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 Down
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 bac5aa5

Please sign in to comment.