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
- #criteria ⇒ void readonly
- #entity ⇒ Entity readonly
- #source_class ⇒ Class readonly
Instance Method Summary collapse
-
#attributes_and_values ⇒ Hash
protected
Attributes with their corresponding values.
-
#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.
-
#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.
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
#attributes ⇒ Set<Mapper::Attribute> (readonly)
17 18 19 |
# File 'lib/datamappify/data/criteria/common.rb', line 17 def attributes @attributes end |
#criteria ⇒ void (readonly)
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 |
#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
#attributes_and_values ⇒ Hash (protected)
Attributes with their corresponding values
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
75 76 77 |
# File 'lib/datamappify/data/criteria/common.rb', line 75 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
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_name ⇒ Symbol (protected)
Key name of either the primary key (e.g. id) or foreign key (e.g. user_id)
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_value ⇒ void (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
61 62 63 |
# File 'lib/datamappify/data/criteria/common.rb', line 61 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
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
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_value ⇒ void (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 |