class Datamappify::Data::Criteria::Common

Provides a set of useful methods for common criteria tasks, all Criteria objects inherit from Common

Attributes

attributes[R]

@return [Set<Mapper::Attribute>]

attributes_and_values[RW]

@return [Hash]

criteria[RW]

@return [void]

entity[R]

@return [Entity]

options[R]

@return [Hash]

source_class[R]

@return [Class]

Public Class Methods

new(source_class, *args, &block) click to toggle source

@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

perform_with_callbacks() click to toggle source

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

any_attribute() click to toggle source

@return [Attribute]

# File lib/datamappify/data/criteria/common.rb, line 127
def any_attribute
  @any_attribute ||= attributes.first
end
default_source_class_name() click to toggle source

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
ignore?() click to toggle source

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() click to toggle source

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
key_value() click to toggle source

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
new_record?() click to toggle source

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
pk() click to toggle source

@return [Attribute]

# File lib/datamappify/data/criteria/common.rb, line 132
def pk
  @pk ||= attributes.find(&:primary_key?)
end
primary_record?() click to toggle source

Determines whether or not it’s a primary record by comparing the source class and the entity class

@return [Boolean]

# File lib/datamappify/data/criteria/common.rb, line 99
def primary_record?
  source_class_name == default_source_class_name
end
source_class_name() click to toggle source

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
store_attribute_value() click to toggle source

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

ignore_attribute?(attribute) click to toggle source

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