Revisions
-
stephencelis revised this gist
Jan 12, 2010 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,10 @@ # Factory.define :post do |f| # f.user { Factory :user } # Blocks, if you must. # end class Miniskirt < Struct.new(:__klass__) undef_method *instance_methods.grep(/^(?!__|object_id)/) # BlankerSlate. @@factories = {} and private_class_method :new class << self def define(name) @@factories[name = name.to_s] = {} and yield new(name) @@ -33,7 +35,7 @@ def create(name, attrs = {}) end def method_missing(name, value = nil, &block) @@factories[__klass__][name] = block || value end end -
stephencelis revised this gist
Jan 11, 2010 . 1 changed file with 4 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,17 +17,14 @@ def define(name) end def build(name, attrs = {}) (name, n = name.to_s) and (m = name.classify.constantize).new do |rec| attrs.stringify_keys!.reverse_update(@@factories[name]).each do |k, v| rec.send "#{k}=", case v when String # Sequence and interpolate. v.sub(/%\d*d/) {|d| d % n ||= m.maximum(:id).to_i + 1} % attrs % n when Proc then v.call(rec) else v end end end end def create(name, attrs = {}) -
stephencelis revised this gist
Jan 10, 2010 . 1 changed file with 8 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,21 +9,16 @@ # Factory.define :post do |f| # f.user { Factory :user } # Blocks, if you must. # end class Miniskirt < Struct.new(:klass) @@factories = {} and private_class_method :new class << self def define(name) @@factories[name = name.to_s] = {} and yield new(name) end def build(name, attrs = {}) name = name.to_s and (mod = name.classify.constantize).new do |record| attrs.stringify_keys!.reverse_update(@@factories[name]).each do |k, v| record.send "#{k}=", case v when String # Sequence and interpolate. v.sub(/%\d*d/) { |n| n % @n ||= mod.maximum(:id).to_i + 1 } % attrs % @n @@ -39,6 +34,10 @@ def create(name, attrs = {}) build(name, attrs).tap { |record| record.save } end end def method_missing(name, value = nil, &block) @@factories[klass][name] = block || value end end def Miniskirt(name, attrs = {}) -
stephencelis revised this gist
Jan 10, 2010 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,12 +25,14 @@ def build(name, attrs = {}) name = name.to_s and (mod = name.classify.constantize).new do |record| attrs.stringify_keys!.reverse_update(factories[name]).each do |k, v| record.send "#{k}=", case v when String # Sequence and interpolate. v.sub(/%\d*d/) { |n| n % @n ||= mod.maximum(:id).to_i + 1 } % attrs % @n when Proc then v.call(record) else v end end end ensure @n = nil end def create(name, attrs = {}) -
stephencelis revised this gist
Jan 10, 2010 . 1 changed file with 23 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,36 @@ # Factory girl, relaxed. # # Factory.define :user do |f| # f.login 'johndoe%d' # Sequence. # f.email '%{login}@example.com' # Interpolate. # f.password f.password_confirmation('foobar') # Chain. # end # # Factory.define :post do |f| # f.user { Factory :user } # Blocks, if you must. # end module Miniskirt @@factories = {} and mattr_reader :factories class << self def define(name) factories[name.to_s] = {} and yield BasicObject.new.instance_eval(%{ def method_missing(name, value = nil, &block) ::Miniskirt.factories["#{name}"][name] = block || value end self }) end def build(name, attrs = {}) name = name.to_s and (mod = name.classify.constantize).new do |record| attrs.stringify_keys!.reverse_update(factories[name]).each do |k, v| record.send "#{k}=", case v when String # Sequence and interpolate. v.sub!(/%\d*d/) { |n| n % @n ||= mod.maximum(:id).to_i + 1 } v % attrs when Proc then v.call(record) else v end end end end def create(name, attrs = {}) @@ -27,4 +44,4 @@ def Miniskirt(name, attrs = {}) end Factory = Miniskirt alias Factory Miniskirt -
stephencelis revised this gist
Jan 10, 2010 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,8 @@ module Miniskirt mattr_reader :factories @@factories = {} class << self def define(name) factories[name.to_s] = {} and yield BasicObject.new.instance_eval(%{ def method_missing(name, value) ::Miniskirt.factories["#{name}"][name] = value -
stephencelis revised this gist
Jan 10, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ module Miniskirt @@factories = {} class << self def define(name, &block) factories[name.to_s] = {} and yield BasicObject.new.instance_eval(%{ def method_missing(name, value) ::Miniskirt.factories["#{name}"][name] = value -
stephencelis created this gist
Jan 10, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ # Factory girl, relaxed. module Miniskirt mattr_reader :factories @@factories = {} class << self def define(name, options = {}, &block) factories[name.to_s] = {} and yield BasicObject.new.instance_eval(%{ def method_missing(name, value) ::Miniskirt.factories["#{name}"][name] = value end self }) end def build(name, attrs = {}) name.to_s.classify.constantize.new factories[name.to_s].merge(attrs) end def create(name, attrs = {}) build(name, attrs).tap { |record| record.save } end end end def Miniskirt(name, attrs = {}) Miniskirt.create(name, attrs) end Factory = Miniskirt alias Factory Miniskirt