Class: Mack::Data::Field
Overview
:nodoc:
Instance Attribute Summary collapse
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#field_rules ⇒ Object
Returns the value of attribute field_rules.
-
#field_value ⇒ Object
Returns the value of attribute field_value.
-
#field_value_producer ⇒ Object
Returns the value of attribute field_value_producer.
Instance Method Summary collapse
- #get_value(index = 0) ⇒ Object
-
#initialize(hash = {}) ⇒ Field
constructor
A new instance of Field.
Constructor Details
#initialize(hash = {}) ⇒ Field
Returns a new instance of Field.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mack-data_factory/field.rb', line 13 def initialize(hash = {}) #puts "Inititalizing DataFactory's Field object:" hash.each_pair do |k, v| #puts "--> Setting #{v} to #{k}" self.send("#{k}=", v) end self.field_rules = { :default => "", :immutable => false, :length => 256, :add_space => true, :content => :alpha_numeric, :null_frequency => nil }.merge!(hash[:field_rules]) if hash[:field_rules] != nil self.field_value = self.field_rules[:default] # transform the content type based on the given default value if self.field_value.is_a?(Fixnum) or self.field_value.is_a?(Integer) self.field_rules[:content] = :numeric end self.field_value_producer = Mack::Data::Factory::FieldContentGenerator.send("#{self.field_rules[:content]}_generator") end |
Instance Attribute Details
#field_name ⇒ Object
Returns the value of attribute field_name.
8 9 10 |
# File 'lib/mack-data_factory/field.rb', line 8 def field_name @field_name end |
#field_rules ⇒ Object
Returns the value of attribute field_rules.
11 12 13 |
# File 'lib/mack-data_factory/field.rb', line 11 def field_rules @field_rules end |
#field_value ⇒ Object
Returns the value of attribute field_value.
9 10 11 |
# File 'lib/mack-data_factory/field.rb', line 9 def field_value @field_value end |
#field_value_producer ⇒ Object
Returns the value of attribute field_value_producer.
10 11 12 |
# File 'lib/mack-data_factory/field.rb', line 10 def field_value_producer @field_value_producer end |
Instance Method Details
#get_value(index = 0) ⇒ Object
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 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 |
# File 'lib/mack-data_factory/field.rb', line 40 def get_value(index = 0) # return the field_value immediately if the rule states that it's immutable return self.field_value if self.field_rules[:immutable] # # if the value is a hash, then it's a relationship mapping if self.field_value.is_a?(Hash) and self.field_value[:df_assoc_map] map_data = self.field_value[:df_assoc_map] owner = map_data.keys[0] key = map_data[owner] begin owner_model = owner.to_s.camelcase.constantize bridge = Mack::Data::Bridge.new assoc_rules = self.field_rules[:assoc] || :spread assoc_rules = :spread if !([:first, :last, :random, :spread].include?(assoc_rules)) # cache the query once if Mack::Data::RegistryMap.registered_items[self.field_name.to_sym] == nil all_owner_models = bridge.get_all(owner_model) Mack::Data::RegistryMap.add(self.field_name.to_sym, all_owner_models) end all_owner_models = Mack::Data::RegistryMap.registered_items[self.field_name.to_sym][0] case assoc_rules when :first value = all_owner_models[0].send(key.to_s) when :last value = all_owner_models[all_owner_models.size - 1].send(key) when :random my_index = rand((all_owner_models.size - 1)) value = all_owner_models[my_index].send(key) when :spread # if index >= all_owner_models.size # my_index = rand((all_owner_models.size - 1)) # else # my_index = index # end my_index = index % all_owner_models.size value = all_owner_models[my_index].send(key) end return value rescue Exception => ex Mack.logger.warn "WARNING: DataFactory: id_map for #{field_name} is not set properly because data relationship defined is not correct" end end # must generate random string and also respect the rules self.field_value_producer.call(self.field_value, self.field_rules, index) end |