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:



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

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

Class Method Details

.wrap(attribute, record_binding) ⇒ Object

:nodoc:



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

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:



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

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:



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

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.



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

def unwrap(attribute) end

#wrap(attribute) ⇒ Object

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



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

def wrap(attribute) end