Class: DataMapper::Sweatshop
- Inherits:
-
Object
- Object
- DataMapper::Sweatshop
- Extended by:
- Unique
- Defined in:
- lib/dm-sweatshop/unique.rb,
lib/dm-sweatshop/sweatshop.rb,
lib/dm-sweatshop/support/class_attributes.rb
Defined Under Namespace
Modules: ClassAttributes, Unique Classes: NoFixtureExist, UniqueWorker
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) ⇒ Array
private
Adds a Proc to model map.
-
.attributes(klass, name) ⇒ Hash
private
Returns a Hash of attributes from the model map.
-
.create(klass, name, attributes = {}) ⇒ DataMapper::Resource
private
Creates an instance from given hash of attributes, saves it and adds it to the record map.
-
.create!(klass, name, attributes = {}) ⇒ DataMapper::Resource
private
Same as create but calls Model#create! and does save invalid models.
-
.expand_callable_values(hash) ⇒ Hash
private
Returns a Hash with callable values evaluated.
-
.make(klass, name, attributes = {}) ⇒ DataMapper::Resource
private
Creates an instance from given hash of attributes and adds it to records map without saving.
-
.pick(klass, name) ⇒ DataMapper::Resource
private
Returns a pre existing instance of a model from the record map.
-
.record(klass, name, instance) ⇒ DataMapper::Resource
private
Adds an instance to records map.
Methods included from Unique
Class Attribute Details
.model_map ⇒ Object
Returns the value of attribute model_map.
12 13 14 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 12 def model_map @model_map end |
.record_map ⇒ Object
Returns the value of attribute record_map.
13 14 15 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 13 def record_map @record_map end |
Class Method Details
.add(klass, name, &proc) ⇒ Array
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.
32 33 34 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 32 def self.add(klass, name, &proc) self.model_map[klass][name.to_sym] << proc end |
.attributes(klass, name) ⇒ Hash
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
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 114 def self.attributes(klass, name) proc = model_map[klass][name.to_sym].pick if proc (proc.call) elsif klass.superclass.is_a?(DataMapper::Model) attributes(klass.superclass, name) else raise NoFixtureExist, "#{name} fixture was not found for class #{klass}" end end |
.create(klass, name, attributes = {}) ⇒ DataMapper::Resource
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.
74 75 76 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 74 def self.create(klass, name, attributes = {}) record(klass, name, klass.create(attributes(klass, name).merge(attributes))) end |
.create!(klass, name, attributes = {}) ⇒ DataMapper::Resource
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
60 61 62 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 60 def self.create!(klass, name, attributes = {}) record(klass, name, klass.create!(attributes(klass, name).merge(attributes))) end |
.expand_callable_values(hash) ⇒ Hash
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.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 133 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 = {}) ⇒ DataMapper::Resource
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.
88 89 90 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 88 def self.make(klass, name, attributes = {}) record(klass, name, klass.new(attributes(klass, name).merge(attributes))) end |
.pick(klass, name) ⇒ DataMapper::Resource
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
101 102 103 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 101 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) ⇒ DataMapper::Resource
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.
45 46 47 48 |
# File 'lib/dm-sweatshop/sweatshop.rb', line 45 def self.record(klass, name, instance) self.record_map[klass][name.to_sym] << instance instance end |