Class: Koujou::DataGenerator
- Inherits:
-
Object
- Object
- Koujou::DataGenerator
- Defined in:
- lib/koujou/data_generator.rb
Instance Attribute Summary collapse
-
#inclusion_values ⇒ Object
Returns the value of attribute inclusion_values.
-
#required_length ⇒ Object
Returns the value of attribute required_length.
Instance Method Summary collapse
- #generate_boolean ⇒ Object
- #generate_data_for_column_type ⇒ Object
- #generate_date ⇒ Object
- #generate_datetime ⇒ Object
- #generate_float ⇒ Object
- #generate_integer ⇒ Object
- #generate_string ⇒ Object
- #generate_text ⇒ Object
- #generate_time ⇒ Object
-
#initialize(sequenced, validation) ⇒ DataGenerator
constructor
A new instance of DataGenerator.
Constructor Details
#initialize(sequenced, validation) ⇒ DataGenerator
Returns a new instance of DataGenerator.
6 7 8 9 10 11 12 |
# File 'lib/koujou/data_generator.rb', line 6 def initialize(sequenced, validation) @sequenced = sequenced # Validation is actually a ActiveRecord::Reflection::MacroReflection @validation = validation @required_length = nil @inclusion_values = nil end |
Instance Attribute Details
#inclusion_values ⇒ Object
Returns the value of attribute inclusion_values.
4 5 6 |
# File 'lib/koujou/data_generator.rb', line 4 def inclusion_values @inclusion_values end |
#required_length ⇒ Object
Returns the value of attribute required_length.
4 5 6 |
# File 'lib/koujou/data_generator.rb', line 4 def required_length @required_length end |
Instance Method Details
#generate_boolean ⇒ Object
91 92 93 |
# File 'lib/koujou/data_generator.rb', line 91 def generate_boolean true end |
#generate_data_for_column_type ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/koujou/data_generator.rb', line 14 def generate_data_for_column_type # So if there was an inclusion passed in for validations_inclusion_of (which is just # an enumberable object), let's just return the first element to ensure the value # set is the correct one. Mmmkay? return get_first_value_for_inclusion unless @inclusion_values.nil? # Sometimes models have a validates_presence_of set, but there's no corresponding # db column. The only example I can think of for this is a user model where the actual # column is hashed_password but the model requires the presence of password. So we'll # assume string. This could bite me in the ass later, but we'll see. if @validation.active_record.columns_hash.has_key?("#{@validation.name}") db_type = @validation.active_record.columns_hash["#{@validation.name}"].type else db_type = 'string' end # Since the method names are all based on the db types, we'll just go ahead and # dynamically call the appropriate one. send("generate_#{db_type}") end |
#generate_date ⇒ Object
83 84 85 |
# File 'lib/koujou/data_generator.rb', line 83 def generate_date DateTime.now.to_date end |
#generate_datetime ⇒ Object
79 80 81 |
# File 'lib/koujou/data_generator.rb', line 79 def generate_datetime DateTime.now end |
#generate_float ⇒ Object
75 76 77 |
# File 'lib/koujou/data_generator.rb', line 75 def generate_float generate_number.to_f end |
#generate_integer ⇒ Object
71 72 73 |
# File 'lib/koujou/data_generator.rb', line 71 def generate_integer generate_number end |
#generate_string ⇒ Object
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 |
# File 'lib/koujou/data_generator.rb', line 33 def generate_string retval = '' # I don't really like all these elsif's but, apparently # it's more efficient than the numerous explicit returns # we used to have. SEE: http://gist.github.com/160718 if @validation.name.to_s.match(/email/) retval = format_if_sequenced(Faker::Internet.email) elsif @validation.name.to_s == 'first_name' retval = format_if_sequenced(Faker::Name.first_name) elsif @validation.name.to_s == 'last_name' retval = format_if_sequenced(Faker::Name.last_name) elsif @validation.name.to_s.match(/login|user_name/) retval = format_if_sequenced(Faker::Internet.user_name) elsif @validation.name.to_s.match(/city/) retval = format_if_sequenced(Faker::Address.city) elsif @validation.name.to_s.match(/state|province/) retval = format_if_sequenced(Faker::Address.us_state) elsif @validation.name.to_s.match(/zip|postal/) retval = format_if_sequenced(Faker::Address.zip_code) else # If we don't match any standard stuff, just return a regular bs lorem string comprised of 10 words. # 10 is sort of a "magic number" I might make a constant for that. standard_text = format_if_sequenced(Faker::Lorem.words(10).to_s) # So if there's a length validation set, we need to return just that amount of data. retval = @required_length ? standard_text[0..@required_length - 1].to_s : standard_text end retval end |
#generate_text ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/koujou/data_generator.rb', line 62 def generate_text if @required_length # So if there's a length validation set, we need to return just that amount of data. Faker::Lorem.paragraph(2).to_s[0..@required_length - 1] else Faker::Lorem.paragraph.to_s end end |
#generate_time ⇒ Object
87 88 89 |
# File 'lib/koujou/data_generator.rb', line 87 def generate_time Time.now end |