Module: DataMapper::Resource

Module | DataMapper::Resource
In: | lib/dm-core/resource.rb

### Methods

<=> == [] []= append_inclusions attribute_dirty? attribute_get attribute_loaded? attribute_set attributes attributes= clean? collection create_hook descendants destroy destroy! destroyed? dirty? dirty_attributes eql? extra_inclusions hash included inspect key new? original_attributes reload repository reset save save! save_children save_parents save_self saved? update update! update_attributes update_hook

### Included Modules

Extlib::Assertions

### External Aliases

class | -> | model
| TODO: document @api public

### Attributes

collection | [W] | Collection this resource associated with. Used by SEL. @api private

### Public Class methods

append_inclusions(*inclusions)

@deprecated

descendants()

@deprecated

extra_inclusions()

@deprecated

included(model)

Makes sure a class gets all the methods when it includes Resource

@api private

### Public Instance methods

<=>(other)

Compares two Resources to allow them to be sorted

@param [Resource] other

      The other Resource to compare with

@return [Integer]

      Return 0 if Resources should be sorted as the same, -1 if the
      other Resource should be after self, and 1 if the other Resource
      should be before self

@api public

==(other)

Compares another Resource for equivalency

Resource is equal to `other` if they are the same object (identity) or if they are both of the *same base model* and all of their attributes are equivalent

@param [Resource] other

      the other Resource to compare with

@return [Boolean]

      true if they are equivalent, false if not

@api public

[](name)

Alias for attribute_get

[]=(name, value)

Alias for attribute_set

attribute_dirty?(name)

Checks if an attribute has unsaved changes

@param [Symbol] name

      name of attribute to check for unsaved changes

@return [Boolean]

      true if attribute has unsaved changes

@api semipublic

attribute_get(name)

Returns the value of the attribute.

Do not read from instance variables directly, but use this method. This method handles lazy loading the attribute and returning of defaults if nessesary.

@example

      class Foo
        include DataMapper::Resource

        property :first_name, String
        property :last_name,  String

        def full_name
          "#{attribute_get(:first_name)} #{attribute_get(:last_name)}"
        end

        # using the shorter syntax
        def name_for_address_book
          "#{last_name}, #{first_name}"
        end
      end

@param [Symbol] name

      name of attribute to retrieve

@return [Object]

      the value stored at that given attribute
      (nil if none, and default if necessary)

@api public

attribute_loaded?(name)

Checks if an attribute has been loaded from the repository

@example

      class Foo
        include DataMapper::Resource

        property :name,        String
        property :description, Text,   :lazy => false
      end

      Foo.new.attribute_loaded?(:description)   #=> false

@return [Boolean]

      true if ivar +name+ has been loaded

@return [Boolean]

      true if ivar +name+ has been loaded

@api private

attribute_set(name, value)

Sets the value of the attribute and marks the attribute as dirty if it has been changed so that it may be saved. Do not set from instance variables directly, but use this method. This method handles the lazy loading the property and returning of defaults if nessesary.

@example

      class Foo
        include DataMapper::Resource

        property :first_name, String
        property :last_name,  String

        def full_name(name)
          name = name.split(' ')
          attribute_set(:first_name, name[0])
          attribute_set(:last_name, name[1])
        end

        # using the shorter syntax
        def name_from_address_book(name)
          name = name.split(', ')
          first_name = name[1]
          last_name = name[0]
        end
      end

@param [Symbol] name

      name of attribute to set

@param [Object] value

      value to store

@return [Object]

      the value stored at that given attribute, nil if none,
      and default if necessary

@api public

attributes(key_on = :name)

Gets all the attributes of the Resource instance

@param [Symbol] key_on

      Use this attribute of the Property as keys.
      defaults to :name. :field is useful for adapters
      :property or nil use the actual Property object.

@return [Hash]

      All the attributes

@api public

attributes=(attributes)

Assign values to multiple attributes in one call (mass assignment)

@param [Hash] attributes

      names and values of attributes to assign

@return [Hash]

      names and values of attributes assigned

@api public

clean?()

Checks if the resource has no changes to save

@return [Boolean]

      true if the resource may not be persisted

@api public

collection()

Gets a Collection with the current Resource instance as its only member

@return [Collection, FalseClass]

      nil if this is a new record,
      otherwise a Collection with self as its only member

@api private

destroy()
destroy!()

Destroy the instance, remove it from the repository, bypassing hooks

@return [Boolean]

      true if resource was destroyed

@api public

destroyed?()

Checks if this Resource instance is destroyed

@return [Boolean]

      true if the resource has been destroyed

@api public

dirty?()

Checks if the resource has unsaved changes

@return [Boolean]

     true if resource may be persisted

@api public

dirty_attributes()

Hash of attributes that have unsaved changes

@return [Hash]

      attributes that have unsaved changes

@api semipublic

eql?(other)

Compares another Resource for equality

Resource is equal to `other` if they are the same object (identity) or if they are both of the *same model* and all of their attributes are equivalent

@param [Resource] other

      the other Resource to compare with

@return [Boolean]

      true if they are equal, false if not

@api public

hash()

Returns hash value of the object. Two objects with the same hash value assumed equal (using eql? method)

DataMapper resources are equal when their models have the same hash and they have the same set of properties

When used as key in a Hash or Hash subclass, objects are compared by eql? and thus hash value has direct effect on lookup

@api private

inspect()

Get a Human-readable representation of this Resource instance

      Foo.new   #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>

@return [String]

      Human-readable representation of this Resource instance

@api public

key()

Retrieve the key(s) for this resource.

This always returns the persisted key value, even if the key is changed and not yet persisted. This is done so all relations still work.

@return [Array(Key)]

      the key(s) identifying this resource

@api public

new?()

Checks if this Resource instance is new

@return [Boolean]

      true if the resource is new and not saved

@api public

original_attributes()

Hash of original values of attributes that have unsaved changes

@return [Hash]

      original values of attributes that have unsaved changes

@api semipublic

reload()

Reloads association and all child association

@return [Resource]

      the receiver, the current Resource instance

@api public

repository()

Repository this resource belongs to in the context of this collection or of the resource‘s class.

@return [Repository]

      the respository this resource belongs to, in the context of
      a collection OR in the instance's Model's context

@api semipublic

reset()

Reset the Resource to a similar state as a new record: removes it from identity map and clears original property values (thus making all properties non dirty)

@api private

save()
save!()

Save the instance and loaded, dirty associations to the data-store, bypassing hooks

@return [Boolean]

      true if Resource instance and all associations were saved

@api public

save_children(safe = true)

Saves the children resources

@return [Boolean]

      true if the children were successfully saved

@api private

save_parents(safe = true)

Saves the parent resources

@return [Boolean]

      true if the parents were successfully saved

@api private

save_self(safe = true)

Saves the resource

@return [Boolean]

      true if the resource was successfully saved

@api semipublic

saved?()

Checks if this Resource instance is saved

@return [Boolean]

      true if the resource has been saved

@api public

update(attributes = {})
update!(attributes = {})

Updates attributes and saves this Resource instance, bypassing hooks

@param [Hash] attributes

      attributes to be updated

@return [Boolean]

      true if resource and storage state match

@api public

update_attributes(attributes = {}, *allowed)

Deprecated API for updating attributes and saving Resource

@see update

@deprecated

### Protected Instance methods

create_hook()

Method for hooking callbacks on resource creation

@return [Boolean]

      true if the create was successful, false if not

@api private

update_hook()

Method for hooking callbacks on resource updates

@return [Boolean]

      true if the update was successful, false if not

@api private

[Validate]