Module: DataMapper::Model
- Defined in:
- lib/dm-sweatshop/model.rb
Overview
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)
Instance Method Summary collapse
-
#default_fauxture_name ⇒ Symbol
Default fauxture name.
-
#fixture(name = default_fauxture_name, &blk) ⇒ Object
(also: #fix)
Adds a fixture to record map.
-
#generate(name = default_fauxture_name, attributes = {}) ⇒ DataMapper::Resource
(also: #gen)
Creates an instance from hash of attributes, saves it and adds it to the record map.
-
#generate!(name = default_fauxture_name, attributes = {}) ⇒ DataMapper::Resource
(also: #gen!)
Same as generate except that it uses Model#create!.
-
#generate_attributes(name = default_fauxture_name) ⇒ Hash
(also: #gen_attrs)
Returns a Hash of attributes from the model map.
-
#make(name = default_fauxture_name, attributes = {}) ⇒ DataMapper::Resource
private
Creates an instance from given hash of attributes and adds it to records map without saving.
-
#pick(name = default_fauxture_name) ⇒ DataMapper::Resource
Returns a pre existing instance of a model from the record map.
Instance Method Details
#default_fauxture_name ⇒ Symbol
Default fauxture name. Usually :default.
103 104 105 |
# File 'lib/dm-sweatshop/model.rb', line 103 def default_fauxture_name :default end |
#fixture(name = default_fauxture_name, &blk) ⇒ Object Also known as: fix
Adds a fixture to record map. Block is supposed to return a hash of attributes.
17 18 19 |
# File 'lib/dm-sweatshop/model.rb', line 17 def fixture(name = default_fauxture_name, &blk) Sweatshop.add(self, name, &blk) end |
#generate(name = default_fauxture_name, attributes = {}) ⇒ DataMapper::Resource Also known as: gen
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.
36 37 38 39 |
# 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 |
#generate!(name = default_fauxture_name, attributes = {}) ⇒ DataMapper::Resource Also known as: gen!
Same as generate except that it uses Model#create!. It forces invalid objects to be saved in the repository.
52 53 54 55 |
# 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 |
#generate_attributes(name = default_fauxture_name) ⇒ Hash Also known as: gen_attrs
Returns a Hash of attributes from the model map.
67 68 69 |
# File 'lib/dm-sweatshop/model.rb', line 67 def generate_attributes(name = default_fauxture_name) Sweatshop.attributes(self, name) end |
#make(name = default_fauxture_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.
82 83 84 85 |
# 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 |