rubualizations 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWYwZjYzMTMzYzg5MzFjNTkzZDFiZDg4ZTY3NzkwMjIyNGU4ODdmZA==
5
+ data.tar.gz: !binary |-
6
+ NWIwMWMzYTMzODg4MWJlNjNjZDUxZGE2NWJhNDNiNjdiYTdmNjIwMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ N2UyNjU4M2U4NjI4ZjRjYzVlNzlmN2MzNWQyYWU3ZGUwOGQxOGFjYjQyYTgy
10
+ YTg0N2JjZTkwMDI0ZTYyY2NjNGY5ZDRlMDBkYzkxOWJiMWY2YWZmNzdhYzcy
11
+ ZWEwYWZiNTBlNjVmNDc1N2NmYTBlNGJkZGM1NWJiYmE5MDU0NGI=
12
+ data.tar.gz: !binary |-
13
+ OTc4MzE3Y2UxYWZhMDBmZGJmZjgxZWEyMDdlNDJhOGQ4ODViMTNhMzY0M2Y4
14
+ Y2IzNDEyODhhMWVmZGEwYmUzZDA3YmUyYTc1YzgzYmE4NzY5ZDMxMWEyMTE0
15
+ NmViZTA3NTJmNTNmN2YzYzViMWUwYmVmM2UyNWFmMDc5NmM2MDY=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = GoogleVisualizations
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Rubualizations'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,85 @@
1
+ module Rubualizations
2
+ module Tableable
3
+
4
+ attr_accessor :cols_def
5
+
6
+ def tablize_hashes(hashes, params)
7
+ if params[:cols].to_s == ""
8
+ cols = @cols_def.keys
9
+ else
10
+ cols = params[:cols].delete(" ").split(",")
11
+ end
12
+ s_hashes = hashes.sort_by {|h| h[cols[0]]}
13
+ if params[:type] == "discrete"
14
+ return self.discrete_with_cols(s_hashes, cols)
15
+ else
16
+ return self.continuous_with_cols(s_hashes, cols)
17
+ end
18
+ end
19
+
20
+
21
+ def discrete_with_cols(hashes, cols)
22
+ key = cols[0]
23
+ date_key, date_unit = key.split "__"
24
+ prev_hash = nil
25
+ hashes.delete_if do |h|
26
+ h[key] = format_datetime_mmyy(h[date_key]) unless date_unit.nil?
27
+ if !prev_hash.nil? and h[key].eql? prev_hash[key]
28
+ prev_hash.merge!(h) do |k,o,n|
29
+ if k.eql?(key) or !n.respond_to?("+") then n
30
+ else n+o end
31
+ end
32
+ else
33
+ prev_hash = h
34
+ false
35
+ end
36
+ end
37
+ return self.hashes_to_table(hashes, cols)
38
+ end
39
+
40
+ def continuous_with_cols(hashes, cols)
41
+ key = cols[0]
42
+ cols.each do |c|
43
+ cumul_col = c[/cumulation\((.*)\)/, 1]
44
+ cumulation = 0
45
+ hashes.map do |h|
46
+ h[key] = format_datetime_continuous(h[key]) if c.eql?(key)
47
+ unless cumul_col.nil?
48
+ @cols_def[c] = self.cumul_col_def cumul_col
49
+ h[c] = cumulation += h[cumul_col]
50
+ end
51
+ end
52
+ end
53
+ return self.hashes_to_table(hashes, cols)
54
+ end
55
+
56
+ def format_datetime_mmyy(datetime)
57
+ datetime.strftime "%m/%y"
58
+ end
59
+
60
+ def format_datetime_continuous(datetime)
61
+ "Date(#{datetime.strftime('%Y')}, #{datetime.strftime('%-m').to_i - 1}, #{datetime.strftime('%-d')}, #{datetime.strftime('%k')}, #{datetime.strftime('%M')}, #{datetime.strftime('%S')})"
62
+ end
63
+
64
+ def cumul_col_def(col)
65
+ {id: "cumulation(col)", label:"#{col_label(col)} Cumulation", type: "number"}
66
+ end
67
+
68
+ def hashes_to_table(hashes, cols)
69
+ table = { cols: @cols_def.values_at(*cols), rows:[]}
70
+ hashes.each do |hash|
71
+ row = {}
72
+ row[:c] = hash.values_at(*cols).map{|v| {v:v}}
73
+ table[:rows] << row
74
+ end
75
+ return table
76
+ end
77
+
78
+ def col_label(value)
79
+ values = value.split('_')
80
+ values.each { |v| v.downcase! }
81
+ values[0].capitalize!
82
+ return values.join " "
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module Rubualizations
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "test/unit"
2
+ require "./lib/rubualizations"
3
+
4
+ class RubualizationsTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @mock = Object.new
8
+ @mock.extend(Rubualizations::Tableable)
9
+ @mock.cols_def = {
10
+ "dateplaced" => {id:"A", label: "Date Placed", type: "datetime"},
11
+ "profit" => {id:"B", label: "Profit", type: "number"}
12
+ }
13
+ end
14
+
15
+ def test_tabelize_hashes
16
+ assert_equal @mock.tablize_hashes({}, {cols:"dateplaced,profit"}).to_s,
17
+ '{:cols=>[{:id=>"A", :label=>"Date Placed", :type=>"datetime"}, {:id=>"B", :label=>"Profit", :type=>"number"}], :rows=>[]}'
18
+ end
19
+
20
+ def test_discrete_with_cols
21
+ assert_equal @mock.discrete_with_cols({}, ["dateplaced", "profit"]).to_s,
22
+ '{:cols=>[{:id=>"A", :label=>"Date Placed", :type=>"datetime"}, {:id=>"B", :label=>"Profit", :type=>"number"}], :rows=>[]}'
23
+ end
24
+
25
+ def test_continuous_with_cols
26
+ assert_equal @mock.continuous_with_cols({}, ["dateplaced", "profit"]).to_s,
27
+ '{:cols=>[{:id=>"A", :label=>"Date Placed", :type=>"datetime"}, {:id=>"B", :label=>"Profit", :type=>"number"}], :rows=>[]}'
28
+ end
29
+
30
+ def test_format_datetime_mmyy
31
+ assert_equal @mock.format_datetime_mmyy(Time.new(2013, 06, 30)), "06/13"
32
+ end
33
+
34
+ def test_format_datetime_continuous
35
+ assert_equal @mock.format_datetime_continuous(Time.new(2013, 06, 30, 2, 2, 2)),
36
+ "Date(2013, 5, 30, 2, 02, 02)"
37
+ end
38
+
39
+ def test_cumul_col_def
40
+ assert_equal @mock.cumul_col_def("sport").to_s,
41
+ '{:id=>"cumulation(col)", :label=>"Sport Cumulation", :type=>"number"}'
42
+ end
43
+
44
+ def test_hashes_to_table
45
+ assert_equal @mock.hashes_to_table({}, ["dateplaced", "profit"]).to_s,
46
+ '{:cols=>[{:id=>"A", :label=>"Date Placed", :type=>"datetime"}, {:id=>"B", :label=>"Profit", :type=>"number"}], :rows=>[]}'
47
+ end
48
+
49
+ def test_col_label
50
+ assert_equal @mock.col_label("date_placed"), "Date placed"
51
+ end
52
+
53
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubualizations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - John Bower
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem that creates a google visualization data source from hashes passed
14
+ to it.
15
+ email:
16
+ - john@zode64.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/rubualizations/version.rb
22
+ - lib/rubualizations.rb
23
+ - MIT-LICENSE
24
+ - README.rdoc
25
+ - Rakefile
26
+ - test/rubualizations_test.rb
27
+ homepage: http://www.zode64.com
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Gem that creates a google visualization data source from hashes passed to
50
+ it.
51
+ test_files:
52
+ - test/rubualizations_test.rb