Module: DataMapper::Resource
- Extended by:
- Chainable
- Includes:
- Assertions
- Defined in:
- lib/dm-core/resource.rb,
lib/dm-core/resource/state.rb,
lib/dm-core/resource/state/clean.rb,
lib/dm-core/resource/state/dirty.rb,
lib/dm-core/resource/state/deleted.rb,
lib/dm-core/resource/state/immutable.rb,
lib/dm-core/resource/state/persisted.rb,
lib/dm-core/resource/state/transient.rb
Defined Under Namespace
Classes: State
Class Method Summary collapse
- .append_inclusions(*inclusions) ⇒ Object deprecated Deprecated.
- .descendants ⇒ Object deprecated Deprecated.
- .extra_inclusions ⇒ Object deprecated Deprecated.
-
.included(model) ⇒ Object
private
Makes sure a class gets all the methods when it includes Resource.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compares two Resources to allow them to be sorted.
-
#==(other) ⇒ Boolean
Compares another Resource for equivalency.
-
#after_create_hook ⇒ undefined
protected
private
Method for hooking callbacks after resource creation.
-
#after_destroy_hook ⇒ undefined
protected
private
Method for hooking callbacks after resource destruction.
-
#after_save_hook ⇒ undefined
protected
private
Method for hooking callbacks after resource saving.
-
#after_update_hook ⇒ undefined
protected
private
Method for hooking callbacks after resource updating.
-
#attribute_dirty?(name) ⇒ Boolean
Checks if an attribute has unsaved changes.
-
#attribute_get(name) ⇒ Object
(also: #[])
Returns the value of the attribute.
-
#attribute_loaded?(name) ⇒ Boolean
private
Checks if an attribute has been loaded from the repository.
-
#attribute_set(name, value) ⇒ undefined
(also: #[]=)
Sets the value of the attribute and marks the attribute as dirty if it has been changed so that it may be saved.
-
#attributes(key_on = :name) ⇒ Hash
Gets all the attributes of the Resource instance.
-
#attributes=(attributes) ⇒ Hash
Assign values to multiple attributes in one call (mass assignment).
-
#before_create_hook ⇒ undefined
protected
private
Method for hooking callbacks before resource creation.
-
#before_destroy_hook ⇒ undefined
protected
private
Method for hooking callbacks before resource destruction.
-
#before_save_hook ⇒ undefined
protected
private
Method for hooking callbacks before resource saving.
-
#before_update_hook ⇒ undefined
protected
private
Method for hooking callbacks before resource updating.
-
#clean? ⇒ Boolean
Checks if the resource has no changes to save.
-
#collection ⇒ nil, Collection
private
Returns the Collection the Resource is associated with.
-
#collection=(collection) ⇒ nil, Collection
private
Associates a Resource to a Collection.
-
#collection_for_self ⇒ Collection
private
Return a collection including the current resource only.
-
#destroy ⇒ Boolean
Destroy the instance, remove it from the repository.
-
#destroy! ⇒ Boolean
Destroy the instance, remove it from the repository, bypassing hooks.
-
#destroyed? ⇒ Boolean
Checks if this Resource instance is destroyed.
-
#dirty? ⇒ Boolean
Checks if the resource has unsaved changes.
-
#dirty_attributes ⇒ Hash
Hash of attributes that have unsaved changes.
-
#eql?(other) ⇒ Boolean
Compares another Resource for equality.
-
#hash ⇒ Object
private
Returns hash value of the object.
-
#inspect ⇒ String
Get a Human-readable representation of this Resource instance.
-
#key ⇒ Array(Key)
Retrieve the key(s) for this resource.
-
#new? ⇒ Boolean
Checks if this Resource instance is new.
-
#original_attributes ⇒ Hash
Hash of original values of attributes that have unsaved changes.
-
#persisted_state ⇒ Resource::State
private
Get the persisted state for the resource.
-
#persisted_state=(state) ⇒ undefined
private
Set the persisted state for the resource.
-
#persisted_state? ⇒ Boolean
private
Test if the persisted state is set.
-
#query ⇒ Query
Returns a Query that will match the resource.
-
#raise_on_save_failure ⇒ Boolean
Return if Resource#save should raise an exception on save failures (per-resource).
-
#raise_on_save_failure=(raise_on_save_failure) ⇒ Boolean
Specify if Resource#save should raise an exception on save failures (per-resource).
-
#readonly? ⇒ Boolean
Checks if this Resource instance is readonly.
-
#reload ⇒ Resource
Reloads association and all child association.
-
#repository ⇒ Repository
Repository this resource belongs to in the context of this collection or of the resource’s class.
-
#save ⇒ Boolean
Save the instance and loaded, dirty associations to the data-store.
-
#save! ⇒ Boolean
Save the instance and loaded, dirty associations to the data-store, bypassing hooks.
-
#saved? ⇒ Boolean
Checks if this Resource instance is saved.
-
#update(attributes) ⇒ Boolean
Updates attributes and saves this Resource instance.
-
#update!(attributes) ⇒ Boolean
Updates attributes and saves this Resource instance, bypassing hooks.
- #update_attributes(attributes = {}, *allowed) ⇒ Object deprecated Deprecated.
Methods included from Chainable
Methods included from Assertions
Class Method Details
.append_inclusions(*inclusions) ⇒ Object
10 11 12 |
# File 'lib/dm-core/resource.rb', line 10 def self.append_inclusions(*inclusions) raise "DataMapper::Resource.append_inclusions is deprecated, use DataMapper::Model.append_inclusions instead (#{caller.first})" end |
.descendants ⇒ Object
20 21 22 |
# File 'lib/dm-core/resource.rb', line 20 def self.descendants raise "DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead (#{caller.first})" end |
.extra_inclusions ⇒ Object
15 16 17 |
# File 'lib/dm-core/resource.rb', line 15 def self.extra_inclusions raise "DataMapper::Resource.extra_inclusions is deprecated, use DataMapper::Model.extra_inclusions instead (#{caller.first})" end |
.included(model) ⇒ Object
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.
Makes sure a class gets all the methods when it includes Resource
Note that including this module into an anonymous class will leave the model descendant tracking mechanism with no possibility to reliably track the anonymous model across code reloads. This means that DescendantSet will currently leak memory in scenarios where anonymous models are reloaded multiple times (as is the case in dm-rails development mode for example).
74 75 76 |
# File 'lib/dm-core/resource.rb', line 74 def self.included(model) model.extend Model end |
Instance Method Details
#<=>(other) ⇒ Integer
Compares two Resources to allow them to be sorted
499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/dm-core/resource.rb', line 499 def <=>(other) model = self.model unless other.kind_of?(model.base_model) raise ArgumentError, "Cannot compare a #{other.class} instance with a #{model} instance" end model.default_order(repository_name).each do |direction| cmp = direction.get(self) <=> direction.get(other) return cmp if cmp.nonzero? end 0 end |
#==(other) ⇒ Boolean
Compares another Resource for equivalency
Resource is equivalent to other
if they are the same object (identical object_id) or all of their attribute are equivalent
482 483 484 485 486 |
# File 'lib/dm-core/resource.rb', line 482 def ==(other) return true if equal?(other) return false unless other.kind_of?(Resource) && model.base_model.equal?(other.model.base_model) cmp?(other, :==) end |
#after_create_hook ⇒ undefined (protected)
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.
Method for hooking callbacks after resource creation
697 698 699 |
# File 'lib/dm-core/resource.rb', line 697 def after_create_hook execute_hooks_for(:after, :create) end |
#after_destroy_hook ⇒ undefined (protected)
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.
Method for hooking callbacks after resource destruction
733 734 735 |
# File 'lib/dm-core/resource.rb', line 733 def after_destroy_hook execute_hooks_for(:after, :destroy) end |
#after_save_hook ⇒ undefined (protected)
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.
Method for hooking callbacks after resource saving
679 680 681 |
# File 'lib/dm-core/resource.rb', line 679 def after_save_hook execute_hooks_for(:after, :save) end |
#after_update_hook ⇒ undefined (protected)
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.
Method for hooking callbacks after resource updating
715 716 717 |
# File 'lib/dm-core/resource.rb', line 715 def after_update_hook execute_hooks_for(:after, :update) end |
#attribute_dirty?(name) ⇒ Boolean
Checks if an attribute has unsaved changes
594 595 596 |
# File 'lib/dm-core/resource.rb', line 594 def attribute_dirty?(name) dirty_attributes.key?(properties[name]) end |
#attribute_get(name) ⇒ Object Also known as: []
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.
242 243 244 |
# File 'lib/dm-core/resource.rb', line 242 def attribute_get(name) persisted_state.get(properties[name]) end |
#attribute_loaded?(name) ⇒ Boolean
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.
Checks if an attribute has been loaded from the repository
581 582 583 |
# File 'lib/dm-core/resource.rb', line 581 def attribute_loaded?(name) properties[name].loaded?(self) end |
#attribute_set(name, value) ⇒ undefined Also known as: []=
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.
283 284 285 |
# File 'lib/dm-core/resource.rb', line 283 def attribute_set(name, value) self.persisted_state = persisted_state.set(properties[name], value) end |
#attributes(key_on = :name) ⇒ Hash
Gets all the attributes of the Resource instance
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/dm-core/resource.rb', line 300 def attributes(key_on = :name) attributes = {} lazy_load(properties) fields.each do |property| if model.public_method_defined?(name = property.name) key = case key_on when :name then name when :field then property.field else property end attributes[key] = __send__(name) end end attributes end |
#attributes=(attributes) ⇒ Hash
Assign values to multiple attributes in one call (mass assignment)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/dm-core/resource.rb', line 328 def attributes=(attributes) model = self.model attributes.each do |name, value| case name when String, Symbol if model.public_method_defined?(setter = "#{name}=") __send__(setter, value) else raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}" end when Associations::Relationship, Property self.persisted_state = persisted_state.set(name, value) end end end |
#before_create_hook ⇒ undefined (protected)
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.
Method for hooking callbacks before resource creation
688 689 690 |
# File 'lib/dm-core/resource.rb', line 688 def before_create_hook execute_hooks_for(:before, :create) end |
#before_destroy_hook ⇒ undefined (protected)
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.
Method for hooking callbacks before resource destruction
724 725 726 |
# File 'lib/dm-core/resource.rb', line 724 def before_destroy_hook execute_hooks_for(:before, :destroy) end |
#before_save_hook ⇒ undefined (protected)
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.
Method for hooking callbacks before resource saving
670 671 672 |
# File 'lib/dm-core/resource.rb', line 670 def before_save_hook execute_hooks_for(:before, :save) end |
#before_update_hook ⇒ undefined (protected)
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.
Method for hooking callbacks before resource updating
706 707 708 |
# File 'lib/dm-core/resource.rb', line 706 def before_update_hook execute_hooks_for(:before, :update) end |
#clean? ⇒ Boolean
Checks if the resource has no changes to save
185 186 187 |
# File 'lib/dm-core/resource.rb', line 185 def clean? persisted_state.kind_of?(State::Clean) || persisted_state.kind_of?(State::Immutable) end |
#collection ⇒ nil, Collection
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.
Returns the Collection the Resource is associated with
623 624 625 626 |
# File 'lib/dm-core/resource.rb', line 623 def collection return @_collection if @_collection || new? || readonly? collection_for_self end |
#collection=(collection) ⇒ nil, Collection
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.
Associates a Resource to a Collection
639 640 641 |
# File 'lib/dm-core/resource.rb', line 639 def collection=(collection) @_collection = collection end |
#collection_for_self ⇒ Collection
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.
Return a collection including the current resource only
649 650 651 |
# File 'lib/dm-core/resource.rb', line 649 def collection_for_self Collection.new(query, [ self ]) end |
#destroy ⇒ Boolean
Destroy the instance, remove it from the repository
430 431 432 433 434 435 436 437 438 |
# File 'lib/dm-core/resource.rb', line 430 def destroy return true if destroyed? catch :halt do before_destroy_hook _destroy after_destroy_hook end destroyed? end |
#destroy! ⇒ Boolean
Destroy the instance, remove it from the repository, bypassing hooks
446 447 448 449 450 |
# File 'lib/dm-core/resource.rb', line 446 def destroy! return true if destroyed? _destroy(false) destroyed? end |
#destroyed? ⇒ Boolean
Checks if this Resource instance is destroyed
175 176 177 |
# File 'lib/dm-core/resource.rb', line 175 def destroyed? readonly? && !key.nil? end |
#dirty? ⇒ Boolean
Checks if the resource has unsaved changes
195 196 197 198 199 |
# File 'lib/dm-core/resource.rb', line 195 def dirty? run_once(true) do dirty_self? || dirty_parents? || dirty_children? end end |
#dirty_attributes ⇒ Hash
Hash of attributes that have unsaved changes
604 605 606 607 608 609 610 611 612 613 |
# File 'lib/dm-core/resource.rb', line 604 def dirty_attributes dirty_attributes = {} original_attributes.each_key do |property| next unless property.respond_to?(:dump) dirty_attributes[property] = property.dump(property.get!(self)) end dirty_attributes end |
#eql?(other) ⇒ Boolean
Compares another Resource for equality
Resource is equal to other
if they are the same object (identical object_id) or if they are both of the *same model* and all of their attributes are equivalent
465 466 467 468 |
# File 'lib/dm-core/resource.rb', line 465 def eql?(other) return true if equal?(other) instance_of?(other.class) && cmp?(other, :eql?) end |
#hash ⇒ Object
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.
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
521 522 523 |
# File 'lib/dm-core/resource.rb', line 521 def hash model.hash ^ key.hash end |
#inspect ⇒ String
Get a Human-readable representation of this Resource instance
Foo.new #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>
533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/dm-core/resource.rb', line 533 def inspect # TODO: display relationship values attrs = properties.map do |property| value = if new? || property.loaded?(self) property.get!(self).inspect else '<not loaded>' end "#{property.instance_variable_name}=#{value}" end "#<#{model.name} #{attrs.join(' ')}>" end |
#key ⇒ Array(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.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/dm-core/resource.rb', line 136 def key return @_key if defined?(@_key) model_key = model.key(repository_name) key = model_key.map do |property| original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil) end # only memoize a valid key @_key = key if model_key.valid?(key) end |
#new? ⇒ Boolean
Checks if this Resource instance is new
155 156 157 |
# File 'lib/dm-core/resource.rb', line 155 def new? persisted_state.kind_of?(State::Transient) end |
#original_attributes ⇒ Hash
Hash of original values of attributes that have unsaved changes
554 555 556 557 558 559 560 |
# File 'lib/dm-core/resource.rb', line 554 def original_attributes if persisted_state.respond_to?(:original_attributes) persisted_state.original_attributes.dup.freeze else {}.freeze end end |
#persisted_state ⇒ Resource::State
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.
Get the persisted state for the resource
87 88 89 |
# File 'lib/dm-core/resource.rb', line 87 def persisted_state @_state ||= Resource::State::Transient.new(self) end |
#persisted_state=(state) ⇒ undefined
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.
Set the persisted state for the resource
99 100 101 |
# File 'lib/dm-core/resource.rb', line 99 def persisted_state=(state) @_state = state end |
#persisted_state? ⇒ Boolean
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.
Test if the persisted state is set
109 110 111 |
# File 'lib/dm-core/resource.rb', line 109 def persisted_state? defined?(@_state) ? true : false end |
#query ⇒ Query
Returns a Query that will match the resource
659 660 661 |
# File 'lib/dm-core/resource.rb', line 659 def query repository.new_query(model, :fields => fields, :conditions => conditions) end |
#raise_on_save_failure ⇒ Boolean
Return if Resource#save should raise an exception on save failures (per-resource)
This delegates to model.raise_on_save_failure by default.
user.raise_on_save_failure # => false
34 35 36 37 38 39 40 |
# File 'lib/dm-core/resource.rb', line 34 def raise_on_save_failure if defined?(@raise_on_save_failure) @raise_on_save_failure else model.raise_on_save_failure end end |
#raise_on_save_failure=(raise_on_save_failure) ⇒ Boolean
Specify if Resource#save should raise an exception on save failures (per-resource)
51 52 53 |
# File 'lib/dm-core/resource.rb', line 51 def raise_on_save_failure=(raise_on_save_failure) @raise_on_save_failure = raise_on_save_failure end |
#readonly? ⇒ Boolean
Checks if this Resource instance is readonly
207 208 209 |
# File 'lib/dm-core/resource.rb', line 207 def readonly? persisted_state.kind_of?(State::Immutable) end |
#reload ⇒ Resource
Reloads association and all child association
This is accomplished by resetting the Resource key to it’s original value, and then removing all the ivars for properties and relationships. On the next access of those ivars, the resource will eager load what it needs. While this is more of a lazy reload, it should result in more consistent behavior since no cached results will remain from the initial load.
357 358 359 360 361 362 363 364 365 366 |
# File 'lib/dm-core/resource.rb', line 357 def reload if key reset_key clear_subjects end self.persisted_state = persisted_state.rollback self end |
#repository ⇒ Repository
Repository this resource belongs to in the context of this collection or of the resource’s class.
121 122 123 124 |
# File 'lib/dm-core/resource.rb', line 121 def repository # only set @_repository explicitly when persisted defined?(@_repository) ? @_repository : model.repository end |
#save ⇒ Boolean
Save the instance and loaded, dirty associations to the data-store
404 405 406 407 408 409 |
# File 'lib/dm-core/resource.rb', line 404 def save assert_not_destroyed(:save) retval = _save assert_save_successful(:save, retval) retval end |
#save! ⇒ Boolean
Save the instance and loaded, dirty associations to the data-store, bypassing hooks
417 418 419 420 421 422 |
# File 'lib/dm-core/resource.rb', line 417 def save! assert_not_destroyed(:save!) retval = _save(false) assert_save_successful(:save!, retval) retval end |
#saved? ⇒ Boolean
Checks if this Resource instance is saved
165 166 167 |
# File 'lib/dm-core/resource.rb', line 165 def saved? persisted_state.kind_of?(State::Persisted) end |
#update(attributes) ⇒ Boolean
Updates attributes and saves this Resource instance
377 378 379 380 381 |
# File 'lib/dm-core/resource.rb', line 377 def update(attributes) assert_update_clean_only(:update) self.attributes = attributes save end |
#update!(attributes) ⇒ Boolean
Updates attributes and saves this Resource instance, bypassing hooks
392 393 394 395 396 |
# File 'lib/dm-core/resource.rb', line 392 def update!(attributes) assert_update_clean_only(:update!) self.attributes = attributes save! end |
#update_attributes(attributes = {}, *allowed) ⇒ Object
Deprecated API for updating attributes and saving Resource
60 61 62 |
# File 'lib/dm-core/resource.rb', line 60 def update_attributes(attributes = {}, *allowed) raise "#{model}#update_attributes is deprecated, use #{model}#update instead (#{caller.first})" end |