From dc5b4d2d02b282f689f78945e20f4f70c53d1fb3 Mon Sep 17 00:00:00 2001 From: Egor Iskrenkov Date: Tue, 16 Apr 2024 19:28:01 +0200 Subject: [PATCH] Generate _key methods for attributes --- Gemfile.lock | 14 +++++++++++++- enumerate_it.gemspec | 2 ++ lib/enumerate_it/class_methods.rb | 11 +++++++++++ spec/enumerate_it_spec.rb | 9 +++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index b2afc03..235ef17 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -98,6 +108,7 @@ PLATFORMS ruby DEPENDENCIES + activerecord appraisal bundler enumerate_it! @@ -107,6 +118,7 @@ DEPENDENCIES rubocop rubocop-rake rubocop-rspec + sqlite3 wwtd BUNDLED WITH diff --git a/enumerate_it.gemspec b/enumerate_it.gemspec index 8f4cbaf..a625f8b 100644 --- a/enumerate_it.gemspec +++ b/enumerate_it.gemspec @@ -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' @@ -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 diff --git a/lib/enumerate_it/class_methods.rb b/lib/enumerate_it/class_methods.rb index b4e2460..dbb92f2 100644 --- a/lib/enumerate_it/class_methods.rb +++ b/lib/enumerate_it/class_methods.rb @@ -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) @@ -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] diff --git a/spec/enumerate_it_spec.rb b/spec/enumerate_it_spec.rb index 5c06356..e5b4146 100644 --- a/spec/enumerate_it_spec.rb +++ b/spec/enumerate_it_spec.rb @@ -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