Class: ActiveContainer::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/active_container/wrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Wrapper

Returns a new instance of Wrapper.



5
6
7
8
9
10
11
# File 'lib/active_container/wrapper.rb', line 5

def initialize(record)
  if record.is_a?(Hash)
    @record = self.class.object_class.new(record)
  else
    @record = record
  end
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/active_container/wrapper.rb', line 3

def record
  @record
end

Class Method Details

.object_classObject



37
38
39
40
41
42
# File 'lib/active_container/wrapper.rb', line 37

def object_class
  return nil if self == Wrapper
  return @object_class if defined?(@object_class)
  object_class = self.name.gsub(/Wrapper/, '')
  @object_class = Kernel.const_get(object_class)
end

.wrap(record) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_container/wrapper.rb', line 14

def wrap(record)
  return nil unless record

  if self == Wrapper
    wrapper_name = record.class.name
    wrapper_name += 'Wrapper'

    wrapper = Kernel.const_get(wrapper_name)

    wrapper.new(record)
  else
    self.new record
  end
end

.wrap_collection(records) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/active_container/wrapper.rb', line 29

def wrap_collection(records)
  return [] unless records

  records.map do |record|
    self.wrap record
  end
end

Instance Method Details

#wrapped?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_container/wrapper.rb', line 86

def wrapped?
  true
end