Class: Watirmark::Model::Factory
- Extended by:
- FactoryMethods
- Includes:
- CucumberHelper, DebugMethods, FactoryMethodGenerators
- Defined in:
- lib/watirmark/models/factory.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#keywords ⇒ Object
readonly
Returns the value of attribute keywords.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#model_type ⇒ Object
Returns the value of attribute model_type.
-
#models ⇒ Object
Returns the value of attribute models.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#updates ⇒ Object
readonly
Returns the value of attribute updates.
Attributes included from FactoryMethods
#default, #included_traits, #keys, #model_type_name, #search
Instance Method Summary collapse
- #add_model(model) ⇒ Object
- #clear_updates ⇒ Object
- #find(model_class) ⇒ Object
- #hash_id(size = nil, type = :hex) ⇒ Object
- #includes?(hash) ⇒ Boolean
-
#initialize(params = {}) ⇒ Factory
constructor
A new instance of Factory.
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(obj) ⇒ Object
- #model_class_name ⇒ Object
- #remove_empty_entries(hash) ⇒ Object
-
#search_term ⇒ Object
The search_term, used for a controller’s search can be defined in this model or will look in a parent’s model.
- #strip_equals_from_method_name(method) ⇒ Object
- #to_h ⇒ Object
- #unique_instance_name ⇒ Object
-
#update(hash) ⇒ Object
(also: #has)
Update the model using the provided hash.
-
#update_existing_members(hash) ⇒ Object
Update the model using the provided hash but only if exists (TODO: may not be needed any more).
- #uuid ⇒ Object
Methods included from FactoryMethods
Methods included from FactoryMethodGenerators
Methods included from DebugMethods
Methods included from CucumberHelper
#format_value, #insert_model, #merge_cucumber_table
Constructor Details
#initialize(params = {}) ⇒ Factory
Returns a new instance of Factory.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/watirmark/models/factory.rb', line 30 def initialize(params={}) @params = params @model_type = self.class.model_type_name @search = self.class.search || Proc.new{nil} @keywords = self.class.keys.dup || [] @children = self.class.children.dup.map(&:new) @updates = [] set_model_name set_default_values create_model_getters_and_setters set_initial_values Watirmark.logger.info inspect end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Watirmark::Model::FactoryMethodGenerators
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
17 18 19 |
# File 'lib/watirmark/models/factory.rb', line 17 def children @children end |
#defaults ⇒ Object
Returns the value of attribute defaults.
17 18 19 |
# File 'lib/watirmark/models/factory.rb', line 17 def defaults @defaults end |
#keywords ⇒ Object (readonly)
Returns the value of attribute keywords.
18 19 20 |
# File 'lib/watirmark/models/factory.rb', line 18 def keywords @keywords end |
#model_name ⇒ Object
Returns the value of attribute model_name.
17 18 19 |
# File 'lib/watirmark/models/factory.rb', line 17 def model_name @model_name end |
#model_type ⇒ Object
Returns the value of attribute model_type.
17 18 19 |
# File 'lib/watirmark/models/factory.rb', line 17 def model_type @model_type end |
#models ⇒ Object
Returns the value of attribute models.
17 18 19 |
# File 'lib/watirmark/models/factory.rb', line 17 def models @models end |
#parent ⇒ Object
Returns the value of attribute parent.
17 18 19 |
# File 'lib/watirmark/models/factory.rb', line 17 def parent @parent end |
#updates ⇒ Object (readonly)
Returns the value of attribute updates.
18 19 20 |
# File 'lib/watirmark/models/factory.rb', line 18 def updates @updates end |
Instance Method Details
#add_model(model) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/watirmark/models/factory.rb', line 65 def add_model(model) @children << model create_child_methods Watirmark.logger.info "Added Model #{model.inspect} to #{model_name || model_class_name}" return self end |
#clear_updates ⇒ Object
118 119 120 |
# File 'lib/watirmark/models/factory.rb', line 118 def clear_updates @updates = [] end |
#find(model_class) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/watirmark/models/factory.rb', line 73 def find(model_class) return self if self.kind_of? model_class @children.each do |m| return m if m.model_type == model_class found_model = m.find model_class return found_model if found_model end nil end |
#hash_id(size = nil, type = :hex) ⇒ Object
48 49 50 51 52 |
# File 'lib/watirmark/models/factory.rb', line 48 def hash_id(size = nil, type = :hex) size = size || Watirmark::Configuration.instance.hash_id_length || 8 seed = Watirmark::Configuration.instance.hash_id_seed || "Watirmark Default Seed" @hash_id ||= generate_hash_id seed, size, type end |
#includes?(hash) ⇒ Boolean
101 102 103 104 |
# File 'lib/watirmark/models/factory.rb', line 101 def includes? hash hash.each_pair { |key, value| return false unless send("#{key}") == value } true end |
#inspect ⇒ Object
84 85 86 87 88 89 |
# File 'lib/watirmark/models/factory.rb', line 84 def inspect model_friendly_name = @model_name ? "#@model_name: " : nil model_details = " #{to_h}" unless to_h.empty? included_models = "\n #{@children.map(&:inspect).join("\n ")}" unless @children.empty? "#{model_friendly_name}#{model_class_name}#{model_details}#{included_models}" end |
#marshal_dump ⇒ Object
20 21 22 |
# File 'lib/watirmark/models/factory.rb', line 20 def marshal_dump [@keywords, @model_name, @models, @parent, @children, @model_type, self.to_h] end |
#marshal_load(obj) ⇒ Object
24 25 26 27 28 |
# File 'lib/watirmark/models/factory.rb', line 24 def marshal_load(obj) keyword_values = obj.pop @keywords, @model_name, @models, @parent, @children, @model_type = obj update keyword_values end |
#model_class_name ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/watirmark/models/factory.rb', line 92 def model_class_name name = self.class.inspect.to_s name = self.class.superclass.to_s if name.to_s =~ /Class/ name = 'Model' if name.to_s =~ /Module/ name.sub!(/.+::/,'') name end |
#remove_empty_entries(hash) ⇒ Object
130 131 132 |
# File 'lib/watirmark/models/factory.rb', line 130 def remove_empty_entries hash hash.delete_if {|k| k.nil? || k == ':' || k =~ /^\s+$/ || k.empty?} end |
#search_term ⇒ Object
The search_term, used for a controller’s search can be defined in this model or will look in a parent’s model. This allows us to define it once for a composed model
60 61 62 |
# File 'lib/watirmark/models/factory.rb', line 60 def search_term instance_eval(&@search) || (parent.search_term if parent) end |
#strip_equals_from_method_name(method) ⇒ Object
54 55 56 |
# File 'lib/watirmark/models/factory.rb', line 54 def strip_equals_from_method_name(method) method.to_s.delete('=').to_sym end |
#to_h ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/watirmark/models/factory.rb', line 134 def to_h h = {} @keywords.each do |key| begin name = key value = send(key) if value.kind_of?(Proc) h[name] = instance_eval(&value) unless value.nil? else h[name] = value unless value.nil? end rescue NoMethodError h[name] = "[defined at runtime]" end end h end |
#unique_instance_name ⇒ Object
152 153 154 155 156 157 |
# File 'lib/watirmark/models/factory.rb', line 152 def unique_instance_name class_name = self.class.name[/([^\:]+)Model$/i,1] model_name_exists = model_name.nil? ? false : (not model_name.empty?) unique_name = model_name_exists ? model_name : class_name.downcase unique_name_with_uuid = unique_name + "_" + uuid end |
#update(hash) ⇒ Object Also known as: has
Update the model using the provided hash
108 109 110 111 112 113 114 115 |
# File 'lib/watirmark/models/factory.rb', line 108 def update hash remove_empty_entries hash hash.each_pair do |key, value| @updates << key send "#{key}=", value end self end |
#update_existing_members(hash) ⇒ Object
Update the model using the provided hash but only if exists (TODO: may not be needed any more)
124 125 126 127 128 |
# File 'lib/watirmark/models/factory.rb', line 124 def update_existing_members hash remove_empty_entries hash hash.each_pair { |key, value| send "#{key}=", value if respond_to? "#{key}=".to_sym } self end |
#uuid ⇒ Object
44 45 46 |
# File 'lib/watirmark/models/factory.rb', line 44 def uuid @uuid ||= (Watirmark::Configuration.instance.uuid || UUID.new.generate(:compact)[0..9]).to_s end |