Module: Stratagem::AutoMock::ValueGenerator

Included in:
Factory
Defined in:
lib/stratagem/auto_mock/value_generator.rb

Constant Summary collapse

ADDRESSES =
[
  {:street => "1600 Pennsylvania Avenue, NW", :city => "Washington", :state => "DC", :zip_code => '20500', :phone => "(202) 395-2020", :fax => "(202) 395-2020"},
  {:street => "525 8th Ave", :city => "New York", :state => "NY", :zip_code => '10018', :phone => "(212) 869-6427", :fax => "(202) 395-2020" }
]
USERS =
[
  {:email => "[email protected]", :first => "winston", :last => "churchill", :login => "wchurchill#{rand 1000}", :password => 'P7D8y5fycaaaa00pa' },
  {:email => "[email protected]", :first => "brenda", :last => "walsh", :login => "bwalsh#{rand 1000}", :password => '@.04@49\PFtB00a0d' },
  {:email => "[email protected]", :first => "hiro", :last => "protagonist", :login => "bigtimehiro#{rand 1000}", :password => '4Kq651p0A534p7T01klj' }
]
GENERIC_NAMES =
[
  'supercalifragilisticexpialidocious',
  'hippopotomonstrosesquipedalian',
  'honorificabilitudinitatibus',
  'otorhinolaryngological',
  'tom'
]
GENERIC_URLS =
[
  'http://www.thewebsiteisdown.com',
  'http://www.failblog.org',
  'http://www.xkcd.org'
]
NUMERIC_TYPES =
[
  :decimal, :float, :integer
]

Instance Method Summary collapse

Instance Method Details

#generate_boolean(attribute_name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/stratagem/auto_mock/value_generator.rb', line 105

def generate_boolean(attribute_name)
  case rand(4)
    when 0
      false
    when 1
      true
    when 2
      '0'
    when 3
      '1'
  end
end

#generate_datetime(attribute_name) ⇒ Object



122
123
124
# File 'lib/stratagem/auto_mock/value_generator.rb', line 122

def generate_datetime(attribute_name)
  Time.now+rand(10.days)
end

#generate_float(attribute_name) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/stratagem/auto_mock/value_generator.rb', line 97

def generate_float(attribute_name)
  if (attribute_name.to_s.stratagem_contains_token?('price'))
    ((rand + rand(10)) * 100).round.to_f / 100
  else
    rand
  end
end

#generate_int(attribute_name) ⇒ Object



118
119
120
# File 'lib/stratagem/auto_mock/value_generator.rb', line 118

def generate_int(attribute_name)
  rand(1000)+1
end

#generate_string(attribute_name) ⇒ Object



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
89
90
91
92
93
94
95
# File 'lib/stratagem/auto_mock/value_generator.rb', line 58

def generate_string(attribute_name)
  attribute_name = attribute_name.to_s
  if (attribute_name.stratagem_contains_token?('_type'))
    nil
  elsif (attribute_name.stratagem_contains_token?('email') || attribute_name.stratagem_contains_token?('e-mail'))
    (random_noise(4,false) || '') + @user[:email]
  elsif (attribute_name.stratagem_contains_token?('login') || attribute_name.stratagem_contains_token?('username') || attribute_name.stratagem_contains_token?('user_name'))
    @user[:login] + (random_noise(4, false) || '')
  elsif (attribute_name.stratagem_contains_token?('first') && attribute_name.stratagem_contains_token?('name'))
    (random_noise(2, false) || '') + @user[:first]
  elsif (attribute_name.stratagem_contains_token?('last') && attribute_name.stratagem_contains_token?('name'))
    (random_noise(2, false) || '') + @user[:last]
  elsif (attribute_name.stratagem_contains_token?('password'))
    @user[:password]
  elsif (attribute_name.stratagem_contains_token?('zip_code') || attribute_name.stratagem_contains_token?('zipcode'))
    @address[:zip_code]
  elsif (attribute_name.stratagem_contains_token?('phone'))
    @address[:phone]
  elsif (attribute_name.stratagem_contains_token?('fax'))
    @address[:fax]
  elsif (attribute_name.stratagem_contains_token?('address'))
    @address[:street]
  elsif (attribute_name.stratagem_contains_token?('city'))
    @address[:city]
  elsif (attribute_name.stratagem_contains_token?('state'))
    @address[:state]
  elsif (attribute_name.stratagem_contains_token?('name'))
    value = GENERIC_NAMES[rand GENERIC_NAMES.size]
    noise = random_noise(4)
    value += noise unless value.nil? || noise.nil?
  elsif (attribute_name.stratagem_contains_token?('url'))
    GENERIC_URLS[rand GENERIC_URLS.size]
  elsif (attribute_name.stratagem_contains_token?('ip'))
    "#{rand(156)+100}.#{rand(156)+100}.#{rand(156)+100}.#{rand(156)+100}"
  else
    random_noise
  end
end

#generate_text(attribute_name) ⇒ Object



126
127
128
# File 'lib/stratagem/auto_mock/value_generator.rb', line 126

def generate_text(attribute_name)
  random_noise(200)
end

#generate_value(attribute_name, attribute_type) ⇒ Object



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
# File 'lib/stratagem/auto_mock/value_generator.rb', line 29

def generate_value(attribute_name, attribute_type)
  @address ||= ADDRESSES[rand ADDRESSES.size]
  @user ||= USERS[rand USERS.size]
  value = case attribute_type
    when :string
      generate_string(attribute_name)
    when :integer
      generate_int(attribute_name)
    when :datetime
      generate_datetime(attribute_name)
    when :date
      generate_datetime(attribute_name)
    when :text
      generate_text(attribute_name)
    when :boolean
      generate_boolean(attribute_name)
    when :decimal
      generate_float(attribute_name)
    when :float
      generate_float(attribute_name)
    when :symbol
      generate_string(attribute_name).to_sym
    else
      raise Stratagem::AutoMock::UnsupportedColumnTypeError.new("Attribute #{attribute_name} of type #{attribute_type} is not supported")
  end
  value = nil if (rand(20) == 1) && (!NUMERIC_TYPES.include?(attribute_type)) && (attribute_name.to_s !~ /password/)
  value
end