@deprecated
# File lib/dm-core/resource.rb, line 6 6: def self.append_inclusions(*inclusions) 7: raise "DataMapper::Resource.append_inclusions is deprecated, use DataMapper::Model.append_inclusions instead (#{caller.first})" 8: end
@deprecated
# File lib/dm-core/resource.rb, line 16 16: def self.descendants 17: raise "DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead (#{caller.first})" 18: end
@deprecated
# File lib/dm-core/resource.rb, line 11 11: def self.extra_inclusions 12: raise "DataMapper::Resource.extra_inclusions is deprecated, use DataMapper::Model.extra_inclusions instead (#{caller.first})" 13: end
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 {DataMapper::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).
@api private
# File lib/dm-core/resource.rb, line 70 70: def self.included(model) 71: model.extend Model 72: end
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
# File lib/dm-core/resource.rb, line 498 498: def <=>(other) 499: model = self.model 500: unless other.kind_of?(model.base_model) 501: raise ArgumentError, "Cannot compare a #{other.class} instance with a #{model} instance" 502: end 503: model.default_order(repository_name).each do |direction| 504: cmp = direction.get(self) <=> direction.get(other) 505: return cmp if cmp.nonzero? 506: end 507: 0 508: end
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
@param [Resource] other
the other Resource to compare with
@return [Boolean]
true if they are equivalent, false if not
@api public
# File lib/dm-core/resource.rb, line 481 481: def ==(other) 482: return true if equal?(other) 483: return false unless other.kind_of?(Resource) && model.base_model.equal?(other.model.base_model) 484: cmp?(other, :==) 485: end
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
# File lib/dm-core/resource.rb, line 593 593: def attribute_dirty?(name) 594: dirty_attributes.key?(properties[name]) 595: end
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
# File lib/dm-core/resource.rb, line 239 239: def attribute_get(name) 240: property = properties[name] 241: persistence_state.get(property) if property 242: end
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
# File lib/dm-core/resource.rb, line 580 580: def attribute_loaded?(name) 581: properties[name].loaded?(self) 582: end
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 [undefined]
@api public
# File lib/dm-core/resource.rb, line 281 281: def attribute_set(name, value) 282: property = properties[name] 283: self.persistence_state = persistence_state.set(property, value) if property 284: end
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
# File lib/dm-core/resource.rb, line 299 299: def attributes(key_on = :name) 300: attributes = {} 301: 302: lazy_load(properties) 303: fields.each do |property| 304: if model.public_method_defined?(name = property.name) 305: key = case key_on 306: when :name then name 307: when :field then property.field 308: else property 309: end 310: 311: attributes[key] = __send__(name) 312: end 313: end 314: 315: attributes 316: end
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
# File lib/dm-core/resource.rb, line 327 327: def attributes=(attributes) 328: model = self.model 329: attributes.each do |name, value| 330: case name 331: when String, Symbol 332: if model.allowed_writer_methods.include?(setter = "#{name}=") 333: __send__(setter, value) 334: else 335: raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}" 336: end 337: when Associations::Relationship, Property 338: self.persistence_state = persistence_state.set(name, value) 339: end 340: end 341: end
Checks if the resource has no changes to save
@return [Boolean]
true if the resource may not be persisted
@api public
# File lib/dm-core/resource.rb, line 181 181: def clean? 182: persistence_state.kind_of?(PersistenceState::Clean) || 183: persistence_state.kind_of?(PersistenceState::Immutable) 184: end
Returns the Collection the Resource is associated with
@return [nil]
nil if this is a new record
@return [Collection]
a Collection that self belongs to
@api private
# File lib/dm-core/resource.rb, line 622 622: def collection 623: return @_collection if @_collection || new? || readonly? 624: collection_for_self 625: end
Associates a Resource to a Collection
@param [Collection, nil] collection
the collection to associate the resource with
@return [nil]
nil if this is a new record
@return [Collection]
a Collection that self belongs to
@api private
# File lib/dm-core/resource.rb, line 638 638: def collection=(collection) 639: @_collection = collection 640: end
Return a collection including the current resource only
@return [Collection]
a collection containing self
@api private
# File lib/dm-core/resource.rb, line 648 648: def collection_for_self 649: Collection.new(query, [ self ]) 650: end
Destroy the instance, remove it from the repository
@return [Boolean]
true if resource was destroyed
@api public
# File lib/dm-core/resource.rb, line 429 429: def destroy 430: return true if destroyed? 431: catch :halt do 432: before_destroy_hook 433: _destroy 434: after_destroy_hook 435: end 436: destroyed? 437: end
Destroy the instance, remove it from the repository, bypassing hooks
@return [Boolean]
true if resource was destroyed
@api public
# File lib/dm-core/resource.rb, line 445 445: def destroy! 446: return true if destroyed? 447: _destroy(false) 448: destroyed? 449: end
Checks if this Resource instance is destroyed
@return [Boolean]
true if the resource has been destroyed
@api public
# File lib/dm-core/resource.rb, line 171 171: def destroyed? 172: readonly? && !key.nil? 173: end
Checks if the resource has unsaved changes
@return [Boolean]
true if resource may be persisted
@api public
# File lib/dm-core/resource.rb, line 192 192: def dirty? 193: run_once(true) do 194: dirty_self? || dirty_parents? || dirty_children? 195: end 196: end
Hash of attributes that have unsaved changes
@return [Hash]
attributes that have unsaved changes
@api semipublic
# File lib/dm-core/resource.rb, line 603 603: def dirty_attributes 604: dirty_attributes = {} 605: 606: original_attributes.each_key do |property| 607: next unless property.respond_to?(:dump) 608: dirty_attributes[property] = property.dump(property.get!(self)) 609: end 610: 611: dirty_attributes 612: end
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
@param [Resource] other
the other Resource to compare with
@return [Boolean]
true if they are equal, false if not
@api public
# File lib/dm-core/resource.rb, line 464 464: def eql?(other) 465: return true if equal?(other) 466: instance_of?(other.class) && cmp?(other, :eql?) 467: end
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
# File lib/dm-core/resource.rb, line 520 520: def hash 521: model.hash ^ key.hash 522: end
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
# File lib/dm-core/resource.rb, line 532 532: def inspect 533: # TODO: display relationship values 534: attrs = properties.map do |property| 535: value = if new? || property.loaded?(self) 536: property.get!(self).inspect 537: else 538: '<not loaded>' 539: end 540: 541: "#{property.instance_variable_name}=#{value}" 542: end 543: 544: "#<#{model.name} #{attrs.join(' ')}>" 545: end
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
# File lib/dm-core/resource.rb, line 132 132: def key 133: return @_key if defined?(@_key) 134: 135: model_key = model.key(repository_name) 136: 137: key = model_key.map do |property| 138: original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil) 139: end 140: 141: # only memoize a valid key 142: @_key = key if model_key.valid?(key) 143: end
Checks if this Resource instance is new
@return [Boolean]
true if the resource is new and not saved
@api public
# File lib/dm-core/resource.rb, line 151 151: def new? 152: persistence_state.kind_of?(PersistenceState::Transient) 153: end
Hash of original values of attributes that have unsaved changes
@return [Hash]
original values of attributes that have unsaved changes
@api semipublic
# File lib/dm-core/resource.rb, line 553 553: def original_attributes 554: if persistence_state.respond_to?(:original_attributes) 555: persistence_state.original_attributes.dup.freeze 556: else 557: {}.freeze 558: end 559: end
Get the persisted state for the resource
@return [Resource::PersistenceState]
the current persisted state for the resource
@api private
# File lib/dm-core/resource.rb, line 83 83: def persistence_state 84: @_persistence_state ||= Resource::PersistenceState::Transient.new(self) 85: end
Set the persisted state for the resource
@param [Resource::PersistenceState]
the new persisted state for the resource
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 95 95: def persistence_state=(state) 96: @_persistence_state = state 97: end
Test if the persisted state is set
@return [Boolean]
true if the persisted state is set
@api private
# File lib/dm-core/resource.rb, line 105 105: def persistence_state? 106: defined?(@_persistence_state) ? true : false 107: end
Returns a Query that will match the resource
@return [Query]
Query that will match the resource
@api semipublic
# File lib/dm-core/resource.rb, line 658 658: def query 659: repository.new_query(model, :fields => fields, :conditions => conditions) 660: end
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
@return [Boolean]
true if a failure in Resource#save should raise an exception
@api public
# File lib/dm-core/resource.rb, line 30 30: def raise_on_save_failure 31: if defined?(@raise_on_save_failure) 32: @raise_on_save_failure 33: else 34: model.raise_on_save_failure 35: end 36: end
Specify if Resource#save should raise an exception on save failures (per-resource)
@param [Boolean]
a boolean that if true will cause Resource#save to raise an exception
@return [Boolean]
true if a failure in Resource#save should raise an exception
@api public
# File lib/dm-core/resource.rb, line 47 47: def raise_on_save_failure=(raise_on_save_failure) 48: @raise_on_save_failure = raise_on_save_failure 49: end
Checks if this Resource instance is readonly
@return [Boolean]
true if the resource cannot be persisted
@api public
# File lib/dm-core/resource.rb, line 204 204: def readonly? 205: persistence_state.kind_of?(PersistenceState::Immutable) 206: end
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.
@return [Resource]
the receiver, the current Resource instance
@api public
# File lib/dm-core/resource.rb, line 356 356: def reload 357: if key 358: reset_key 359: clear_subjects 360: end 361: 362: self.persistence_state = persistence_state.rollback 363: 364: self 365: end
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
# File lib/dm-core/resource.rb, line 117 117: def repository 118: # only set @_repository explicitly when persisted 119: defined?(@_repository) ? @_repository : model.repository 120: end
Save the instance and loaded, dirty associations to the data-store
@return [Boolean]
true if Resource instance and all associations were saved
@api public
# File lib/dm-core/resource.rb, line 403 403: def save 404: assert_not_destroyed(:save) 405: retval = _save 406: assert_save_successful(:save, retval) 407: retval 408: end
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
# File lib/dm-core/resource.rb, line 416 416: def save! 417: assert_not_destroyed(:save!) 418: retval = _save(false) 419: assert_save_successful(:save!, retval) 420: retval 421: end
Checks if this Resource instance is saved
@return [Boolean]
true if the resource has been saved
@api public
# File lib/dm-core/resource.rb, line 161 161: def saved? 162: persistence_state.kind_of?(PersistenceState::Persisted) 163: end
Updates attributes and saves this Resource instance
@param [Hash] attributes
attributes to be updated
@return [Boolean]
true if resource and storage state match
@api public
# File lib/dm-core/resource.rb, line 376 376: def update(attributes) 377: assert_update_clean_only(:update) 378: self.attributes = attributes 379: save 380: end
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
# File lib/dm-core/resource.rb, line 391 391: def update!(attributes) 392: assert_update_clean_only(:update!) 393: self.attributes = attributes 394: save! 395: end
Method for hooking callbacks after resource creation
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 696 696: def after_create_hook 697: execute_hooks_for(:after, :create) 698: end
Method for hooking callbacks after resource destruction
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 732 732: def after_destroy_hook 733: execute_hooks_for(:after, :destroy) 734: end
Method for hooking callbacks after resource saving
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 678 678: def after_save_hook 679: execute_hooks_for(:after, :save) 680: end
Method for hooking callbacks after resource updating
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 714 714: def after_update_hook 715: execute_hooks_for(:after, :update) 716: end
Method for hooking callbacks before resource creation
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 687 687: def before_create_hook 688: execute_hooks_for(:before, :create) 689: end
Method for hooking callbacks before resource destruction
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 723 723: def before_destroy_hook 724: execute_hooks_for(:before, :destroy) 725: end
Destroy the resource
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 998 998: def _destroy(execute_hooks = true) 999: self.persistence_state = persistence_state.delete 1000: _persist 1001: end
Commit the persisted state
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 955 955: def _persist 956: self.persistence_state = persistence_state.commit 957: end
@api private
# File lib/dm-core/resource.rb, line 1004 1004: def _save(execute_hooks = true) 1005: run_once(true) do 1006: save_parents(execute_hooks) && save_self(execute_hooks) && save_children(execute_hooks) 1007: end 1008: end
@api private
# File lib/dm-core/resource.rb, line 802 802: def add_to_identity_map 803: identity_map[key] = self 804: end
Raises an exception if # is performed on a destroyed resource
@param [Symbol] method
the name of the method to use in the exception
@return [undefined]
@raise [PersistenceError]
raise if the resource is destroyed
@api private
# File lib/dm-core/resource.rb, line 1175 1175: def assert_not_destroyed(method) 1176: if destroyed? 1177: raise PersistenceError, "#{model}##{method} cannot be called on a destroyed resource" 1178: end 1179: end
Raises an exception if # returns false
@param [Symbol] method
the name of the method to use in the exception
@param [Boolean] save_result
the result of the #save call
@return [undefined]
@raise [SaveFailureError]
raise if the resource was not saved
@api private
# File lib/dm-core/resource.rb, line 1194 1194: def assert_save_successful(method, save_retval) 1195: if save_retval != true && raise_on_save_failure 1196: raise SaveFailureError.new("#{model}##{method} returned #{save_retval.inspect}, #{model} was not saved", self) 1197: end 1198: end
Raises an exception if # is performed on a dirty resource
@param [Symbol] method
the name of the method to use in the exception
@return [undefined]
@raise [UpdateConflictError]
raise if the resource is dirty
@api private
# File lib/dm-core/resource.rb, line 1158 1158: def assert_update_clean_only(method) 1159: if dirty? 1160: raise UpdateConflictError, "#{model}##{method} cannot be called on a #{new? ? 'new' : 'dirty'} resource" 1161: end 1162: end
@api private
# File lib/dm-core/resource.rb, line 946 946: def child_associations 947: child_relationships.map { |relationship| relationship.get_collection(self) } 948: end
Returns loaded child relationships
@return [Array
array of child relationships for which this resource is parent and is loaded
@api private
# File lib/dm-core/resource.rb, line 922 922: def child_relationships 923: child_relationships = [] 924: 925: relationships.each do |relationship| 926: next unless relationship.respond_to?(:collection_for) 927: set_default_value(relationship) 928: next unless relationship.loaded?(self) 929: 930: child_relationships << relationship 931: end 932: 933: many_to_many, other = child_relationships.partition do |relationship| 934: relationship.kind_of?(Associations::ManyToMany::Relationship) 935: end 936: 937: many_to_many + other 938: end
Remove all the ivars for properties and relationships
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 840 840: def clear_subjects 841: model_properties = properties 842: 843: (model_properties - model_properties.key | relationships).each do |subject| 844: next unless subject.loaded?(self) 845: remove_instance_variable(subject.instance_variable_name) 846: end 847: end
Return true if other’s is equivalent or equal to self’s
@param [Resource] other
The Resource whose attributes are to be compared with +self+'s
@param [Symbol] operator
The comparison operator to use to compare the attributes
@return [Boolean]
The result of the comparison of +other+'s attributes with +self+'s
@api private
# File lib/dm-core/resource.rb, line 1112 1112: def cmp?(other, operator) 1113: return false unless repository.send(operator, other.repository) && 1114: key.send(operator, other.key) 1115: 1116: if saved? && other.saved? 1117: # if dirty attributes match then they are the same resource 1118: dirty_attributes == other.dirty_attributes 1119: else 1120: # compare properties for unsaved resources 1121: properties.all? do |property| 1122: __send__(property.name).send(operator, other.__send__(property.name)) 1123: end 1124: end 1125: end
Return conditions to match the Resource
@return [Hash]
query conditions
@api private
# File lib/dm-core/resource.rb, line 887 887: def conditions 888: key = self.key 889: if key 890: model.key_conditions(repository, key) 891: else 892: conditions = {} 893: properties.each do |property| 894: next unless property.loaded?(self) 895: conditions[property] = property.get!(self) 896: end 897: conditions 898: end 899: end
This method executes the hooks before and after resource creation
@return [Boolean]
@see Resource#_create
@api private
# File lib/dm-core/resource.rb, line 966 966: def create_with_hooks 967: catch :halt do 968: before_save_hook 969: before_create_hook 970: _persist 971: after_create_hook 972: after_save_hook 973: end 974: end
Checks if the children have unsaved changes
@param [Hash] resources
resources that have already been tested
@return [Boolean]
true if the children have unsaved changes
@api private
# File lib/dm-core/resource.rb, line 1097 1097: def dirty_children? 1098: child_associations.any? { |association| association.dirty? } 1099: end
Checks if the parents have unsaved changes
@return [Boolean]
true if the parents have unsaved changes
@api private
# File lib/dm-core/resource.rb, line 1080 1080: def dirty_parents? 1081: run_once(false) do 1082: parent_associations.any? do |association| 1083: association.__send__(:dirty_self?) || association.__send__(:dirty_parents?) 1084: end 1085: end 1086: end
Checks if the resource has unsaved changes
@return [Boolean]
true if the resource has unsaved changes
@api semipublic
# File lib/dm-core/resource.rb, line 1064 1064: def dirty_self? 1065: if original_attributes.any? 1066: true 1067: elsif new? 1068: !model.serial.nil? || properties.any? { |property| property.default? } 1069: else 1070: false 1071: end 1072: end
Reloads specified attributes
@param [Array
the properties to reload
@return [Resource]
the receiver, the current Resource instance
@api private
# File lib/dm-core/resource.rb, line 870 870: def eager_load(properties) 871: unless properties.empty? || key.nil? || collection.nil? 872: # set an initial value to prevent recursive lazy loads 873: properties.each { |property| property.set!(self, nil) } 874: 875: collection.reload(:fields => properties) 876: end 877: 878: self 879: end
Execute all the queued up hooks for a given type and name
@param [Symbol] type
the type of hook to execute (before or after)
@param [Symbol] name
the name of the hook to execute
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 1143 1143: def execute_hooks_for(type, name) 1144: model.hooks[name][type].each { |hook| hook.call(self) } 1145: end
Fetches all the names of the attributes that have been loaded, even if they are lazy but have been called
@return [Array
names of attributes that have been loaded
@api private
# File lib/dm-core/resource.rb, line 818 818: def fields 819: properties.select do |property| 820: property.loaded?(self) || (new? && property.default?) 821: end 822: end
Returns the identity map for the model from the repository
@return [IdentityMap]
identity map of repository this object was loaded from
@api private
# File lib/dm-core/resource.rb, line 797 797: def identity_map 798: repository.identity_map(model) 799: end
@api private
# File lib/dm-core/resource.rb, line 752 752: def initialize_copy(original) 753: instance_variables.each do |ivar| 754: instance_variable_set(ivar, DataMapper::Ext.try_dup(instance_variable_get(ivar))) 755: end 756: 757: self.persistence_state = persistence_state.class.new(self) 758: end
Lazy loads attributes not yet loaded
@param [Array
the properties to reload
@return [self]
@api private
# File lib/dm-core/resource.rb, line 857 857: def lazy_load(properties) 858: eager_load(properties - fields) 859: end
@api private
# File lib/dm-core/resource.rb, line 941 941: def parent_associations 942: parent_relationships.map { |relationship| relationship.get!(self) } 943: end
@api private
# File lib/dm-core/resource.rb, line 902 902: def parent_relationships 903: parent_relationships = [] 904: 905: relationships.each do |relationship| 906: next unless relationship.respond_to?(:resource_for) 907: set_default_value(relationship) 908: next unless relationship.loaded?(self) && relationship.get!(self) 909: 910: parent_relationships << relationship 911: end 912: 913: parent_relationships 914: end
Gets this instance’s Model’s properties
@return [PropertySet]
List of this Resource's Model's properties
@api private
# File lib/dm-core/resource.rb, line 777 777: def properties 778: model.properties(repository_name) 779: end
Gets this instance’s Model’s relationships
@return [RelationshipSet]
List of this instance's Model's Relationships
@api private
# File lib/dm-core/resource.rb, line 787 787: def relationships 788: model.relationships(repository_name) 789: end
@api private
# File lib/dm-core/resource.rb, line 807 807: def remove_from_identity_map 808: identity_map.delete(key) 809: end
Returns name of the repository this object was loaded from
@return [String]
name of the repository this object was loaded from
@api private
# File lib/dm-core/resource.rb, line 767 767: def repository_name 768: repository.name 769: end
Reset the key to the original value
@return [undefined]
@api private
# File lib/dm-core/resource.rb, line 829 829: def reset_key 830: properties.key.zip(key) do |property, value| 831: property.set!(self, value) 832: end 833: end
Prevent a method from being in the stack more than once
The purpose of this method is to prevent SystemStackError from being thrown from methods from encountering infinite recursion when called on resources having circular dependencies.
@param [Object] default
default return value
@yield The block of code to run once
@return [Object]
block return value
@api private
# File lib/dm-core/resource.rb, line 1215 1215: def run_once(default) 1216: caller_method = Kernel.caller(1).first[/`([^'?!]+)[?!]?'/, 1] 1217: sentinel = "@_#{caller_method}_sentinel" 1218: return instance_variable_get(sentinel) if instance_variable_defined?(sentinel) 1219: 1220: begin 1221: instance_variable_set(sentinel, default) 1222: yield 1223: ensure 1224: remove_instance_variable(sentinel) 1225: end 1226: end
Saves the children resources
@return [Boolean]
true if the children were successfully saved
@api private
# File lib/dm-core/resource.rb, line 1052 1052: def save_children(execute_hooks) 1053: child_associations.map do |association| 1054: association.__send__(execute_hooks ? :save : :save!) 1055: end.all? 1056: end
Saves the parent resources
@return [Boolean]
true if the parents were successfully saved
@api private
# File lib/dm-core/resource.rb, line 1034 1034: def save_parents(execute_hooks) 1035: run_once(true) do 1036: parent_relationships.map do |relationship| 1037: parent = relationship.get(self) 1038: 1039: if parent.__send__(:save_parents, execute_hooks) && parent.__send__(:save_self, execute_hooks) 1040: relationship.set(self, parent) # set the FK values 1041: end 1042: end.all? 1043: end 1044: end
Saves the resource
@return [Boolean]
true if the resource was successfully saved
@api semipublic
# File lib/dm-core/resource.rb, line 1016 1016: def save_self(execute_hooks = true) 1017: # short-circuit if the resource is not dirty 1018: return saved? unless dirty_self? 1019: 1020: if execute_hooks 1021: new? ? create_with_hooks : update_with_hooks 1022: else 1023: _persist 1024: end 1025: clean? 1026: end
@api private
# File lib/dm-core/resource.rb, line 1128 1128: def set_default_value(subject) 1129: return unless persistence_state.respond_to?(:set_default_value, true) 1130: persistence_state.__send__(:set_default_value, subject) 1131: end
This method executes the hooks before and after resource updating
@return [Boolean]
@see Resource#_update
@api private
# File lib/dm-core/resource.rb, line 983 983: def update_with_hooks 984: catch :halt do 985: before_save_hook 986: before_update_hook 987: _persist 988: after_update_hook 989: after_save_hook 990: end 991: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.