Class: DNApi::Sweatshop
- Inherits:
-
Object
- Object
- DNApi::Sweatshop
- Defined in:
- lib/dnapi/test/sweatshop.rb
Defined Under Namespace
Classes: NoFixtureExist
Class Attribute Summary collapse
-
.model_map ⇒ Object
Returns the value of attribute model_map.
-
.record_map ⇒ Object
Returns the value of attribute record_map.
Class Method Summary collapse
-
.add(klass, name, &proc) ⇒ Object
private
Adds a Proc to model map.
-
.attributes(klass, name) ⇒ Object
private
Returns a Hash of attributes from the model map.
-
.create(klass, name, attributes = {}) ⇒ Object
private
Creates an instance from given hash of attributes, saves it and adds it to the record map.
-
.create!(klass, name, attributes = {}) ⇒ Object
private
Same as create but calls Model#create! and does save invalid models.
-
.expand_callable_values(hash) ⇒ Object
private
Returns a Hash with callable values evaluated.
-
.make(klass, name, attributes = {}) ⇒ Object
private
Creates an instance from given hash of attributes and adds it to records map without saving.
-
.pick(klass, name) ⇒ Object
private
Returns a pre existing instance of a model from the record map.
-
.record(klass, name, instance) ⇒ Object
private
Adds an instance to records map.
Class Attribute Details
.model_map ⇒ Object
Returns the value of attribute model_map.
15 16 17 |
# File 'lib/dnapi/test/sweatshop.rb', line 15 def model_map @model_map end |
.record_map ⇒ Object
Returns the value of attribute record_map.
16 17 18 |
# File 'lib/dnapi/test/sweatshop.rb', line 16 def record_map @record_map end |
Class Method Details
.add(klass, name, &proc) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a Proc to model map. Proc must return a Hash of attributes.
35 36 37 |
# File 'lib/dnapi/test/sweatshop.rb', line 35 def self.add(klass, name, &proc) self.model_map[klass][name.to_sym] << proc end |
.attributes(klass, name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a Hash of attributes from the model map
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dnapi/test/sweatshop.rb', line 117 def self.attributes(klass, name) proc = model_map[klass][name.to_sym].pick if proc (proc.call) elsif klass.superclass.is_a?(Struct) attributes(klass.superclass, name) else raise NoFixtureExist, "#{name} fixture was not found for class #{klass}" end end |
.create(klass, name, attributes = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an instance from given hash of attributes, saves it and adds it to the record map.
77 78 79 |
# File 'lib/dnapi/test/sweatshop.rb', line 77 def self.create(klass, name, attributes = {}) record(klass, name, klass.create(attributes(klass, name).merge(attributes))) end |
.create!(klass, name, attributes = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Same as create but calls Model#create! and does save invalid models
63 64 65 |
# File 'lib/dnapi/test/sweatshop.rb', line 63 def self.create!(klass, name, attributes = {}) record(klass, name, klass.create!(attributes(klass, name).merge(attributes))) end |
.expand_callable_values(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a Hash with callable values evaluated.
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dnapi/test/sweatshop.rb', line 136 def self.(hash) = {} hash.each do |key, value| if value.respond_to?(:call) [key] = value.call else [key] = value end end end |
.make(klass, name, attributes = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an instance from given hash of attributes and adds it to records map without saving.
91 92 93 |
# File 'lib/dnapi/test/sweatshop.rb', line 91 def self.make(klass, name, attributes = {}) record(klass, name, klass.new(attributes(klass, name).merge(attributes))) end |
.pick(klass, name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a pre existing instance of a model from the record map
104 105 106 |
# File 'lib/dnapi/test/sweatshop.rb', line 104 def self.pick(klass, name) self.record_map[klass][name.to_sym].pick || raise(NoFixtureExist, "no #{name} context fixtures have been generated for the #{klass} class") end |
.record(klass, name, instance) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds an instance to records map.
48 49 50 51 |
# File 'lib/dnapi/test/sweatshop.rb', line 48 def self.record(klass, name, instance) self.record_map[klass][name.to_sym] << instance instance end |