rubypivot 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2976ecf2bef2b1fcc45d0044cbf553ab7b07e0ee8ecd48aca334f13836399016
4
+ data.tar.gz: 92c6cf0c117a8dbbff68b55a72c760bae6bc62992b1ca7744e9bfe35ebc731eb
5
+ SHA512:
6
+ metadata.gz: 9575159c5670c6831c9777e98bede1a93e19347f3b9ae6d3f9a848450d78029dc3810323857de554d98d0fa5a72edfd5930173fc049c73b8ee0b28a00b9c4e7c
7
+ data.tar.gz: a9a32709eec385451e3bd1398e412cc874e7020daf02ad25ca85ed4905606c15ee300e4884dc8a9be906b0da8188d6ec5bb498cd2c3d180b6e90ad22cd5fd4f7
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ /SetupMemo.md
11
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rubypivot.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 utsumi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # Rubypivot
2
+
3
+ Rubypivot is a tool to make pivot table arrays for ruby.
4
+ It transforming a dataset or array of hashes into a spreadsheet-style array.
5
+
6
+ Suggestions and pull requests are welcome.
7
+
8
+ ## Installation
9
+
10
+ ```
11
+ gem install rubypivot
12
+ ```
13
+ Not yet ready, please clone the repository for the moment.
14
+ ## Usage
15
+
16
+ ```ruby
17
+ require "rubypivot"
18
+ pivot = Rubypivot::Pivot.new(source_data, :month, :name, :value, data_type: :integer)
19
+ pivot.build.each do |line|
20
+ p line
21
+ end
22
+ ```
23
+ See sample scripts in examples folder.
24
+
25
+ Supported data aggregation is only SUM for numeric values.
26
+
27
+ Total calculation supported.
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
32
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubypivot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ #
4
+ # Active record example
5
+ #
6
+ APP_ROOT = File.dirname(__FILE__)
7
+ $LOAD_PATH << "#{APP_ROOT}/../lib"
8
+ require "rubypivot"
9
+
10
+ MONTHS = {
11
+ 1 => 'Jan',
12
+ 2 => 'Feb',
13
+ 3 => 'Mar',
14
+ }
15
+ NAMES = {
16
+ 1 => 'Wendy',
17
+ 2 => 'John',
18
+ 3 => 'David',
19
+ }
20
+
21
+ class ActiveRecordLikeRecord
22
+ def initialize(data) @data = data; end
23
+ def method_missing(name, *_args) @data[name.to_sym]; end
24
+ end
25
+
26
+ class ActiveRecordLike
27
+ SOURCE_DATA = [
28
+ { month: 1, name: 1, value: 33},
29
+ { month: 2, name: 1, value: 45},
30
+ { month: 2, name: 1, value: 55},
31
+ { month: 1, name: 2, value: 123},
32
+ { month: 2, name: 2, value: 23},
33
+ { month: 1, name: 3, value: 3},
34
+ ]
35
+ def self.all
36
+ res = []
37
+ SOURCE_DATA.each do |data|
38
+ res << ActiveRecordLikeRecord.new(data)
39
+ end
40
+ res
41
+ end
42
+ end
43
+
44
+ records = ActiveRecordLike.all
45
+ pivot = Rubypivot::Pivot.new(records, :month, :name, :value)
46
+ pivot.options[:data_type] = :integer
47
+ pivot.options[:column_lookup] = MONTHS
48
+ pivot.options[:row_lookup] = NAMES
49
+ pivot.build.each do |line|
50
+ p line
51
+ end
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ #
4
+ # Lookup example : column/row header can lookup hash table
5
+ #
6
+ APP_ROOT = File.dirname(__FILE__)
7
+ $LOAD_PATH << "#{APP_ROOT}/../lib"
8
+ require "rubypivot"
9
+
10
+ MONTHS = {
11
+ 1 => 'Jan',
12
+ 2 => 'Feb',
13
+ 3 => 'Mar',
14
+ }
15
+ NAMES = {
16
+ 1 => 'Wendy',
17
+ 2 => 'John',
18
+ 3 => 'David',
19
+ }
20
+ source_data = [
21
+ { month: 1, name: 1, value: 33},
22
+ { month: 2, name: 1, value: 45},
23
+ { month: 2, name: 1, value: 55},
24
+ { month: 1, name: 2, value: 123},
25
+ { month: 2, name: 2, value: 23},
26
+ { month: 1, name: 3, value: 3},
27
+ ]
28
+
29
+ pivot = Rubypivot::Pivot.new(source_data, :month, :name, :value)
30
+ pivot.options[:data_type] = :integer # or :float or :string or nil for raw data
31
+ pivot.options[:column_lookup] = MONTHS
32
+ pivot.options[:row_lookup] = NAMES
33
+ pivot.options[:row_total] = 'Personal Total'
34
+ # pivot.options[:header] = false
35
+ # pivot.options[:row_header] = false
36
+ pivot.build.each do |line|
37
+ p line
38
+ end
39
+ puts "------------"
40
+ p pivot.total_row('Total')
41
+
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ #
4
+ # Simple example
5
+ #
6
+ APP_ROOT = File.dirname(__FILE__)
7
+ $LOAD_PATH << "#{APP_ROOT}/../lib"
8
+ require "rubypivot"
9
+
10
+ source_data = [
11
+ { month: 'Jan', name: 'Wendy', value: 33},
12
+ { month: 'Feb', name: 'Wendy', value: 45},
13
+ { month: 'Feb', name: 'Wendy', value: 55},
14
+ { month: 'Jan', name: 'John', value: 123},
15
+ { month: 'Feb', name: 'John', value: 23},
16
+ { month: 'Jan', name: 'David', value: 3},
17
+ ]
18
+
19
+ pivot = Rubypivot::Pivot.new(source_data, :month, :name, :value, data_type: :integer)
20
+ pivot.column_titles = ['Jan', 'Feb', 'Mar']
21
+ # pivot.options[:header] = true
22
+ # pivot.options[:row_header] = false
23
+ # pivot.column_titles.sort!
24
+ # p pivot.column_titles
25
+ # p pivot.row_titles
26
+ pivot.build.each do |line|
27
+ p line
28
+ end
29
+ puts "------------"
30
+ p pivot.total_row('Total')
31
+
32
+ #["", "Jan", "Feb", "Mar"]
33
+ #["David", 3, nil, nil]
34
+ #["John", 123, 23, nil]
35
+ #["Wendy", 33, 100, nil]
@@ -0,0 +1,9 @@
1
+ # require "rubypivot/version"
2
+ require "rubypivot/pivot"
3
+ require "rubypivot/pivot_row"
4
+
5
+ module Rubypivot
6
+ VERSION = "0.0.1" # Pre-release
7
+
8
+ class PivotError < StandardError; end
9
+ end
@@ -0,0 +1,149 @@
1
+ module Rubypivot
2
+
3
+ class Pivot
4
+ DEFAULT_OPTIONS = {
5
+ data_type: :integer, # :integer, :float or :string
6
+ column_sort: true,
7
+ row_sort: true,
8
+ header: true,
9
+ row_header: true,
10
+ # column_lookup: HashName
11
+ # row_lookup: HashName
12
+ # :row_total: 'Title for total column'
13
+ }.freeze
14
+
15
+ attr_accessor :options
16
+ def initialize(source_data, column_name, row_name, value_name, options = {})
17
+ @options = DEFAULT_OPTIONS.dup
18
+ @options.merge! options if options
19
+ raise PivotError, "Source Data must be an array of [hash|dataset]." if !source_data.is_a?(Array)
20
+ raise PivotError, "Column name must be specified." if column_name.nil?
21
+ raise PivotError, "Row name must be specified." if row_name.nil?
22
+ raise PivotError, "Value name must be specified." if value_name.nil?
23
+ # TODO: error checks for options
24
+ @column_name = column_name.to_sym
25
+ @row_name = row_name.to_sym
26
+ @value_name = value_name.to_sym
27
+ @source_data = source_data
28
+ #
29
+ @rows_parsed = nil # holds hash of DataRow instances
30
+ @column_titles = nil # array of column titles
31
+ @row_titles = nil # array of row titles
32
+ end
33
+
34
+ # Column title can be predefined by caller
35
+ def column_titles= (new_array)
36
+ raise PivotError, "Column title must be an array" unless new_array.is_a?(Array)
37
+ @column_titles = new_array
38
+ end
39
+ # Scan source data and buide titles array
40
+ def column_titles
41
+ @column_titles ||= make_column_list
42
+ end
43
+ # Row title can be predefined by caller
44
+ def row_titles= (new_array)
45
+ raise PivotError, "Row title must be an array" unless new_array.is_a?(Array)
46
+ @row_titles = new_array
47
+ end
48
+ # Scan source data and buide titles array
49
+ def row_titles
50
+ @row_titles || make_row_list
51
+ end
52
+
53
+ def make_column_list
54
+ @column_titles = [] unless @column_titles
55
+ @source_data.each do |each_line|
56
+ title = Pivot.get_title(@column_name, each_line)
57
+ @column_titles << title unless @column_titles.include?(title)
58
+ end
59
+ @column_titles.sort! if @options[:column_sort]
60
+ @column_titles
61
+ end
62
+
63
+ def make_row_list
64
+ @row_titles = [] unless @row_titles
65
+ @source_data.each do |each_line|
66
+ title = Pivot.get_title(@row_name, each_line)
67
+ @row_titles << title unless @row_titles.include?(title)
68
+ end
69
+ @row_titles.sort! if @options[:row_sort]
70
+ @row_titles
71
+ end
72
+
73
+ def parse_data
74
+ make_column_list unless @column_titles
75
+ make_row_list unless @row_titles
76
+ @rows_parsed = PivotRows.new(@options)
77
+ @source_data.each do |each_line|
78
+ column_title = Pivot.get_title(@column_name, each_line)
79
+ row_title = Pivot.get_title(@row_name, each_line)
80
+ value = Pivot.get_value(@value_name, each_line)
81
+ row = @rows_parsed.get_row(row_title) # create a new row if not exists
82
+ row.add(column_title, value)
83
+ end
84
+ self
85
+ end
86
+
87
+ def column_header
88
+ res = []
89
+ res << '' if @options[:row_header]
90
+ if @options[:column_lookup]
91
+ @column_titles.each do |column|
92
+ res << @options[:column_lookup][column]
93
+ end
94
+ else
95
+ res += @column_titles
96
+ end
97
+ res << @options[:row_total] if @options[:row_total]
98
+ res
99
+ end
100
+
101
+ # return an pivot array
102
+ def build
103
+ parse_data unless @rows_parsed
104
+ res = []
105
+ res << column_header if @options[:header]
106
+ @row_titles.each do |row_title|
107
+ row = @rows_parsed.get_row(row_title)
108
+ data_array = []
109
+ data_array << @rows_parsed.header(row_title) if @options[:row_header]
110
+ data_array += row.to_a(column_titles)
111
+ data_array << row.total(column_titles) if @options[:row_total]
112
+ res << data_array
113
+ end
114
+ res
115
+ end
116
+
117
+ def total_row(title = nil)
118
+ parse_data unless @rows_parsed
119
+ # title: message at title row, third(3) param is grand total true/false
120
+ res = []
121
+ res << title if @options[:row_header]
122
+ res += @rows_parsed.total(@column_titles, @options[:row_total])
123
+ end
124
+
125
+ def self.build(data, column_name, row_name, data_name, options = {})
126
+ obj = self.new(data, column_name, row_name, data_name, options)
127
+ obj.build
128
+ end
129
+ # get column title or row title
130
+ def self.get_title(name, line)
131
+ if line.is_a?(Hash)
132
+ res = line[name]
133
+ else
134
+ res = line.send(name)
135
+ end
136
+ res = 'Empty' if res.nil?
137
+ res
138
+ end
139
+
140
+ def self.get_value(name, line)
141
+ if line.is_a?(Hash)
142
+ line[name]
143
+ else
144
+ line.send(name)
145
+ end
146
+ end
147
+
148
+ end
149
+ end
@@ -0,0 +1,5 @@
1
+ # Not used at this moment
2
+ module Rubypivot
3
+ class PivotColumn
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ module Rubypivot
2
+ class PivotRows
3
+ attr_reader :rows, :data_type
4
+ def initialize(options = {})
5
+ @options = options
6
+ @data_type = options[:data_type]
7
+ @lookup = options[:row_lookup]
8
+ @rows = {}
9
+ end
10
+
11
+ def get_row(row_title)
12
+ @rows[row_title] ||= PivotRow.new(row_title, @data_type)
13
+ end
14
+ alias :add_row :get_row
15
+
16
+ def header(row_title)
17
+ @lookup ? @lookup[row_title] : row_title
18
+ end
19
+
20
+ def total(column_titles = [], show_grand_total = false)
21
+ return ['Total', 'row', 'can', 'not', 'create', "type :#{@data_type}"] unless [:integer, :float].include?(@data_type)
22
+ grand_total = @data_type == :float ? 0.0 : 0
23
+ data_array = []
24
+ column_titles.each do |column_title|
25
+ total = @data_type == :float ? 0.0 : 0
26
+ @rows.each do |row_title, row|
27
+ v = row.get(column_title)
28
+ total += v if v
29
+ end
30
+ grand_total += total if show_grand_total
31
+ data_array << total
32
+ end
33
+ data_array << grand_total if show_grand_total
34
+ data_array
35
+ end
36
+ end
37
+
38
+ class PivotRow
39
+ attr_reader :title
40
+ def initialize(title, data_type = nil)
41
+ @title = title # Title of the row : String
42
+ @data_type = data_type
43
+ @data = {}
44
+ end
45
+
46
+ def add(column_title, value)
47
+ return unless value
48
+ case @data_type
49
+ when :integer
50
+ @data[column_title] = 0 unless @data[column_title]
51
+ @data[column_title] += value.to_i
52
+ when :float
53
+ @data[column_title] = 0.0 unless @data[column_title]
54
+ @data[column_title] += value.to_f
55
+ when :string
56
+ @data[column_title] = value.to_s
57
+ else # raw data
58
+ @data[column_title] = value
59
+ end
60
+ end
61
+
62
+ def get(column_title)
63
+ return nil if column_title.nil?
64
+ @data[column_title]
65
+ end
66
+
67
+ def to_a(column_titles)
68
+ column_titles.map{|column_title| @data[column_title] }
69
+ end
70
+
71
+ def total(column_titles)
72
+ return unless [:integer, :float].include?(@data_type)
73
+ total = @data_type == :float ? 0.0 : 0
74
+ column_titles.each do |column_title|
75
+ total += @data[column_title] if @data[column_title]
76
+ end
77
+ total
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module Rubypivot
2
+ VERSION = "0.0.1" # Pre-release
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/rubypivot/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rubypivot"
5
+ spec.version = Rubypivot::VERSION
6
+ spec.authors = ["hiro utsumi"]
7
+ spec.email = ["gambaldia@gmail.com"]
8
+
9
+ spec.summary = "A tool to make pivot table arrays for ruby"
10
+ spec.description = "Transforming a dataset or array of hashes into a spreadsheet-style array"
11
+ spec.homepage = "https://github.com/gambldia/rubypivot"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ # spec.metadata["changelog_uri"] = ""
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubypivot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hiro utsumi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Transforming a dataset or array of hashes into a spreadsheet-style array
14
+ email:
15
+ - gambaldia@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - SetupMemo.md
26
+ - bin/console
27
+ - bin/setup
28
+ - examples/active_record_like.rb
29
+ - examples/lookup.rb
30
+ - examples/simple.rb
31
+ - lib/rubypivot.rb
32
+ - lib/rubypivot/pivot.rb
33
+ - lib/rubypivot/pivot_column.rb
34
+ - lib/rubypivot/pivot_row.rb
35
+ - lib/rubypivot/version.rb
36
+ - rubypivot.gemspec
37
+ homepage: https://github.com/gambldia/rubypivot
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/gambldia/rubypivot
42
+ source_code_uri: https://github.com/gambldia/rubypivot
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.3.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.2.3
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A tool to make pivot table arrays for ruby
62
+ test_files: []