Skip to content

Commit

Permalink
Merge pull request ginty#5 from beorc/traits
Browse files Browse the repository at this point in the history
Implement traits
  • Loading branch information
ginty committed May 30, 2016
2 parents 47b2c9e + 7933271 commit 4b3e710
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

28 changes: 11 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -17,23 +14,20 @@ 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

DEPENDENCIES
gemcutter
rake
rcov
rspec
ruby-debug19
simplecov

BUNDLED WITH
1.12.2
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
11 changes: 11 additions & 0 deletions lib/cranky/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/cranky_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rubygems'
require 'rspec'
require 'cranky'
require 'simplecov'
SimpleCov.start

require 'cranky'

class TestClass
attr_accessor :valid
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 4b3e710

Please sign in to comment.