Class: Gloo::Objs::EachChild

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/objs/ctrl/each_child.rb

Constant Summary collapse

CHILD =
'child'.freeze
IN =
'IN'.freeze
GROUP_BY =
'group_by'.freeze
ON_GROUP_START =
'on_group_start'.freeze
ON_GROUP_END =
'on_group_end'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, iterator_obj) ⇒ EachChild


Create Iterator



23
24
25
26
# File 'lib/gloo/objs/ctrl/each_child.rb', line 23

def initialize( engine, iterator_obj )
  @engine = engine
  @iterator_obj = iterator_obj
end

Class Method Details

.use_for?(iterator_obj) ⇒ Boolean

Use this iterator for each loop?

Returns:



36
37
38
39
40
# File 'lib/gloo/objs/ctrl/each_child.rb', line 36

def self.use_for?( iterator_obj )
  return true if iterator_obj.find_child CHILD

  return false
end

Instance Method Details

#group_by_value(obj_can) ⇒ Object

Get the child that is the group by. Return nil if there is no group by.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gloo/objs/ctrl/each_child.rb', line 63

def group_by_value obj_can
  return nil unless obj_can

  child = @iterator_obj.find_child GROUP_BY
  return nil unless child

  group_by_child_name = child.value

  obj = obj_can.find_child( group_by_child_name )
  return nil unless obj

  return obj.value
end

#has_group_by?Boolean

If the iterator has a group by, then we need to group by that value. Otherwise, we just loop for each child.

Returns:



52
53
54
55
56
57
# File 'lib/gloo/objs/ctrl/each_child.rb', line 52

def has_group_by?
  child = @iterator_obj.find_child GROUP_BY
  return false unless child

  return true
end

#runObject

Run for each child.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gloo/objs/ctrl/each_child.rb', line 105

def run
  o = @iterator_obj.find_child IN
  return unless o

  # Set up for optional groups.
  group_mode = false
  if has_group_by?
    group_mode = true
    last_group = nil
    first_time = true
  end

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  o.children.each do |child|
    if group_mode
      group_value = group_by_value( child )
      # puts "last group = #{last_group}, group_value = #{group_value}"
      if last_group != group_value
        last_group = group_value
        # puts "New group: #{group_value}"
        if first_time
          first_time = false
        else
          run_on_group_end
        end
        run_on_group_start
      end
    end

    set_child child
    @iterator_obj.run_do
  end

  if group_mode && !first_time
    run_on_group_end
  end
end

#run_on_group_endObject

Run the on group end script.



90
91
92
93
94
95
# File 'lib/gloo/objs/ctrl/each_child.rb', line 90

def run_on_group_end
  o = @iterator_obj.find_child ON_GROUP_END
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_on_group_startObject

Run the on group start script.



80
81
82
83
84
85
# File 'lib/gloo/objs/ctrl/each_child.rb', line 80

def run_on_group_start
  o = @iterator_obj.find_child ON_GROUP_START
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#set_child(obj) ⇒ Object

Set the child alias.



146
147
148
149
150
151
# File 'lib/gloo/objs/ctrl/each_child.rb', line 146

def set_child( obj )
  o = @iterator_obj.find_child CHILD
  return unless o

  o.set_value obj.pn
end