Class: Datamappify::Data::Criteria::Common
- Inherits:
-
Object
- Object
- Datamappify::Data::Criteria::Common
- 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
Direct Known Subclasses
ActiveRecord::Destroy, ActiveRecord::Exists, ActiveRecord::Transaction, Relational::Count, Relational::Find, Relational::FindMultiple, Relational::Save, Sequel::Destroy, Sequel::Exists, Sequel::Transaction
Instance Attribute Summary collapse
- #attributes ⇒ Set<Mapper::Attribute> readonly
-
#attributes_and_values ⇒ Hash
Attributes with their corresponding values.
- #criteria ⇒ void
- #entity ⇒ Entity readonly
- #options ⇒ Hash readonly
- #source_class ⇒ Class readonly
Instance Method Summary collapse
- #any_attribute ⇒ Attribute protected
-
#default_source_class_name ⇒ String
protected
Name of the default source class, e.g.
-
#ignore? ⇒ Boolean
protected
Ignores the current Criteria’s operation if there is no dirty attributes.
-
#ignore_attribute?(attribute) ⇒ Boolean
private
Ignores the attribute if it isn’t dirty or if it’s a primary key.
-
#initialize(source_class, *args) { ... } ⇒ Common
constructor
A new instance of Common.
-
#key_name ⇒ Symbol
protected
Key name of either the primary key (e.g.
id
) or foreign key (e.g.user_id
). -
#key_value ⇒ void
protected
The value of #key_name.
-
#new_record? ⇒ Boolean
protected
Determines whether or not it’s going to be a new record by looking at the #key_value.
-
#perform_with_callbacks ⇒ void
Performs the action (defined by child method classes) with callbacks.
- #pk ⇒ Attribute protected
-
#primary_record? ⇒ Boolean
protected
Determines whether or not it’s a primary record by comparing the source class and the entity class.
-
#store_attribute_value ⇒ void
protected
Stores the attribute value in Mapper::Attribute for later use.
Constructor Details
#initialize(source_class, *args) { ... } ⇒ Common
Returns a new instance of Common.
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
#attributes ⇒ Set<Mapper::Attribute> (readonly)
17 18 19 |
# File 'lib/datamappify/data/criteria/common.rb', line 17 def attributes @attributes end |
#attributes_and_values ⇒ Hash
Attributes with their corresponding values
20 21 22 |
# File 'lib/datamappify/data/criteria/common.rb', line 20 def attributes_and_values @attributes_and_values end |
#criteria ⇒ void
This method returns an undefined value.
14 15 16 |
# File 'lib/datamappify/data/criteria/common.rb', line 14 def criteria @criteria end |
#entity ⇒ Entity (readonly)
11 12 13 |
# File 'lib/datamappify/data/criteria/common.rb', line 11 def entity @entity end |
#options ⇒ Hash (readonly)
23 24 25 |
# File 'lib/datamappify/data/criteria/common.rb', line 23 def @options end |
#source_class ⇒ Class (readonly)
8 9 10 |
# File 'lib/datamappify/data/criteria/common.rb', line 8 def source_class @source_class end |
Instance Method Details
#any_attribute ⇒ Attribute (protected)
120 121 122 |
# File 'lib/datamappify/data/criteria/common.rb', line 120 def any_attribute @any_attribute ||= attributes.first end |
#default_source_class_name ⇒ String (protected)
Name of the default source class, e.g. “User”, it is determined from either the PK or the entity
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
106 107 108 |
# File 'lib/datamappify/data/criteria/common.rb', line 106 def ignore? attributes_and_values.empty? end |
#ignore_attribute?(attribute) ⇒ Boolean (private)
implement proper dirty attribute tracking
Ignores the attribute if it isn’t dirty or if it’s a primary key
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_name ⇒ Symbol (protected)
Key name of either the primary key (e.g. id
) or foreign key (e.g. user_id
)
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_value ⇒ void (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
92 93 94 |
# File 'lib/datamappify/data/criteria/common.rb', line 92 def new_record? key_value.nil? end |
#perform_with_callbacks ⇒ void
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 |
#pk ⇒ Attribute (protected)
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
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_value ⇒ void (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 |