Sha256: 58747f8bdf284efbf6b9411c3fd2d03bee9d864489970e2b0dab7db11394cb64

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require "much-result"

class MuchResult; end
class MuchResult::Aggregate
  def self.call(values)
    new(values).call
  end

  def initialize(values)
    @values = values
  end

  def call
    combine_values(@values)
  end

  private

  def combine_values(values)
    combine_in_a_collection(array_wrap(values))
  end

  def combine_in_a_collection(values)
    if all_hash_values?(values)
      values.
        reduce { |acc, value| combine_in_a_hash(acc, value) }.
        transform_values { |nested_values| combine_values(nested_values) }
    else
      array_wrap(
        values.reduce([]) { |acc, value| combine_in_an_array(acc, value) }
      )
    end
  end

  def combine_in_a_hash(hash1, hash2)
    ((h1 = hash1 || {}).keys + (h2 = hash2 || {}).keys).
      uniq.
      reduce({}) { |hash, key|
        hash[key] = combine_in_an_array(h1[key], h2[key])
        hash
      }
  end

  def combine_in_an_array(acc, other)
    array_wrap(acc) + array_wrap(other)
  end

  def array_wrap(value)
    value.kind_of?(Array) ? value : [value]
  end

  def all_hash_values?(values)
    (compacted = values.compact).reduce(compacted.any?) { |acc, value|
      acc && value.kind_of?(::Hash)
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
much-result-0.1.2 lib/much-result/aggregate.rb
much-result-0.1.1 lib/much-result/aggregate.rb
much-result-0.1.0 lib/much-result/aggregate.rb