Class: Datamappify::Data::Criteria::Common

Inherits:
Object
  • Object
show all
Defined in:
lib/datamappify/data/criteria/common.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_class, *args) { ... } ⇒ Common

Returns a new instance of Common.

Parameters:

  • source_class (Class)
  • args (any)

Yields:

  • an optional block



25
26
27
28
29
# File 'lib/datamappify/data/criteria/common.rb', line 25

def initialize(source_class, *args, &block)
  @source_class = source_class
  @entity, @criteria, @attributes = *args
  @block = block
end

Instance Attribute Details

#attributesSet<Mapper::Attribute> (readonly)

Returns:



17
18
19
# File 'lib/datamappify/data/criteria/common.rb', line 17

def attributes
  @attributes
end

#criteriavoid (readonly)

This method returns an undefined value.



14
15
16
# File 'lib/datamappify/data/criteria/common.rb', line 14

def criteria
  @criteria
end

#entityEntity (readonly)

Returns:



11
12
13
# File 'lib/datamappify/data/criteria/common.rb', line 11

def entity
  @entity
end

#source_classClass (readonly)

Returns:

  • (Class)


8
9
10
# File 'lib/datamappify/data/criteria/common.rb', line 8

def source_class
  @source_class
end

Instance Method Details

#attributes_and_valuesHash (protected)

Attributes with their corresponding values

Returns:

  • (Hash)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datamappify/data/criteria/common.rb', line 82

def attributes_and_values
  hash = {}

  attributes.each do |attribute|
    unless ignore_attribute?(attribute)
      hash[attribute.source_attribute_name] = entity.send(attribute.name)
    end
  end

  hash
end

#ignore?Boolean (protected)

Ignores the current Criteria’s operation if there is no dirty attributes

Returns:

  • (Boolean)


75
76
77
# File 'lib/datamappify/data/criteria/common.rb', line 75

def ignore?
  attributes_and_values.empty?
end

#ignore_attribute?(attribute) ⇒ Boolean (private)

TODO:

implement proper dirty attribute tracking

Ignores the attribute if it isn’t dirty or if it’s a primary key

Returns:

  • (Boolean)


110
111
112
# File 'lib/datamappify/data/criteria/common.rb', line 110

def ignore_attribute?(attribute)
  entity.send(attribute.name).nil? || attribute.primary_key?
end

#key_nameSymbol (protected)

Key name of either the primary key (e.g. id) or foreign key (e.g. user_id)

Returns:

  • (Symbol)


47
48
49
# File 'lib/datamappify/data/criteria/common.rb', line 47

def key_name
  primary_record? ? :id : "#{entity.class.name.demodulize.underscore}_id".to_sym
end

#key_valuevoid (protected)

This method returns an undefined value.

The value of #key_name



54
55
56
# File 'lib/datamappify/data/criteria/common.rb', line 54

def key_value
  criteria.with_indifferent_access[key_name]
end

#new_record?Boolean (protected)

Determines whether or not it’s going to be a new record by looking at the #key_value

Returns:

  • (Boolean)


61
62
63
# File 'lib/datamappify/data/criteria/common.rb', line 61

def new_record?
  key_value.nil?
end

#perform_with_callbacksvoid

This method returns an undefined value.

Performs the action (defined by child method classes) with callbacks



34
35
36
37
38
39
40
# File 'lib/datamappify/data/criteria/common.rb', line 34

def perform_with_callbacks
  result = perform

  store_attribute_value if attributes

  result
end

#primary_record?Boolean (protected)

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/datamappify/data/criteria/common.rb', line 68

def primary_record?
  source_class.name.demodulize == entity.class.name.demodulize
end

#store_attribute_valuevoid (protected)

This method returns an undefined value.

Stores the attribute value in Mapper::Attribute for later use



97
98
99
100
101
# File 'lib/datamappify/data/criteria/common.rb', line 97

def store_attribute_value
  attributes.each do |attribute|
    attribute.value = entity.instance_variable_get("@#{attribute.name}")
  end
end