Module: Modlr

Defined in:
lib/modlr.rb,
lib/modlr/ages.rb,
lib/modlr/email.rb,
lib/modlr/names.rb,
lib/modlr/phone.rb,
lib/modlr/address.rb,
lib/modlr/version.rb

Defined Under Namespace

Modules: Address, Age, Email, Name, Phone

Constant Summary collapse

VERSION =
"0.1"

Class Method Summary collapse

Class Method Details

.modlr(model, args = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/modlr.rb', line 26

def self.modlr(model, args = nil)
  args ||= {}
  model.new.is_a?(ActiveRecord::Base)
  count = model.count
  args[:number].nil? ? num = 10 : num = args[:number]
  if count >= num
    "No new records created: #{model} currently has #{count} records." 
  else
    unless args.nil?
      params = {}
      args.each do |key, value|
        unless key == :number
          params[key] = value
        end
      end
      #return params
      num.times do 
        m = model.new
        params.each do |key, value|
          m[key] = case value
            when :name
              Name.rand_name
            when :adult
              Age.adult
            when :child
              Age.child
            when :phone
              Phone.random
            when :address
              Address.random
            when :email
              Email.us
          end
        end
        m.save
      end
    end
  end  
end

.read_config_fileObject

modlr :class, number_of_records, => :type, :field => type

If I have a User class, with fields name (string), and age (int), then
`modlr :user, 100, {:name => :name, :age => :age}`
will create 100 user records with names and ages which make sense.


21
22
23
24
# File 'lib/modlr.rb', line 21

def self.read_config_file
  data = File.read("config/modlr.rb")
  eval(data)
end