Class: Flor::Pro::Merge

Inherits:
Flor::Procedure show all
Defined in:
lib/flor/pcore/merge.rb

Constant Summary

Constants inherited from Flor::Procedure

Flor::Procedure::RVARS, Flor::Procedure::TRUE_ATTS

Instance Attribute Summary

Attributes inherited from Node

#message

Instance Method Summary collapse

Methods inherited from Flor::Procedure

[], core?, #debug_msg, #debug_tree, #end, #flank, #heap, inherited, make, names, #prepare_on_receive_last, #trigger_on_error

Methods inherited from Node

#child_id, #cnodes, #cnodes_any?, #cnodes_empty?, #deref, #descendant_of?, #domain, #exid, #fei, #from, #h, #initialize, #lookup_tree, #lookup_value, #message_or_node_payload, #nid, #node_closed?, #node_ended?, #node_open?, #node_payload, #node_payload_ret, #node_status, #node_status_flavour, #on_error_parent, #parent, #payload, #payload_ret, #point, #reheap, #to_procedure_node, #tree

Constructor Details

This class inherits a constructor from Flor::Node

Instance Method Details

#pre_executeObject



74
75
76
77
78
79
80
# File 'lib/flor/pcore/merge.rb', line 74

def pre_execute

  @node['atts'] = []
  @node['rets'] = []

  unatt_unkeyed_children
end

#receive_lastObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/flor/pcore/merge.rb', line 82

def receive_last

  indexes = @node['rets']
    .each_with_index
    .inject({ array: [], object: [], other: [] }) { |is, (e, i)|
      case e
      when Array then is[:array]
      when Hash then is[:object]
      else is[:other]
      end << i
      is }

  a0 = indexes[:array].first || @node['rets'].length
  o0 = indexes[:object].first || @node['rets'].length

  kln = a0 < o0 ? :array : :object
  okln = kln == :array ? :object : :array

  cols = indexes[kln].collect { |i| @node['rets'][i] }

  cols.unshift(node_payload_ret) \
    if cols.length == 1 && Flor.type(node_payload_ret) == kln

  fail Flor::FlorError.new('found no array or object to merge', self) \
    if cols.empty?

  unless att('lax', 'loose') == true || att('strict') == false

    others = (indexes[:other] + indexes[okln]).sort

    fail Flor::FlorError.new(
      "found a non-#{kln} item (#{Flor.type(@node['rets'][others[0]])} item)",
      self
    ) if others.any?
  end

  wrap('ret' => send(kln == :array ? :merge_arrays : :merge_objects, cols))
end