module DataMapper::Model
Methods added to this module are available on classes that include DataMapper::Resource.
This lets you use Person.pick(:michael) instead of DataMapper::Sweatshop.pick
(Person, :michael)
Public Instance Methods
Default fauxture name. Usually :default.
@return [Symbol] default fauxture name @api public
# File lib/dm-sweatshop/model.rb, line 103 def default_fauxture_name :default end
Adds a fixture to record map. Block is supposed to return a hash of attributes.
@param name [Symbol, String] Name of the fixture @param blk [Proc] A proc that returns fixture attributes
@return nil
@api public
# File lib/dm-sweatshop/model.rb, line 17 def fixture(name = default_fauxture_name, &blk) Sweatshop.add(self, name, &blk) end
Creates an instance from hash of attributes, saves it and adds it to the record map. Attributes given as the second argument are merged into attributes from fixture.
If record is valid because of duplicated property value, this method does a retry.
@param name [Symbol] @param attributes [Hash]
@api public
@return [DataMapper::Resource] added instance
# File lib/dm-sweatshop/model.rb, line 36 def generate(name = default_fauxture_name, attributes = {}) name, attributes = default_fauxture_name, name if name.is_a? Hash Sweatshop.create(self, name, attributes) end
Same as generate except that it uses Model#create!. It forces invalid objects to be saved in the repository.
@param name [Symbol] @param attributes [Hash]
@api public
@return [DataMapper::Resource] added instance
# File lib/dm-sweatshop/model.rb, line 52 def generate!(name = default_fauxture_name, attributes = {}) name, attributes = default_fauxture_name, name if name.is_a? Hash Sweatshop.create!(self, name, attributes) end
Returns a Hash of attributes from the model map.
@param name [Symbol] name of the fauxture to use
@return [Hash] existing instance of a model from the model map @raise NoFixtureExist when requested fixture does not exist in the model map
@api public
# File lib/dm-sweatshop/model.rb, line 67 def generate_attributes(name = default_fauxture_name) Sweatshop.attributes(self, name) end
Creates an instance from given hash of attributes and adds it to records map without saving.
@param name [Symbol] name of the fauxture to use @param attributes [Hash]
@api private
@return [DataMapper::Resource] added instance
# File lib/dm-sweatshop/model.rb, line 82 def make(name = default_fauxture_name, attributes = {}) name, attributes = default_fauxture_name, name if name.is_a? Hash Sweatshop.make(self, name, attributes) end
Returns a pre existing instance of a model from the record map
@param name [Symbol] name of the fauxture to pick
@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 public
# File lib/dm-sweatshop/model.rb, line 95 def pick(name = default_fauxture_name) Sweatshop.pick(self, name) end