From 1a08b4755f818355ec755312d54bf9ed15829a5e Mon Sep 17 00:00:00 2001 From: robmckinnon Date: Mon, 18 Jul 2016 11:37:34 +0100 Subject: [PATCH 1/3] Add attribute accessor methods when provided attribute value is blank Create attribute accessor methods and set value when provided attribute value is blank, instead of not creating accessor methods. --- CHANGELOG | 2 ++ lib/morph.rb | 4 ++-- spec/lib/morph_spec.rb | 24 ++++++++++++++---------- spec/morph_spec_helper.rb | 22 ---------------------- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 401daaa..a8441d6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +v0.5.0. set accessor methods when attribute value is blank, instead of ignoring + v0.4.0. add from_json() and register_listener() methods v0.3.7. don't mix in private methods when Morph is included diff --git a/lib/morph.rb b/lib/morph.rb index ac32cb1..972f016 100644 --- a/lib/morph.rb +++ b/lib/morph.rb @@ -111,7 +111,7 @@ def self.morph_method_missing object, symbol, *args end def self.argument_provided? args - args.size > 0 && !args[0].nil? && !(args[0].is_a?(String) && args[0].strip.size == 0) + args.size > 0 end def self.convert_to_morph_class_name label @@ -132,7 +132,7 @@ def self.convert_to_morph_method_name label end module Morph - VERSION = '0.4.1' unless defined? Morph::VERSION + VERSION = '0.5.0' unless defined? Morph::VERSION class << self def classes diff --git a/spec/lib/morph_spec.rb b/spec/lib/morph_spec.rb index d182b0f..3856456 100644 --- a/spec/lib/morph_spec.rb +++ b/spec/lib/morph_spec.rb @@ -81,14 +81,15 @@ def self.extended_class describe "when writer method that didn't exist before is called with nil value" do after(:all) { unload_morph_class } - let(:attribute) { 'noise' } + let(:attributes) { ['noise'] } + let(:expected_morph_methods_count) { 2 } before(:all) do initialize_morph @morph.noise= nil end - it_should_behave_like "class without generated accessor methods added" + it_should_behave_like "class with generated accessor methods added" end describe "when different writer method called on two different morph classes" do @@ -188,14 +189,15 @@ def self.extended_class describe "when writer method that didn't exist before is called with blank space attribute value" do after(:all) { unload_morph_class } - let(:attribute) { 'noise' } + let(:attributes) { [:noise] } + let(:expected_morph_methods_count) { 2 } before(:all) do initialize_morph @morph.noise = ' ' end - it_should_behave_like "class without generated accessor methods added" + it_should_behave_like "class with generated accessor methods added" end describe 'when morph method used to set attribute value' do @@ -299,27 +301,29 @@ def self.extended_class describe 'when morph method used to set blank space attribute value' do after(:all) { unload_morph_class } - let(:attribute) { 'pizza' } + let(:attributes) { [:pizza] } + let(:expected_morph_methods_count) { 2 } before(:all) do initialize_morph @morph.morph('Pizza', ' ') end - it_should_behave_like "class without generated accessor methods added" + it_should_behave_like "class with generated accessor methods added" end describe 'when morph method used to set nil attribute value' do after(:all) { unload_morph_class } - let(:attribute) { 'pizza' } + let(:attributes) { [:pizza] } + let(:expected_morph_methods_count) { 2 } before(:all) do initialize_morph @morph.morph('Pizza', nil) end - it_should_behave_like "class without generated accessor methods added" + it_should_behave_like "class with generated accessor methods added" end @@ -695,14 +699,14 @@ def check_councillors councillors, class_name, nil_value='' expect(councillor.party).to eq 'labour' expect(councillor.councillors).to eq 'Councillor for Stretford Ward' expect(councillor.councils).to eq 'Trafford Council' - expect(councillor.respond_to?(:council_experience)).to be false + expect(councillor.respond_to?(:council_experience)).to be true councillor = councillors.last expect(councillor.name).to eq 'Ali Davidson' expect(councillor.party).to eq 'labour' expect(councillor.councillors).to eq nil_value expect(councillor.councils).to eq 'Basildon District Council' - expect(councillor.respond_to?(:council_experience)).to be false + expect(councillor.respond_to?(:council_experience)).to be true end describe 'tsv (tab separated value)' do diff --git a/spec/morph_spec_helper.rb b/spec/morph_spec_helper.rb index 535ddb8..e8d1075 100644 --- a/spec/morph_spec_helper.rb +++ b/spec/morph_spec_helper.rb @@ -102,25 +102,3 @@ def each_attribute end -shared_examples_for "class without generated accessor methods added" do - - it 'should not add reader method to class instance_methods list' do - expect(instance_methods).to_not include(attribute) - end - - it 'should not add writer method to class instance_methods list' do - expect(instance_methods).to_not include("#{attribute}=") - end - - it 'should not add reader method to class morph_methods list' do - expect(morph_methods).to_not include(attribute) - end - - it 'should not add writer method to class morph_methods list' do - expect(morph_methods).to_not include("#{attribute}=") - end - - it 'should have empty morph_methods list' do - expect(morph_methods.size).to eq 0 - end -end From e45bc45982ddf0b9136656cb404cb754510272bf Mon Sep 17 00:00:00 2001 From: robmckinnon Date: Mon, 18 Jul 2016 12:13:01 +0100 Subject: [PATCH 2/3] Spec attribute value set in shared example helper Refactor specs to test attribute value is set correctly in a shared example helper. Remove separate redundant examples that test attribute value is set. Use shared context for setting a single attribute value. --- spec/lib/morph_spec.rb | 96 +++++++-------------------------------- spec/morph_spec_helper.rb | 26 +++++++++-- 2 files changed, 38 insertions(+), 84 deletions(-) diff --git a/spec/lib/morph_spec.rb b/spec/lib/morph_spec.rb index 3856456..57f23a0 100644 --- a/spec/lib/morph_spec.rb +++ b/spec/lib/morph_spec.rb @@ -29,27 +29,21 @@ def morphed_class let(:extended_morph) { extended_morphed_class.new } describe "when writer method that didn't exist before is called with non-nil value" do - before(:all) { initialize_morph } - after(:all) { unload_morph_class } - - let(:quack) { 'quack' } - let(:attribute) { 'noise' } - let(:expected_morph_methods_count) { 2 } + include_context 'single attribute value set', 'noise', 'quack' context do before do - remove_morph_methods - @morph.noise = quack + @morph.noise = value end it_should_behave_like "class with generated accessor methods added" it 'should return assigned value when reader method called' do - expect(@morph.noise).to eq quack + expect(@morph.noise).to eq value end it 'should return hash of attributes when morph_attributes called' do - expect(@morph.morph_attributes).to eq({attribute.to_sym => quack}) + expect(@morph.morph_attributes).to eq({attribute.to_sym => value}) end it 'should generate rails model generator script line, with given model name' do @@ -79,15 +73,9 @@ def self.extended_class end describe "when writer method that didn't exist before is called with nil value" do - after(:all) { unload_morph_class } + include_context 'single attribute value set', 'noise', nil - let(:attributes) { ['noise'] } - let(:expected_morph_methods_count) { 2 } - - before(:all) do - initialize_morph - @morph.noise= nil - end + before { @morph.noise = value } it_should_behave_like "class with generated accessor methods added" end @@ -187,37 +175,19 @@ def self.extended_class end describe "when writer method that didn't exist before is called with blank space attribute value" do - after(:all) { unload_morph_class } - - let(:attributes) { [:noise] } - let(:expected_morph_methods_count) { 2 } + include_context 'single attribute value set', 'noise', ' ' - before(:all) do - initialize_morph - @morph.noise = ' ' - end + before { @morph.noise = value } it_should_behave_like "class with generated accessor methods added" end describe 'when morph method used to set attribute value' do - before(:all) { initialize_morph } - after(:all) { unload_morph_class } - - let(:attribute) { 'reading' } - let(:value) { '20 Mar 2008' } - let(:expected_morph_methods_count) { 2 } + include_context 'single attribute value set', 'reading', '20 Mar 2008' - before do - remove_morph_methods - @morph.morph('Reading', value) - end + before { @morph.morph('Reading', value) } it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - expect(@morph.reading).to eq value - end end describe 'when morph method used to set an attribute value hash' do @@ -226,6 +196,7 @@ def self.extended_class let(:expected_morph_methods_count) { 6 } let(:attributes) { [:drink,:sugars,:milk] } + let(:value) { 'tea' } before do remove_morph_methods @@ -268,10 +239,6 @@ def self.extended_class end it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - @morph.send(@attribute.to_sym) == @age - end end describe "when morph method used to set japanese and latin unicode attribute name with a value" do before :all do initialize_morph; end @@ -291,37 +258,21 @@ def self.extended_class end it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - @morph.send(@attribute.to_sym) == @age - end end =end describe 'when morph method used to set blank space attribute value' do - after(:all) { unload_morph_class } + include_context 'single attribute value set', 'pizza', ' ' - let(:attributes) { [:pizza] } - let(:expected_morph_methods_count) { 2 } - - before(:all) do - initialize_morph - @morph.morph('Pizza', ' ') - end + before(:each) { @morph.morph('Pizza', value) } it_should_behave_like "class with generated accessor methods added" end describe 'when morph method used to set nil attribute value' do - after(:all) { unload_morph_class } + include_context 'single attribute value set', 'pizza', nil - let(:attributes) { [:pizza] } - let(:expected_morph_methods_count) { 2 } - - before(:all) do - initialize_morph - @morph.morph('Pizza', nil) - end + before { @morph.morph('Pizza', value) } it_should_behave_like "class with generated accessor methods added" end @@ -344,24 +295,11 @@ def self.extended_class end describe "when writer method called matches a class reader method" do + include_context 'single attribute value set', 'name', 'Morph' - before(:all) { initialize_morph } - after(:all) { unload_morph_class } - - let(:attribute) { 'name' } - let(:value) { 'Morph' } - let(:expected_morph_methods_count) { 2 } - - before do - remove_morph_methods - @morph.name = value - end + before { @morph.name = value } it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - expect(@morph.name).to eq value - end end diff --git a/spec/morph_spec_helper.rb b/spec/morph_spec_helper.rb index e8d1075..22d8ead 100644 --- a/spec/morph_spec_helper.rb +++ b/spec/morph_spec_helper.rb @@ -64,7 +64,13 @@ def each_attribute klass = morphed_class unless klass end - it 'should add reader method to class instance_methods list' do + it 'sets first attribute value correctly' do + attribute = nil + each_attribute {|a| attribute = a unless attribute} + expect(@morph.send(attribute)).to eq value + end + + it 'adds reader method to class instance_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(instance_methods(klass)).to include(a.to_s.to_sym) } else @@ -72,7 +78,7 @@ def each_attribute end end - it 'should add writer method to class instance_methods list' do + it 'adds writer method to class instance_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(instance_methods(klass)).to include("#{a}=".to_sym) } else @@ -80,7 +86,7 @@ def each_attribute end end - it 'should add reader method to class morph_methods list' do + it 'adds reader method to class morph_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(morph_methods(klass)).to include(a.to_s.to_sym) } else @@ -88,7 +94,7 @@ def each_attribute end end - it 'should add writer method to class morph_methods list' do + it 'adds writer method to class morph_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(morph_methods(klass)).to include("#{a}=".to_sym) } else @@ -96,9 +102,19 @@ def each_attribute end end - it 'should only have generated accessor methods in morph_methods list' do + it 'only has generated accessor methods in morph_methods list' do expect(morph_methods(klass).size).to eq expected_morph_methods_count end end +shared_context 'single attribute value set' do |field, value| + before(:all) { initialize_morph } + after(:all) { unload_morph_class } + + let(:attribute) { field } + let(:expected_morph_methods_count) { 2 } + let(:value) { value } + + before { remove_morph_methods } +end From 107eb7fab3646727987a609185784a3631f77d5c Mon Sep 17 00:00:00 2001 From: robmckinnon Date: Mon, 18 Jul 2016 12:48:47 +0100 Subject: [PATCH 3/3] Remove testing with 2.0.0, 1.9.3, 1.8.7-head, jruby-head --- .travis.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5b68cc2..1c0b0a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,7 @@ language: ruby rvm: - - 2.3.0 - - 2.2.4 - - 2.0.0-p598 - - 1.9.3 - - 1.8.7-head - - jruby-head + - 2.3.1 + - 2.2.5 - ruby-head - rbx-3.13 bundler_args: --without test