Class: ActiveRecord::Wrappings::AbstractWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/wrappings.rb

Overview

:nodoc:

Direct Known Subclasses

YamlWrapper

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ AbstractWrapper

:nodoc:



23
24
25
# File 'lib/active_record/wrappings.rb', line 23

def initialize(attribute) #:nodoc:
  @attribute = attribute
end

Class Method Details

.wrap(attribute, record_binding) ⇒ Object

:nodoc:



17
18
19
20
21
# File 'lib/active_record/wrappings.rb', line 17

def self.wrap(attribute, record_binding) #:nodoc:
  %w( before_save after_save after_initialize ).each do |callback|
    eval "#{callback} #{name}.new('#{attribute}')", record_binding
  end
end

Instance Method Details

#load_wrapped_attribute(record) ⇒ Object Also known as: after_save

:nodoc:



37
38
39
40
41
42
43
44
45
# File 'lib/active_record/wrappings.rb', line 37

def load_wrapped_attribute(record) #:nodoc:
  if record.attribute_present?(@attribute)
    record.send(
      "write_attribute", 
      @attribute, 
      unwrap(record.send("read_attribute", @attribute))
    )
  end
end

#save_wrapped_attribute(record) ⇒ Object Also known as: before_save

:nodoc:



27
28
29
30
31
32
33
34
35
# File 'lib/active_record/wrappings.rb', line 27

def save_wrapped_attribute(record) #:nodoc:
  if record.attribute_present?(@attribute)
    record.send(
      "write_attribute", 
      @attribute, 
      wrap(record.send("read_attribute", @attribute))
    )
  end
end

#unwrap(attribute) ⇒ Object

Overwrite to implement the logic that’ll take the wrapped attribute and unwrap it.



55
# File 'lib/active_record/wrappings.rb', line 55

def unwrap(attribute) end

#wrap(attribute) ⇒ Object

Overwrite to implement the logic that’ll take the regular attribute and wrap it.



52
# File 'lib/active_record/wrappings.rb', line 52

def wrap(attribute) end