Class: Eddy::Loop::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/eddy/models/loop/base.rb

Overview

A repeated collection of Segments and/or other Loops.

See:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, *components) ⇒ void

All of a Loop's elements need to be declared in its constructor.



35
36
37
38
39
40
# File 'lib/eddy/models/loop/base.rb', line 35

def initialize(store, *components)
  @store = store
  components.flatten!
  @components = components || []
  @content = []
end

Instance Attribute Details

#componentsArray<Eddy::Segment, Eddy::Loop::Base> (readonly)

An array of Segments and/or other Loops. This is used as a template to populate content.



23
24
25
# File 'lib/eddy/models/loop/base.rb', line 23

def components
  @components
end

#contentArray<Array>

An array of loop iterations.



28
29
30
# File 'lib/eddy/models/loop/base.rb', line 28

def content
  @content
end

#loop_idString (readonly)

(Name) A unique string used to identify the Loop within its Transaction Set.



13
14
15
# File 'lib/eddy/models/loop/base.rb', line 13

def loop_id
  @loop_id
end

#repeatInteger (readonly)

Number of times a particular Loop may be repeated.



16
17
18
# File 'lib/eddy/models/loop/base.rb', line 16

def repeat
  @repeat
end

#reqString (readonly)

Defines if/how the Loop is required.



19
20
21
# File 'lib/eddy/models/loop/base.rb', line 19

def req
  @req
end

#storeEddy::Data::Store (readonly)



25
26
27
# File 'lib/eddy/models/loop/base.rb', line 25

def store
  @store
end

Instance Method Details

#add_iteration(&block) ⇒ void



61
62
63
64
65
66
67
68
69
70
# File 'lib/eddy/models/loop/base.rb', line 61

def add_iteration(&block)
  iteration = self.components.map { |c| c.new(self.store) }
  if block_given?
    yield(*iteration)
  else
    raise Eddy::Errors::Error, "No block given in loop iteration"
  end
  self.content << iteration
  return nil
end

#all_contentsArray<Eddy::Segment>

Return all contained Segments in a single, flattened array.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eddy/models/loop/base.rb', line 45

def all_contents()
  contents = self.content.flatten.map do |c|
    if c.is_a?(Eddy::Loop::Base)
      c.all_contents()
    elsif c.is_a?(Eddy::Segment)
    # elsif c < Eddy::Segment
      c
    else
      raise Eddy::Errors::RenderError
    end
  end
  return contents.flatten
end