Class: Historical::Models::AttributeDiff

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Historical::MongoMapperEnhancements

belongs_to_active_record

Instance Attribute Details

#parentModelVersion::Diff

Returns The parent diff model.

Returns:



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.

Returns:



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

#attributeString

Returns The attribute name.

Returns:

  • (String)

    The attribute name



15
# File 'lib/historical/models/attribute_diff.rb', line 15

key :attribute,       String, :required => true

#attribute_typeString

Returns The attribute type (string, integer, float).

Returns:

  • (String)

    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