Module: Knuckles::Stages::Combiner

Extended by:
Combiner
Included in:
Combiner
Defined in:
lib/knuckles/stages/combiner.rb

Overview

The combiner stage merges all of the individually rendered results into a single hash. The output of this stage is a single object with string keys and array values, ready to be serialized.

Instance Method Summary collapse

Instance Method Details

#call(prepared, _options) ⇒ Object

Merge all of the rendered data into a single hash. Each resulting value will be an array, even if there was only one value in the original rendered results.

Examples:

Combining rendered data


prepared = [
  {
    result: {
      author: {id: 1, name: "Michael"},
      posts: [{id: 1, title: "hello"}],
    }
  }, {
    result: {
      author: {id: 1, name: "Michael"},
      posts: [{id: 2, title: "there"}],
    }
  }
]

Knuckles::Stage::Combiner.call(prepared, {}) #=> {
  "author" => [
    {id: 1, name: "Michael"}
  ],
  "posts" => [
    {id: 1, title: "hello"},
    {id: 2, title: "there"}
  ]
}

Parameters:

  • prepared (Enumerable)

    The prepared collection to be combined

  • _options (Hash)

    Options aren’t used, but are accepted to maintain a consistent interface



45
46
47
48
49
50
51
52
53
54
# File 'lib/knuckles/stages/combiner.rb', line 45

def call(prepared, _options)
  prepared.each_with_object(array_backed_hash) do |hash, memo|
    hash[:result].each do |root, values|
      case values
      when Hash  then memo[root.to_s] << values
      when Array then memo[root.to_s] += values
      end
    end
  end
end