class Flor::Pro::Merge

Public Instance Methods

pre_execute() click to toggle source
# File lib/flor/pcore/merge.rb, line 74
def pre_execute

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

  unatt_unkeyed_children
end
receive_last() click to toggle source
# 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

Protected Instance Methods

merge_arrays(as) click to toggle source
# File lib/flor/pcore/merge.rb, line 123
def merge_arrays(as)

  as
    .inject([]) { |r, a| a.each_with_index { |e, i| r[i] = e }; r }
end
merge_objects(os) click to toggle source
# File lib/flor/pcore/merge.rb, line 129
def merge_objects(os)

  os
    .inject({}) { |r, o| r.merge(o) }
end