From 7933271abe6f13685516cccf0f953f2e08d77dff Mon Sep 17 00:00:00 2001 From: Yury Kotov Date: Sun, 29 May 2016 12:53:27 +0300 Subject: [PATCH] Implement traits --- Gemfile | 3 +-- Gemfile.lock | 28 +++++++++++----------------- README.md | 23 +++++++++++++++++++++++ lib/cranky/factory.rb | 11 +++++++++++ spec/cranky_spec.rb | 9 +++++++++ spec/spec_helper.rb | 10 +++++++--- 6 files changed, 62 insertions(+), 22 deletions(-) diff --git a/Gemfile b/Gemfile index 52bd02c..0b10259 100644 --- a/Gemfile +++ b/Gemfile @@ -3,11 +3,10 @@ source "http://gemcutter.org" group :development do gem "rake" gem "gemcutter" - gem "ruby-debug19", :require => 'ruby-debug' end group :test do gem "rspec" - gem "rcov" + gem "simplecov", require: false end diff --git a/Gemfile.lock b/Gemfile.lock index f8034a6..cce22e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,11 @@ GEM remote: http://gemcutter.org/ specs: - archive-tar-minitar (0.5.2) - columnize (0.3.4) diff-lcs (1.1.2) + docile (1.1.5) gemcutter (0.7.0) - linecache19 (0.5.12) - ruby_core_source (>= 0.1.4) + json (1.8.3) rake (0.9.2) - rcov (0.9.9) rspec (2.6.0) rspec-core (~> 2.6.0) rspec-expectations (~> 2.6.0) @@ -17,16 +14,11 @@ GEM rspec-expectations (2.6.0) diff-lcs (~> 1.1.2) rspec-mocks (2.6.0) - ruby-debug-base19 (0.11.25) - columnize (>= 0.3.1) - linecache19 (>= 0.5.11) - ruby_core_source (>= 0.1.4) - ruby-debug19 (0.11.6) - columnize (>= 0.3.1) - linecache19 (>= 0.5.11) - ruby-debug-base19 (>= 0.11.19) - ruby_core_source (0.1.5) - archive-tar-minitar (>= 0.5.2) + simplecov (0.11.2) + docile (~> 1.1.0) + json (~> 1.8) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.0) PLATFORMS ruby @@ -34,6 +26,8 @@ PLATFORMS DEPENDENCIES gemcutter rake - rcov rspec - ruby-debug19 + simplecov + +BUNDLED WITH + 1.12.2 diff --git a/README.md b/README.md index 012b322..db92036 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,29 @@ def user end ~~~ +You can use traits... + +~~~ruby +Factory.build(:user, traits: [:admin, :manager]) + +def user + u = User.new + u.name = options[:name] || "Jimmy" + u.email = options[:email] || "jimmy#{n}@home.com" + u.role = options[:role] || "pleb" + u.address = options[:new_address] ? create(:address) : default_address + u +end + +def apply_trait_admin_to_user(user) + user.roles << :admin +end + +def apply_trait_manager_to_user(user) + user.roles << :manager +end +~~~ + ## Helpers Of course its nice to get some help... diff --git a/lib/cranky/factory.rb b/lib/cranky/factory.rb index 23f2830..682a1de 100644 --- a/lib/cranky/factory.rb +++ b/lib/cranky/factory.rb @@ -58,6 +58,16 @@ def debug!(*args) private + def apply_traits(what, item) + Array(options[:traits]).each do |t| + trait_method_name = "apply_trait_#{t}_to_#{what}" + fail("Invalid trait '#{t}'! No method '#{trait_method_name}' is defined.") unless respond_to?(trait_method_name) + send(trait_method_name, item) + end + + item + end + def n @n += 1 end @@ -71,6 +81,7 @@ def crank_it(what, overrides) item = "TBD" new_job(what, overrides) do item = self.send(what) # Invoke the factory method + item = apply_traits(what, item) end item end diff --git a/spec/cranky_spec.rb b/spec/cranky_spec.rb index 711efb0..be4bbcf 100644 --- a/spec/cranky_spec.rb +++ b/spec/cranky_spec.rb @@ -127,6 +127,15 @@ Factory.build(:user_hash, :role => :admin)[:role].should == :admin end + it "is capable of using traits" do + user = Factory.build(:user_manually, :traits => :manager) + user.role.should == :manager + end + + it "raises exception if trait method is undefined" do + expect { Factory.build(:user_by_define, :traits => :manager) }.to raise_error("Invalid trait 'manager'! No method 'apply_trait_manager_to_user_by_define' is defined.") + end + specify "attributes are not assigned when they have the value :skip" do crank(:user, :name => :skip).name.should_not be end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c575bd0..1f779f7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ -require 'rubygems' -require 'rspec' -require 'cranky' +require 'simplecov' +SimpleCov.start +require 'cranky' class TestClass attr_accessor :valid @@ -91,6 +91,10 @@ def user_hash :name => "Fred", :role => :user end + + def apply_trait_manager_to_user_manually(user) + user.role = :manager + end end