# File lib/datamappify/data/criteria/common.rb, line 99 def primary_record? source_class_name == default_source_class_name end
class Datamappify::Data::Criteria::Common
Provides a set of useful methods for common criteria tasks, all Criteria
objects inherit from Common
Attributes
@return [Set<Mapper::Attribute>]
@return [Hash]
@return [void]
@return [Entity]
@return [Hash]
@return [Class]
Public Class Methods
@param source_class
[Class]
@param args [any]
@yield
an optional block
# File lib/datamappify/data/criteria/common.rb, line 31 def initialize(source_class, *args, &block) @source_class = source_class @entity, @criteria, @attributes, @options = *args @block = block end
Public Instance Methods
Performs the action (defined by child method classes) with callbacks
@return [void]
# File lib/datamappify/data/criteria/common.rb, line 40 def perform_with_callbacks result = perform store_attribute_value if attributes result end
Protected Instance Methods
@return [Attribute]
# File lib/datamappify/data/criteria/common.rb, line 127 def any_attribute @any_attribute ||= attributes.first end
Name of the default source class, e.g. +“User”+, it is determined from either the PK or the entity
@return [String]
# File lib/datamappify/data/criteria/common.rb, line 71 def default_source_class_name @default_source_class_name ||= pk ? pk.source_class_name : entity.class.name.demodulize end
Ignores the current Criteria’s operation if there is no dirty attributes
@return [Boolean]
# File lib/datamappify/data/criteria/common.rb, line 113 def ignore? attributes_and_values.empty? end
Key name of either the primary key (e.g. id
) or foreign key (e.g. user_id
)
@return [Symbol]
# File lib/datamappify/data/criteria/common.rb, line 78 def key_name primary_record? ? :id : any_attribute.primary_reference_key end
The value of {#key_name}
@return [void]
# File lib/datamappify/data/criteria/common.rb, line 85 def key_value criteria.with_indifferent_access[key_name] end
Determines whether or not it’s going to be a new record by looking at the {#key_value}
@return [Boolean]
# File lib/datamappify/data/criteria/common.rb, line 92 def new_record? key_value.nil? end
@return [Attribute]
# File lib/datamappify/data/criteria/common.rb, line 132 def pk @pk ||= attributes.find(&:primary_key?) end
Determines whether or not it’s a primary record by comparing the source class and the entity class
@return [Boolean]
Source class name with its namespace but without Data::Record::Provider
@return [String]
# File lib/datamappify/data/criteria/common.rb, line 106 def source_class_name source_class.name.split('Datamappify::Data::Record::')[-1].split('::', 2)[-1] end
Stores the attribute value in {Mapper::Attribute} for later use
@return [void]
# File lib/datamappify/data/criteria/common.rb, line 120 def store_attribute_value attributes.each do |attribute| attribute.value = entity.instance_variable_get("@#{attribute.name}") end end
Private Instance Methods
Ignores the attribute if it isn’t dirty or if it’s a primary key
@todo implement proper dirty attribute tracking
@return [Boolean]
# File lib/datamappify/data/criteria/common.rb, line 143 def ignore_attribute?(attribute) entity.send(attribute.name).nil? || attribute.primary_key? end