Module: FactoryGrabber::Grab

Defined in:
lib/factory_grabber.rb

Constant Summary collapse

UNITS =

numbers 1 to 9

{
"one"       => 1, 
"two"       => 2, 
"three"     => 3, 
"four"      => 4, 
"five"      => 5, 
"six"       => 6, 
"seven"     => 7,
"eight"     => 8,
"nine"      => 9}
TEENS =

numbers 10 to 19

{
"ten"       => 10,
"eleven"    => 11,
"twelve"    => 12,
"thirteen"  => 13,
"fourteen"  => 14,
"fifteen"   => 15,
"sixteen"   => 16,
"seventeen" => 17,
"eighteen"  => 18,
"nineteen"  => 19}
TENS =

tens from 20 to 90

{
"twenty"  => 20,
"thirty"  => 30,
"forty"   => 40,
"fifty"   => 50,
"sixty"   => 60,
"seventy" => 70,
"eighty"  => 80,
"ninety"  => 90}
ALL_NUMBERS =

define an empty hash

{}

Class Method Summary collapse

Class Method Details

.method_missing(name, options = {}) ⇒ Object

methods called to Grab are not really methods, they’re ghost methods. If method_missing cannot identify the number and the factory to be created it will pass this up the chain to Object. @name is the method name called (symbol) and options is the hash of options passed to the method.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/factory_grabber.rb', line 72

def method_missing(name, options = {})
  @options = options
  @parts_in_name = name.to_s.split("_")
  
  # replace 'a' or 'an' with 'one'
  if @parts_in_name.first == "a" || @parts_in_name.first == "an"
    @parts_in_name.delete_at(0)
    @parts_in_name.unshift("one")
  end
  # rejoins the name with 'a' and 'an' replaced with 'one'
  @name = @parts_in_name.join("_")
  
  # check the options if there are any
  unless @options.empty?
    check_options_against_database
  end

  # results to be returned
  @results = []
  
  if @should_create_new
    number.times { @results << create_factory }
  else 
    # create new records if there is not a sufficient amount in the database
    required_records ? required_records.times { create_factory } : nil
  
    # find and return the desired records
    @results = klass_name_constant.all(:conditions => @options, :limit => number)
  end
  # return a single record if only one exists
  @results.size == 1 ? @results.first : @results
end