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

Instance Method Details

#default_fauxture_nameSymbol

Default fauxture name. Usually :default.

Returns:

  • (Symbol)

    default fauxture name


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.

Parameters:

  • name (Symbol, String) (defaults to: default_fauxture_name)

    Name of the fixture

  • blk (Proc)

    A proc that returns fixture attributes

Returns:

  • nil


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.

Parameters:

  • name (Symbol) (defaults to: default_fauxture_name)
  • attributes (Hash) (defaults to: {})

Returns:

  • (DataMapper::Resource)

    added instance


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.

Parameters:

  • name (Symbol) (defaults to: default_fauxture_name)
  • attributes (Hash) (defaults to: {})

Returns:

  • (DataMapper::Resource)

    added instance


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.

Parameters:

  • name (Symbol) (defaults to: default_fauxture_name)

    name of the fauxture to use

Returns:

  • (Hash)

    existing instance of a model from the model map

Raises:

  • NoFixtureExist when requested fixture does not exist in 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.

Parameters:

  • name (Symbol) (defaults to: default_fauxture_name)

    name of the fauxture to use

  • attributes (Hash) (defaults to: {})

Returns:

  • (DataMapper::Resource)

    added instance


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

#pick(name = default_fauxture_name) ⇒ DataMapper::Resource

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

Parameters:

  • name (Symbol) (defaults to: default_fauxture_name)

    name of the fauxture to pick

Returns:

  • (DataMapper::Resource)

    existing instance of a model from the record map

Raises:

  • DataMapper::Sweatshop::NoFixtureExist when requested fixture does not exist in the record map


95
96
97
# File 'lib/dm-sweatshop/model.rb', line 95

def pick(name = default_fauxture_name)
  Sweatshop.pick(self, name)
end