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



31
32
33
34
35
# 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

Instance Attribute Details

#attributesSet<Mapper::Attribute> (readonly)

Returns:



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

def attributes
  @attributes
end

#attributes_and_valuesHash

Attributes with their corresponding values

Returns:

  • (Hash)


20
21
22
# File 'lib/datamappify/data/criteria/common.rb', line 20

def attributes_and_values
  @attributes_and_values
end

#criteriavoid

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

#optionsHash (readonly)

Returns:

  • (Hash)


23
24
25
# File 'lib/datamappify/data/criteria/common.rb', line 23

def options
  @options
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)


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

def any_attribute
  @any_attribute ||= attributes.first
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)


71
72
73
# 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?Boolean (protected)

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/datamappify/data/criteria/common.rb', line 106

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)


136
137
138
# File 'lib/datamappify/data/criteria/common.rb', line 136

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)


78
79
80
# File 'lib/datamappify/data/criteria/common.rb', line 78

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



85
86
87
# File 'lib/datamappify/data/criteria/common.rb', line 85

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)


92
93
94
# File 'lib/datamappify/data/criteria/common.rb', line 92

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



40
41
42
43
44
45
46
# File 'lib/datamappify/data/criteria/common.rb', line 40

def perform_with_callbacks
  result = perform

  store_attribute_value if attributes

  result
end

#pkAttribute (protected)

Returns:

  • (Attribute)


125
126
127
# File 'lib/datamappify/data/criteria/common.rb', line 125

def pk
  @pk ||= attributes.find(&:primary_key?)
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)


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

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



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

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