Module: Seed
- Defined in:
- lib/seed.rb,
lib/seed/version.rb
Defined Under Namespace
Modules: Version
Instance Method Summary collapse
-
#_instantiate_factories(method, options = nil, results = [], iteration = 0) ⇒ Object
:nodoc:.
-
#method_missing(method, *args) ⇒ Object
:nodoc:.
-
#respond_to?(method, include_private = false) ⇒ Boolean
:nodoc:.
-
#seed(text, options = {}) ⇒ Object
The
seed
method allows you to instantiate factories by parsing a simple text.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
:nodoc:
98 99 100 101 102 103 104 105 106 |
# File 'lib/seed.rb', line 98 def method_missing(method, *args) # :nodoc: method_name = method.to_s if method_name =~ /^_(\d+)/ _instantiate_factories(method_name.gsub(/_/, " "), args.first) else super end end |
Instance Method Details
#_instantiate_factories(method, options = nil, results = [], iteration = 0) ⇒ Object
:nodoc:
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/seed.rb', line 45 def _instantiate_factories(method, = nil, results = [], iteration = 0) # :nodoc: iteration += 1 # Set default options ||= {} # Retrieve count and main factory name _, size, what = *method.match(/^ ?(\d+) (.*?)$/) size = size.to_i associations = nil matches = what.match(/^(.*?) with (.*?)$/) if matches what = $1 associations = $2.split(" and ") end what.gsub!(/ /, "_") what = what.singularize if size > 1 records = Array.new(size) { Factory.create(what, ) } if size > 1 && iteration == 1 results = records elsif size > 1 results << records elsif size == 1 && iteration == 1 && !associations results = records.first else results << records.first end if associations records.each do |record| associations.each do |assoc| _instantiate_factories(assoc, .merge(what => record), results, iteration) end end end results end |
#respond_to?(method, include_private = false) ⇒ Boolean
:nodoc:
90 91 92 93 94 95 96 |
# File 'lib/seed.rb', line 90 def respond_to?(method, include_private = false) # :nodoc: if method.to_s =~ /^_\d+_[a-z_]+/ true else super end end |
#seed(text, options = {}) ⇒ Object
The seed
method allows you to instantiate factories by parsing a simple text.
seed "1 user"
seed "2 users"
seed "1 admin user"
seed "1 user with 10 posts"
seed "1 user with 10 posts and 10 comments"
seed "10 comments", :user => @user
You can set variables by using the returned result.
@user = seed("1 user")
@users = seed("10 users")
@user, @comments = seed("1 user with 10 comments")
@user, @comments, @posts = seed("1 user with 10 comments and 10 posts")
There’s a shortcut for the seed
method. All you have to do is replace the spaces by underscores and call it as a method by prefixing it with an underscore. Confuse? Not at all! This
seed("1 user")
becomes
_1_user
More samples:
@user = _1_user
@users = _10_users
@comment = _1_comment(:user => @user)
@comments = _10_comments
@user, @comments, @posts = _1_user_with_10_comments_and_3_posts
41 42 43 |
# File 'lib/seed.rb', line 41 def seed(text, = {}) _instantiate_factories(text, ) end |