Class: Plaster::WrappingList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/plaster/wrapping_list.rb

Overview

A list of entry values, each stored in an attribute of an instance of a specified struct-like wrapper class. This allows the list to apply the same enforcement and/or transformation that the wrapper’s attribute write/read process does.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWrappingList

Returns a new instance of WrappingList.



32
33
34
# File 'lib/plaster/wrapping_list.rb', line 32

def initialize
  @inner_array = []
end

Class Attribute Details

.wrapper_attribObject (readonly)

Returns the value of attribute wrapper_attrib.



11
12
13
# File 'lib/plaster/wrapping_list.rb', line 11

def wrapper_attrib
  @wrapper_attrib
end

.wrapper_classObject (readonly)

Returns the value of attribute wrapper_class.



11
12
13
# File 'lib/plaster/wrapping_list.rb', line 11

def wrapper_class
  @wrapper_class
end

Instance Attribute Details

#inner_arrayObject (readonly)

Returns the value of attribute inner_array.



30
31
32
# File 'lib/plaster/wrapping_list.rb', line 30

def inner_array
  @inner_array
end

Class Method Details

.wrapper_attrib_writerObject



13
14
15
# File 'lib/plaster/wrapping_list.rb', line 13

def wrapper_attrib_writer
  @wrapper_attrib_writer ||= :"#{wrapper_attrib}="
end

Instance Method Details

#<<(value) ⇒ Object



67
68
69
70
71
72
# File 'lib/plaster/wrapping_list.rb', line 67

def <<(value)
  wrapper = self.class.wrapper_class.new
  wrapper.send self.class.wrapper_attrib_writer, value
  inner_array << wrapper
  self
end

#[](index) ⇒ Object



54
55
56
57
# File 'lib/plaster/wrapping_list.rb', line 54

def [](index)
  wrapper = inner_array[index]
  wrapper.send self.class.wrapper_attrib
end

#[]=(index, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/plaster/wrapping_list.rb', line 40

def []=(index, value)
  old_length = inner_array.length
  wrapper = (
    inner_array[index] ||= self.class.wrapper_class.new
  )
  wrapper.send self.class.wrapper_attrib_writer, value
  if index > old_length
    (old_length...index).each do |fill_idx|
      inner_array[fill_idx] = self.class.wrapper_class.new
    end
  end
  value
end

#eachObject



59
60
61
62
63
64
65
# File 'lib/plaster/wrapping_list.rb', line 59

def each
  return Enumerator.new(self, :each) unless block_given?
  inner_array.each do |wrapper|
    value = wrapper.send(self.class.wrapper_attrib)
    yield value
  end
end

#model_deconstructObject



36
37
38
# File 'lib/plaster/wrapping_list.rb', line 36

def model_deconstruct
  Plaster.deconstruct( to_a )
end

#push(*values) ⇒ Object



74
75
76
77
78
# File 'lib/plaster/wrapping_list.rb', line 74

def push(*values)
  values.each do |value|
    self << value
  end
end