lib/gloo/objs/ctrl/each.rb in gloo-0.4.0 vs lib/gloo/objs/ctrl/each.rb in gloo-0.5.0

- old
+ new

@@ -13,10 +13,11 @@ module Objs class Each < Gloo::Core::Obj KEYWORD = 'each'.freeze KEYWORD_SHORT = 'each'.freeze + CHILD = 'child'.freeze WORD = 'word'.freeze LINE = 'line'.freeze FILE = 'file'.freeze REPO = 'repo'.freeze IN = 'IN'.freeze @@ -46,11 +47,13 @@ end # Run the do script once. def run_do o = find_child DO - o.send_message( 'run' ) if o.can_receive_message? 'run' + return unless o + + Gloo::Exec::Dispatch.message 'run', o end # --------------------------------------------------------------------- # Children # --------------------------------------------------------------------- @@ -65,22 +68,13 @@ # Add children to this object. # This is used by containers to add children needed # for default configurations. def add_default_children fac = $engine.factory - fac.create( { :name => 'word', - :type => 'string', - :value => '', - :parent => self } ) - fac.create( { :name => 'in', - :type => 'string', - :value => '', - :parent => self } ) - fac.create( { :name => 'do', - :type => 'script', - :value => '', - :parent => self } ) + fac.create_string WORD, '', self + fac.create_string IN, '', self + fac.create_script DO, '', self end # --------------------------------------------------------------------- # Messages # --------------------------------------------------------------------- @@ -92,20 +86,55 @@ return super + [ 'run' ] end # Run the system command. def msg_run - if each_word? + if each_child? + run_each_child + elsif each_word? run_each_word elsif each_line? run_each_line elsif each_repo? run_each_repo end end # --------------------------------------------------------------------- + # Child Object + # --------------------------------------------------------------------- + + # Is it set up to run for each word? + # If there is a child object by the name "word" + # then we will loop for each word in the string. + def each_child? + return true if contains_child? CHILD + + return false + end + + # Run for each word. + def run_each_child + o = find_child IN + return unless o + + o = Gloo::Objs::Alias.resolve_alias( o ) + o.children.each do |child| + set_child child + run_do + end + end + + # Set the child alias. + def set_child( obj ) + o = find_child CHILD + return unless o + + o.set_value obj.pn + end + + # --------------------------------------------------------------------- # Word # --------------------------------------------------------------------- # Is it set up to run for each word? # If there is a child object by the name "word" @@ -151,11 +180,11 @@ # Run for each line. def run_each_line str = in_value return unless str - str.split( '\n' ).each do |line| + str.each_line do |line| set_line line run_do end end @@ -227,10 +256,10 @@ DESCRIPTION Perform an action for each item in a collection. CHILDREN - word | line | repo - string - none + child | word | line | repo - string - none The entity we want to loop for. It will hold the current value while the script is running. in - string - none The collection we will iterate in. In the case of <word> or <line> this will be a string or text.