Module: Denormalized::Core
- Defined in:
- lib/denormalized/core.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #assign_attributes(new_attributes) ⇒ Object
- #contains_denormalized_attributes(attributes) ⇒ Object
- #denormalized_subject(table) ⇒ Object
- #denormalized_subjects(table:, where_attrs:) ⇒ Object
- #denormalized_tables ⇒ Object
- #extract_denormalized_attributes(new_attributes) ⇒ Object
- #extract_existing_denormalized_attributes(new_attributes) ⇒ Object
- #update(attributes) ⇒ Object
- #update_attribute(name, value) ⇒ Object
- #update_columns(attributes) ⇒ Object
- #write_attribute(attr_name, value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 |
# File 'lib/denormalized/core.rb', line 5 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#assign_attributes(new_attributes) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/denormalized/core.rb', line 88 def assign_attributes(new_attributes) if contains_denormalized_attributes(new_attributes) gifted_attributes = extract_denormalized_attributes(new_attributes) denormalized_tables.each do |table| denormalized_subjects( table: table, where_attrs: extract_existing_denormalized_attributes(new_attributes) ).each { |obj| obj.assign_attributes(gifted_attributes) } end end super end |
#contains_denormalized_attributes(attributes) ⇒ Object
25 26 27 28 29 |
# File 'lib/denormalized/core.rb', line 25 def contains_denormalized_attributes(attributes) attributes.keys.any? do |name| self.class.denormalized_attribute?(name) end end |
#denormalized_subject(table) ⇒ Object
9 10 11 |
# File 'lib/denormalized/core.rb', line 9 def denormalized_subject(table) table.to_s.classify.constantize end |
#denormalized_subjects(table:, where_attrs:) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/denormalized/core.rb', line 13 def denormalized_subjects(table:, where_attrs:) denormalized_subject( table ).where( where_attrs ).uniq end |
#denormalized_tables ⇒ Object
21 22 23 |
# File 'lib/denormalized/core.rb', line 21 def denormalized_tables denormalized_configuration[:tables].uniq end |
#extract_denormalized_attributes(new_attributes) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/denormalized/core.rb', line 39 def extract_denormalized_attributes(new_attributes) {}.tap do |extracted_attributes| (denormalized_configuration[:columns] & new_attributes.keys).each do |attribute| extracted_attributes[attribute] = new_attributes[attribute] end end end |
#extract_existing_denormalized_attributes(new_attributes) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/denormalized/core.rb', line 31 def extract_existing_denormalized_attributes(new_attributes) {}.tap do |extracted_attributes| (denormalized_configuration[:columns] & new_attributes.keys).each do |attribute| extracted_attributes[attribute] = read_attribute(attribute) end end end |
#update(attributes) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/denormalized/core.rb', line 73 def update(attributes) if contains_denormalized_attributes(attributes) gifted_attributes = extract_denormalized_attributes(attributes) denormalized_tables.each do |table| denormalized_subjects( table: table, where_attrs: extract_existing_denormalized_attributes(attributes) ).each { |obj| obj.update(gifted_attributes) } end end super end |
#update_attribute(name, value) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/denormalized/core.rb', line 60 def update_attribute(name, value) if self.class.denormalized_attribute?(attr_name) denormalized_tables.each do |table| denormalized_subjects( table: table, where_attrs: { name => read_attribute(name) } ).each { |obj| obj.send(:update_attribute, name, value) } end end super end |
#update_columns(attributes) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/denormalized/core.rb', line 103 def update_columns(attributes) if contains_denormalized_attributes(attributes) gifted_attributes = extract_denormalized_attributes(attributes) denormalized_tables.each do |table| denormalized_subjects( table: table, where_attrs: extract_existing_denormalized_attributes(attributes) ).each { |obj| obj.send(:update_columns, gifted_attributes) } end end super end |
#write_attribute(attr_name, value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/denormalized/core.rb', line 47 def write_attribute(attr_name, value) if self.class.denormalized_attribute?(attr_name) denormalized_tables.each do |table| denormalized_subjects( table: table, where_attrs: { attr_name => read_attribute(attr_name) } ).each { |obj| obj.send(:write_attribute, attr_name, value) } end end super end |