Parent

DataMapper::Sweatshop

Attributes

model_map[RW]
record_map[RW]

Public Class Methods

add(klass, name, &proc) click to toggle source

Adds a Proc to model map. Proc must return a Hash of attributes.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param instance [DataMapper::Resource]

@api private

@return [Array] model map

    # File lib/dm-sweatshop/sweatshop.rb, line 32
32:     def self.add(klass, name, &proc)
33:       self.model_map[klass][name.to_sym] << proc
34:     end
attributes(klass, name) click to toggle source

Returns a Hash of attributes from the model map

@param klass [Class, DataMapper::Resource] @param name [Symbol]

@return [Hash] existing instance of a model from the model map @raise NoFixtureExist when requested fixture does not exist in the model map

@api private

     # File lib/dm-sweatshop/sweatshop.rb, line 114
114:     def self.attributes(klass, name)
115:       proc = model_map[klass][name.to_sym].pick
116: 
117:       if proc
118:         expand_callable_values(proc.call)
119:       elsif klass.superclass.is_a?(DataMapper::Model)
120:         attributes(klass.superclass, name)
121:       else
122:         raise NoFixtureExist, "#{name} fixture was not found for class #{klass}"
123:       end
124:     end
create(klass, name, attributes = {}) click to toggle source

Creates an instance from given hash of attributes, saves it and adds it to the record map.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param attributes [Hash]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 74
74:     def self.create(klass, name, attributes = {})
75:       record(klass, name, klass.create(attributes(klass, name).merge(attributes)))
76:     end
create!(klass, name, attributes = {}) click to toggle source

Same as create but calls Model#create! and does save invalid models

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param attributes [Hash]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 60
60:     def self.create!(klass, name, attributes = {})
61:       record(klass, name, klass.create!(attributes(klass, name).merge(attributes)))
62:     end
expand_callable_values(hash) click to toggle source

Returns a Hash with callable values evaluated.

@param hash [Hash]

@return [Hash] existing instance of a model from the model map

@api private

     # File lib/dm-sweatshop/sweatshop.rb, line 133
133:     def self.expand_callable_values(hash)
134:       expanded = {}
135:       hash.each do |key, value|
136:         if value.respond_to?(:call)
137:           expanded[key] = value.call
138:         else
139:           expanded[key] = value
140:         end
141:       end
142:       expanded
143:     end
make(klass, name, attributes = {}) click to toggle source

Creates an instance from given hash of attributes and adds it to records map without saving.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param attributes [Hash]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 88
88:     def self.make(klass, name, attributes = {})
89:       record(klass, name, klass.new(attributes(klass, name).merge(attributes)))
90:     end
pick(klass, name) click to toggle source

Returns a pre existing instance of a model from the record map

@param klass [Class, DataMapper::Resource] @param name [Symbol]

@return [DataMapper::Resource] existing instance of a model from the record map @raise DataMapper::Sweatshop::NoFixtureExist when requested fixture does not exist in the record map

@api private

     # File lib/dm-sweatshop/sweatshop.rb, line 101
101:     def self.pick(klass, name)
102:       self.record_map[klass][name.to_sym].pick || raise(NoFixtureExist, "no #{name} context fixtures have been generated for the #{klass} class")
103:     end
record(klass, name, instance) click to toggle source

Adds an instance to records map.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param instance [DataMapper::Resource]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 45
45:     def self.record(klass, name, instance)
46:       self.record_map[klass][name.to_sym] << instance
47:       instance
48:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.