Class: Historical::Models::AttributeDiff
- Inherits:
-
Object
- Object
- Historical::Models::AttributeDiff
- Extended by:
- Historical::MongoMapperEnhancements
- Includes:
- MongoMapper::EmbeddedDocument
- Defined in:
- lib/historical/models/attribute_diff.rb
Overview
The diff of an attribute (specialized by a type qualifier)
Constant Summary collapse
- SUPPORTED_NATIVE_RUBY_TYPES =
MongoMappers supported native Ruby types
%w{Date String Time Boolean Integer Float Binary}
Instance Attribute Summary collapse
-
#parent ⇒ ModelVersion::Diff
The parent diff model.
Class Method Summary collapse
-
.detect_attribute_type(parent, attribute) ⇒ Object
Get attribute type for an attribute.
-
.generate_subclasses! ⇒ Object
Generates subclasses of AttributeDiff for each type in SUPPORTED_NATIVE_RUBY_TYPES.
-
.specialized_for(parent, attribute) ⇒ Class
The specialized AttributeDiff class for a attribute type.
Instance Method Summary collapse
-
#attribute ⇒ String
The attribute name.
-
#attribute_type ⇒ String
The attribute type (string, integer, float).
- #new_value=(value) ⇒ Object
- #old_value=(value) ⇒ Object
Methods included from Historical::MongoMapperEnhancements
Instance Attribute Details
#parent ⇒ ModelVersion::Diff
Returns The parent diff model.
29 30 31 |
# File 'lib/historical/models/attribute_diff.rb', line 29 def parent @parent end |
Class Method Details
.detect_attribute_type(parent, attribute) ⇒ Object
Get attribute type for an attribute
52 53 54 55 56 57 58 59 60 |
# File 'lib/historical/models/attribute_diff.rb', line 52 def self.detect_attribute_type(parent, attribute) model_class = parent.record.class column = model_class.columns.select do |c| c.name.to_s == attribute.to_s end.first column ? column.type.to_s : nil end |
.generate_subclasses! ⇒ Object
Generates subclasses of Historical::Models::AttributeDiff for each type in SUPPORTED_NATIVE_RUBY_TYPES
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/historical/models/attribute_diff.rb', line 64 def self.generate_subclasses! SUPPORTED_NATIVE_RUBY_TYPES.each do |type| diff_class = Class.new(self) type_class = type.constantize diff_class.send :key, :_old_value, type_class diff_class.send :key, :_new_value, type_class const_name = specialized_class_name(type_class) namespace = Historical::Models namespace.send(:remove_const, const_name) if namespace.const_defined?(const_name) namespace.const_set(const_name, diff_class) end end |
.specialized_for(parent, attribute) ⇒ Class
Returns The specialized Historical::Models::AttributeDiff class for a attribute type.
38 39 40 41 |
# File 'lib/historical/models/attribute_diff.rb', line 38 def self.specialized_for(parent, attribute) type = detect_attribute_type(parent, attribute) Historical::Models.const_get(specialized_class_name(type)) end |
Instance Method Details
#attribute ⇒ String
Returns The attribute name.
15 |
# File 'lib/historical/models/attribute_diff.rb', line 15 key :attribute, String, :required => true |
#attribute_type ⇒ String
Returns The attribute type (string, integer, float).
18 |
# File 'lib/historical/models/attribute_diff.rb', line 18 key :attribute_type, String, :required => true |
#new_value=(value) ⇒ Object
47 48 49 |
# File 'lib/historical/models/attribute_diff.rb', line 47 def new_value=(value) self._new_value = value end |
#old_value=(value) ⇒ Object
43 44 45 |
# File 'lib/historical/models/attribute_diff.rb', line 43 def old_value=(value) self._old_value = value end |