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

#any_attributeAttribute (protected)

Returns:

  • (Attribute)


114
115
116
# File 'lib/datamappify/data/criteria/common.rb', line 114

def any_attribute
  @any_attribute ||= attributes.first
end

#attributes_and_valuesHash (protected)

Attributes with their corresponding values

Returns:

  • (Hash)


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/datamappify/data/criteria/common.rb', line 90

def attributes_and_values
  @attributes_and_values ||= begin
    hash = {}

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

    hash
  end
end

#default_source_class_nameString (protected)

Name of the default source class, e.g. “User”, it is determined from either the PK or the entity

Returns:

  • (String)


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

def default_source_class_name
  @default_source_class_name ||= pk ? pk.source_class_name : entity.class.name.demodulize
end

#ignore?Boolean (protected)

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

Returns:

  • (Boolean)


83
84
85
# File 'lib/datamappify/data/criteria/common.rb', line 83

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)


130
131
132
# File 'lib/datamappify/data/criteria/common.rb', line 130

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)


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

def key_name
  primary_record? ? :id : any_attribute.primary_reference_key
end

#key_valuevoid (protected)

This method returns an undefined value.

The value of #key_name



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

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)


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

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

#pkAttribute (protected)

Returns:

  • (Attribute)


119
120
121
# File 'lib/datamappify/data/criteria/common.rb', line 119

def pk
  @pk ||= attributes.find { |attribute| attribute.key == :id }
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)


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

def primary_record?
  source_class.name.demodulize == default_source_class_name
end

#store_attribute_valuevoid (protected)

This method returns an undefined value.

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



107
108
109
110
111
# File 'lib/datamappify/data/criteria/common.rb', line 107

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