README.md in hash_pivot-0.1.0 vs README.md in hash_pivot-0.2.0

- old
+ new

@@ -1,13 +1,11 @@ [![codecov](https://codecov.io/gh/junara/hash_pivot/branch/main/graph/badge.svg?token=NNQ37LG8R7)](https://codecov.io/gh/junara/hash_pivot) # HashPivot -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hash_pivot`. To experiment with that code, run `bin/console` for an interactive prompt. +Pivot Array of Hash or Array of Struct or ActiveRecord::Relation. -TODO: Delete this and the text above, and describe your gem - ## Installation Install the gem and add to the application's Gemfile by executing: $ bundle add hash_pivot @@ -16,10 +14,132 @@ $ gem install hash_pivot ## Usage -TODO: Write usage instructions here +### Pivot Array of Hash + +#### Prepare data + +Prepare Array of Hash. + +```ruby + data = [ + { id: 1, role: 'guest', team: 'rabbit', age: 1 }, + { id: 2, role: 'guest', team: 'mouse', age: 2 }, + { id: 3, role: 'guest', team: 'rabbit', age: 3 }, + { id: 4, role: 'admin', team: 'mouse', age: 4 } +] +``` + +#### Basic usage + +Grouping by `:role` and pivot in `:team`. Pivot column is `rabbit or mouse` . + +```ruby +HashPivot.pivot(data, :role, :team, %w[rabbit mouse]) + +# [{ :role => "guest", +# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }], +# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] }, +# { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }] +``` + +Grouping by `:role` and pivot in `:team`. + +Pivot column is nil. This means that pivot column is automatically configured. + +```ruby +HashPivot.pivot(data, :role, :team, nil) + +# [{ :role => "guest", +# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }], +# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] }, +# { :role => "admin", "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }] +``` + +#### Pivot with summarize. + +Pivot data is summarized by block. + +Age is summarized by block. + +```ruby +HashPivot.pivot(data, :role, :team, %w[rabbit mouse]) { |array| array.sum { |h| h[:age] } } + +# [{ :role => "guest", "rabbit" => 4, "mouse" => 2 }, { :role => "admin", "rabbit" => 0, "mouse" => 4 }] +``` + + +### Pivot Array of Struct + +#### Prepare data + +Prepare Array of Struct. + +```ruby +HashPivotUserStruct = Struct.new(:id, :role, :team, :age, keyword_init: true) +data = [ + HashPivotUserStruct.new(id: 1, role: 'guest', team: 'rabbit', age: 1), + HashPivotUserStruct.new(id: 2, role: 'guest', team: 'mouse', age: 2), + HashPivotUserStruct.new({ id: 3, role: 'guest', team: 'rabbit', age: 3 }), + HashPivotUserStruct.new({ id: 4, role: 'admin', team: 'mouse', age: 4 }) +] +``` + +#### Basic usage + +Grouping by `:role` and pivot in `:team`. Pivot column is `rabbit or mouse` . + +```ruby +HashPivot.pivot(data, :role, :team, %w[rabbit mouse], repository: HashPivot::Repository::StructRepository) + +# [{ :role => "guest", +# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }], +# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] }, +# { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }] +``` + + +### Pivot Array of ActiveRecord::Relation + +#### Prepare data + +Prepare Array of ActiveRecord::Relation. + +```ruby +class MigrateSqlDatabase < ActiveRecord::Migration[6.1] + def self.up + create_table(:hash_pivot_users) do |t| + t.string :role + t.string :team + t.integer :age + end + end +end +``` + +```ruby +HashPivotUser.destroy_all +HashPivotUser.create(:hash_pivot_user, id: 1, role: 'guest', team: 'rabbit', age: 1) +HashPivotUser.create(:hash_pivot_user, id: 2, role: 'guest', team: 'mouse', age: 2) +HashPivotUser.create(:hash_pivot_user, id: 3, role: 'guest', team: 'rabbit', age: 3) +HashPivotUser.create(:hash_pivot_user, id: 4, role: 'admin', team: 'mouse', age: 4) +``` + +#### Basic usage + +Grouping by `:role` and pivot in `:team`. Pivot column is `rabbit or mouse` . + +```ruby +HashPivot.pivot(data, :role, :team, %w[rabbit mouse], repository: HashPivot::Repository::ActiveRecordRepository) + +# [{ :role => "guest", +# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }], +# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] }, +# { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }] +``` + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.