Class: FeideeUtils::Record::ModifiedRecord
- Inherits:
-
Object
- Object
- FeideeUtils::Record::ModifiedRecord
- Defined in:
- lib/feidee_utils/record/modified_record.rb
Overview
TODO: Reconsider this class and ship full support to all entities.
Direct Known Subclasses
Defined Under Namespace
Classes: ValuePair
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#modified_fields ⇒ Object
readonly
Returns the value of attribute modified_fields.
-
#poid ⇒ Object
readonly
Returns the value of attribute poid.
Class Method Summary collapse
- .define_custom_methods(fields) ⇒ Object
- .define_default_methods(field_mappings) ⇒ Object
- .fields_diff(base, head) ⇒ Object
Instance Method Summary collapse
- #changed? ⇒ Boolean
-
#initialize(poid, base, head) ⇒ ModifiedRecord
constructor
A new instance of ModifiedRecord.
- #touched? ⇒ Boolean
Constructor Details
#initialize(poid, base, head) ⇒ ModifiedRecord
Returns a new instance of ModifiedRecord.
9 10 11 12 13 14 15 16 |
# File 'lib/feidee_utils/record/modified_record.rb', line 9 def initialize(poid, base, head) raise "Base row doesn't have the given poid." if base.poid != poid raise "Head row doesn't have the given poid." if head.poid != poid @poid = poid @base = base @head = head @modified_fields = self.class.fields_diff(base.field, head.field) end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
6 7 8 |
# File 'lib/feidee_utils/record/modified_record.rb', line 6 def base @base end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
6 7 8 |
# File 'lib/feidee_utils/record/modified_record.rb', line 6 def head @head end |
#modified_fields ⇒ Object (readonly)
Returns the value of attribute modified_fields.
7 8 9 |
# File 'lib/feidee_utils/record/modified_record.rb', line 7 def modified_fields @modified_fields end |
#poid ⇒ Object (readonly)
Returns the value of attribute poid.
5 6 7 |
# File 'lib/feidee_utils/record/modified_record.rb', line 5 def poid @poid end |
Class Method Details
.define_custom_methods(fields) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/feidee_utils/record/modified_record.rb', line 49 def self.define_custom_methods fields fields.each do |name| if !respond_to? name define_method name do ValuePair.new((base.send name), (head.send name)) end define_method (name.to_s + "_changed?").to_sym do (base.send name) != (head.send name) end end end end |
.define_default_methods(field_mappings) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/feidee_utils/record/modified_record.rb', line 62 def self.define_default_methods field_mappings field_mappings.each do |name, key| if !respond_to? name define_method name do modified_fields[key] end define_method (name.to_s + "_changed?").to_sym do modified_fields.has_key? key end end end end |
.fields_diff(base, head) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/feidee_utils/record/modified_record.rb', line 26 def self.fields_diff base, head (base.keys.sort | head.keys.sort).inject({}) do |hash, key| if base[key] != head[key] hash[key] = ValuePair.new(base[key], head[key]) end hash end end |
Instance Method Details
#changed? ⇒ Boolean
39 40 41 42 43 44 45 46 |
# File 'lib/feidee_utils/record/modified_record.rb', line 39 def changed? methods.inject(false) do |acc, name| if name.to_s.end_with? "_changed?" acc ||= send name end acc end end |
#touched? ⇒ Boolean
35 36 37 |
# File 'lib/feidee_utils/record/modified_record.rb', line 35 def touched? !modified_fields.empty? end |