Sha256: 8759a6801f48bc64874b0cced675737a0ad2020ca5cdc7a4e833ed242e88ad5a

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require 'spec_helper'

describe Enumerable do
  context "with a non-empty collection of hashes" do
    let(:collection) {
      [
        {name: 'Luke Skywalker', class: 'Jedi Knight', world: 'Star Wars', power_level: 50},
        {name: 'Emporer Palpatine', class: 'Sith Lord', world: 'Star Wars', power_level: 95},
        {name: 'Mithrander', class: 'Wizard', world: 'Middle Earth', power_level: 9001},
        {name: 'Aragorn', class: 'Ranger', world: 'Middle Earth', power_level: 80}
      ]
    }

    let(:groupings) { [:class] }

    it "should transform a collection into nested hash based on and array of groups" do
      expect(
        collection.group_by_recursive(groupings)
      ).to eq(
        {
          "Jedi Knight"=>[{:name=>"Luke Skywalker", :class=>"Jedi Knight", :world=>"Star Wars", :power_level=>50}],
          "Sith Lord"=>[{:name=>"Emporer Palpatine", :class=>"Sith Lord", :world=>"Star Wars", :power_level=>95}],
          "Wizard"=>[{:name=>"Mithrander", :class=>"Wizard", :world=>"Middle Earth", :power_level=>9001}],
          "Ranger"=>[{:name=>"Aragorn", :class=>"Ranger", :world=>"Middle Earth", :power_level=>80}]
        }
      )
    end

    it "should traverse a nested hash" do
      grouped_collection = collection.group_by_recursive(groupings)
      ungrouped = []
      grouped_collection.each_pair_recursive { |_k, v| ungrouped.concat(v) }
      expect(ungrouped).to eq(collection)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
data-table-2.0.3 spec/enum_spec.rb
data-table-2.0.2 spec/enum_spec.rb
data-table-2.0.1 spec/enum_spec.rb
data-table-2.0 spec/enum_spec.rb