summaryse 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,14 @@
1
+ module Summaryse
2
+ module Version
3
+
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ TINY = 0
7
+
8
+ def self.to_s
9
+ [ MAJOR, MINOR, TINY ].join('.')
10
+ end
11
+
12
+ end
13
+ VERSION = Version.to_s
14
+ end
@@ -0,0 +1,112 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ describe "README file" do
3
+
4
+ it 'should provide correct examples' do
5
+
6
+ # :count, same as #size
7
+ [1, 4, 12, 7].summaryse(:count).should eq(4)
8
+
9
+ # :sum, same as #inject(:+)
10
+ [1, 4, 12, 7].summaryse(:sum).should eq(24)
11
+
12
+ # :avg, same as #inject(:+)/size
13
+ [1, 4, 12, 7].summaryse(:avg).should eq(6.0)
14
+
15
+ # :min, same as #min
16
+ [1, 4, 12, 7].summaryse(:min).should eq(1)
17
+
18
+ # :max, same as #max
19
+ [1, 4, 12, 7].summaryse(:max).should eq(12)
20
+
21
+ # :first, same as #first
22
+ [1, 4, 12, 7].summaryse(:first).should eq(1)
23
+
24
+ # :last, same as #last
25
+ [1, 4, 12, 7].summaryse(:last).should eq(7)
26
+
27
+ # :union, same as #inject(:|)
28
+ [ [1, 4], [12, 1, 7], [1] ].summaryse(:union).should eq([1, 4, 12, 7])
29
+
30
+ # :intersection, same as #inject(:&)
31
+ [ [1, 4], [12, 1, 7], [1] ].summaryse(:intersection).should eq([1])
32
+
33
+ [
34
+ { :hobbies => [:ruby], :size => 12 },
35
+ { :hobbies => [:music], :size => 17 }
36
+ ].summaryse(:hobbies => :union, :size => :max).should eq(
37
+ :hobbies => [:ruby, :music], :size => 17
38
+ )
39
+
40
+ [
41
+ { :hobbies => [:ruby], :size => 12 },
42
+ { :hobbies => [:music], :size => 17 }
43
+ ].summaryse(:hobbies => :union, nil => :first).should eq(
44
+ :hobbies => [:ruby, :music], :size => 12
45
+ )
46
+
47
+ [
48
+ { :hobbies => [:ruby], :size => 12 },
49
+ { :hobbies => [:music], :size => 17 }
50
+ ].summaryse(:hobbies => :union, :size => lambda{|a|
51
+ a.inject(:+).to_f
52
+ }).should eq(:hobbies => [:ruby, :music], :size => 29.0)
53
+
54
+ [
55
+ { :hobbies => [:ruby], :size => 12 },
56
+ { :hobbies => [:music], :size => 17 }
57
+ ].summaryse(:hobbies => :union, :size => lambda{|a|
58
+ a.join(', ')
59
+ }).should eq(:hobbies => [:ruby, :music], :size => "12, 17")
60
+
61
+ [
62
+ { :hobbies => {:day => [:ruby], :night => [:ruby] } },
63
+ { :hobbies => {:day => [], :night => [:sleep]} }
64
+ ].summaryse(:hobbies => {:day => :union, :night => :union}).should eq(
65
+ :hobbies => {:day => [:ruby], :night => [:ruby, :sleep]}
66
+ )
67
+
68
+ end
69
+
70
+ it 'should provide a correct integration example' do
71
+ # This is left.yaml
72
+ left = YAML.load <<-Y
73
+ hobbies:
74
+ - ruby
75
+ - rails
76
+ dependencies:
77
+ - {name: rspec, version: '2.6.4', for: [ runtime ]}
78
+ Y
79
+
80
+ # This is right.yaml
81
+ right = YAML.load <<-Y
82
+ hobbies:
83
+ - ruby
84
+ - music
85
+ dependencies:
86
+ - {name: rails, version: '3.0', for: [ runtime ]}
87
+ - {name: rspec, version: '2.6.4', for: [ test ]}
88
+ Y
89
+
90
+ # This is merge.yaml
91
+ merge = YAML.load <<-M
92
+ hobbies:
93
+ :union
94
+ dependencies:
95
+ - [name, version]
96
+ - for: :union
97
+ M
98
+
99
+ # Merge and re-dump
100
+ exp = YAML.load <<-Y
101
+ hobbies:
102
+ - ruby
103
+ - rails
104
+ - music
105
+ dependencies:
106
+ - {name: rspec, version: '2.6.4', for: [ runtime, test ]}
107
+ - {name: rails, version: '3.0', for: [ runtime ]}
108
+ Y
109
+ [ left, right ].summaryse(merge).should eq(exp)
110
+ end
111
+
112
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'summaryse'
@@ -0,0 +1,111 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ describe Summaryse do
3
+
4
+ it "should have a version number" do
5
+ Summaryse.const_defined?(:VERSION).should be_true
6
+ end
7
+
8
+ describe "when called with a Proc argument" do
9
+
10
+ let(:numbers){ [15, 3, 17, 4, 12] }
11
+
12
+ it "should simply call the proc with the array" do
13
+ numbers.summaryse(lambda{|a| "hello"}).should eq("hello")
14
+ end
15
+
16
+ end
17
+
18
+ describe "when called with a Symbol argument" do
19
+
20
+ let(:numbers){ [15, 3, 17, 4, 12] }
21
+ let(:arrays){ [[15, 3, 12], [17, 4, 12]] }
22
+
23
+ it "should recognize avg" do
24
+ numbers.summaryse(:avg).should eq((15 + 3 + 17 + 4 + 12)/5.0)
25
+ end
26
+
27
+ it "should recognize count" do
28
+ numbers.summaryse(:count).should eq(5)
29
+ arrays.summaryse(:count).should eq(2)
30
+ end
31
+
32
+ it "should recognize first" do
33
+ numbers.summaryse(:first).should eq(15)
34
+ end
35
+
36
+ it "should recognize intersection" do
37
+ arrays.summaryse(:intersection).should eq([12])
38
+ end
39
+
40
+ it "should recognize last" do
41
+ numbers.summaryse(:last).should eq(12)
42
+ end
43
+
44
+ it "should recognize min" do
45
+ numbers.summaryse(:min).should eq(3)
46
+ end
47
+
48
+ it "should recognize max" do
49
+ numbers.summaryse(:max).should eq(17)
50
+ end
51
+
52
+ it "should recognize sum" do
53
+ numbers.summaryse(:sum).should eq(15 + 3 + 17 + 4 + 12)
54
+ arrays.summaryse(:sum).should eq([ 15, 3, 12, 17, 4, 12])
55
+ end
56
+
57
+ it "should recognize union" do
58
+ arrays.summaryse(:union).should eq([15, 3, 12, 17, 4])
59
+ end
60
+
61
+ end # Symbol argument
62
+
63
+ describe "when called with a Hash argument" do
64
+
65
+ let(:rel){[
66
+ { :size => 12, :hobbies => [:ruby] },
67
+ { :size => 1, :hobbies => [:music] }
68
+ ]}
69
+
70
+ it "should allow simple sub-summarizations" do
71
+ control = { :size => :max, :hobbies => :union }
72
+ rel.summaryse(control).should eq(:size => 12, :hobbies => [:ruby, :music])
73
+ end
74
+
75
+ it "should not keep non summarysed arguments by default" do
76
+ control = {:size => :max}
77
+ rel.summaryse(control).should eq(:size => 12)
78
+ end
79
+
80
+ it "should allow specifying a default behavior" do
81
+ control = {:size => :max, nil => :union}
82
+ rel.summaryse(control).should eq(:size => 12, :hobbies => [:ruby, :music])
83
+ end
84
+
85
+ it "should support having inner Procs" do
86
+ control = {:size => lambda{|a| "hello"}}
87
+ rel.summaryse(control).should eq(:size => "hello")
88
+ end
89
+
90
+ end # Hash argument
91
+
92
+ describe "when called with an Array argument" do
93
+ let(:array){[
94
+ [ {:version => "1.9", :size => 16},
95
+ {:version => "1.8", :size => 12} ],
96
+ [ {:version => "2.0", :size => 99},
97
+ {:version => "1.8", :size => 10} ]
98
+ ]}
99
+
100
+ it "should union then summaryse while respecting by key order" do
101
+ control = [ [:version], {:size => :min} ]
102
+ array.summaryse(control).should eq([
103
+ {:version => "1.9", :size => 16},
104
+ {:version => "1.8", :size => 10},
105
+ {:version => "2.0", :size => 99}
106
+ ])
107
+ end
108
+
109
+ end
110
+
111
+ end
@@ -0,0 +1,190 @@
1
+ # We require your library, mainly to have access to the VERSION number.
2
+ # Feel free to set $version manually.
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
4
+ require "summaryse/version"
5
+ $version = Summaryse::Version.to_s
6
+
7
+ #
8
+ # This is your Gem specification. Default values are provided so that your library
9
+ # should be correctly packaged given what you have described in the .noespec file.
10
+ #
11
+ Gem::Specification.new do |s|
12
+
13
+ ################################################################### ABOUT YOUR GEM
14
+
15
+ # Gem name (required)
16
+ s.name = "summaryse"
17
+
18
+ # Gem version (required)
19
+ s.version = $version
20
+
21
+ # A short summary of this gem
22
+ #
23
+ # This is displayed in `gem list -d`.
24
+ s.summary = "Array#summaryse"
25
+
26
+ # A long description of this gem (required)
27
+ #
28
+ # The description should be more detailed than the summary. For example,
29
+ # you might wish to copy the entire README into the description.
30
+ s.description = "summaryse arrays with full power. Among others, this gem allows merging YAML\nconfiguration files,... "
31
+
32
+ # The URL of this gem home page (optional)
33
+ s.homepage = "http://github.com/blambeau/summaryse"
34
+
35
+ # Gem publication date (required but auto)
36
+ #
37
+ # Today is automatically used by default, uncomment only if
38
+ # you know what you do!
39
+ #
40
+ # s.date = Time.now.strftime('%Y-%m-%d')
41
+
42
+ # The license(s) for the library. Each license must be a short name, no
43
+ # more than 64 characters.
44
+ #
45
+ # s.licences = %w{}
46
+
47
+ # The rubyforge project this gem lives under (optional)
48
+ #
49
+ # s.rubyforge_project = nil
50
+
51
+ ################################################################### ABOUT THE AUTHORS
52
+
53
+ # The list of author names who wrote this gem.
54
+ #
55
+ # If you are providing multiple authors and multiple emails they should be
56
+ # in the same order.
57
+ #
58
+ s.authors = ["Bernard Lambeau"]
59
+
60
+ # Contact emails for this gem
61
+ #
62
+ # If you are providing multiple authors and multiple emails they should be
63
+ # in the same order.
64
+ #
65
+ # NOTE: Somewhat strangly this attribute is always singular!
66
+ # Don't replace by s.emails = ...
67
+ s.email = ["blambeau@gmail.com"]
68
+
69
+ ################################################################### PATHS, FILES, BINARIES
70
+
71
+ # Paths in the gem to add to $LOAD_PATH when this gem is
72
+ # activated (required).
73
+ #
74
+ # The default 'lib' is typically sufficient.
75
+ s.require_paths = ["lib"]
76
+
77
+ # Files included in this gem.
78
+ #
79
+ # By default, we take all files included in the Manifest.txt file on root
80
+ # of the project. Entries of the manifest are interpreted as Dir[...]
81
+ # patterns so that lazy people may use wilcards like lib/**/*
82
+ #
83
+ here = File.expand_path(File.dirname(__FILE__))
84
+ s.files = File.readlines(File.join(here, 'Manifest.txt')).
85
+ inject([]){|files, pattern| files + Dir[File.join(here, pattern.strip)]}.
86
+ collect{|x| x[(1+here.size)..-1]}
87
+
88
+ # Test files included in this gem.
89
+ #
90
+ s.test_files = Dir["test/**/*"] + Dir["spec/**/*"]
91
+
92
+ # The path in the gem for executable scripts (optional)
93
+ #
94
+ s.bindir = "bin"
95
+
96
+ # Executables included in the gem.
97
+ #
98
+ s.executables = (Dir["bin/*"]).collect{|f| File.basename(f)}
99
+
100
+ ################################################################### REQUIREMENTS & INSTALL
101
+ # Remember the gem version requirements operators and schemes:
102
+ # = Equals version
103
+ # != Not equal to version
104
+ # > Greater than version
105
+ # < Less than version
106
+ # >= Greater than or equal to
107
+ # <= Less than or equal to
108
+ # ~> Approximately greater than
109
+ #
110
+ # Don't forget to have a look at http://lmgtfy.com/?q=Ruby+Versioning+Policies
111
+ # for setting your gem version.
112
+ #
113
+ # For your requirements to other gems, remember that
114
+ # ">= 2.2.0" (optimistic: specify minimal version)
115
+ # ">= 2.2.0", "< 3.0" (pessimistic: not greater than the next major)
116
+ # "~> 2.2" (shortcut for ">= 2.2.0", "< 3.0")
117
+ # "~> 2.2.0" (shortcut for ">= 2.2.0", "< 2.3.0")
118
+ #
119
+
120
+ #
121
+ # One call to add_dependency('gem_name', 'gem version requirement') for each
122
+ # runtime dependency. These gems will be installed with your gem.
123
+ # One call to add_development_dependency('gem_name', 'gem version requirement')
124
+ # for each development dependency. These gems are required for developers
125
+ #
126
+ s.add_development_dependency("rake", "~> 0.9.2")
127
+ s.add_development_dependency("bundler", "~> 1.0")
128
+ s.add_development_dependency("rspec", "~> 2.4.0")
129
+ s.add_development_dependency("yard", "~> 0.7.2")
130
+ s.add_development_dependency("bluecloth", "~> 2.0.9")
131
+ s.add_development_dependency("wlang", "~> 0.10.1")
132
+
133
+
134
+ # The version of ruby required by this gem
135
+ #
136
+ # Uncomment and set this if your gem requires specific ruby versions.
137
+ #
138
+ # s.required_ruby_version = ">= 0"
139
+
140
+ # The RubyGems version required by this gem
141
+ #
142
+ # s.required_rubygems_version = ">= 0"
143
+
144
+ # The platform this gem runs on. See Gem::Platform for details.
145
+ #
146
+ # s.platform = nil
147
+
148
+ # Extensions to build when installing the gem.
149
+ #
150
+ # Valid types of extensions are extconf.rb files, configure scripts
151
+ # and rakefiles or mkrf_conf files.
152
+ #
153
+ s.extensions = []
154
+
155
+ # External (to RubyGems) requirements that must be met for this gem to work.
156
+ # It’s simply information for the user.
157
+ #
158
+ s.requirements = nil
159
+
160
+ # A message that gets displayed after the gem is installed
161
+ #
162
+ # Uncomment and set this if you want to say something to the user
163
+ # after gem installation
164
+ #
165
+ s.post_install_message = nil
166
+
167
+ ################################################################### SECURITY
168
+
169
+ # The key used to sign this gem. See Gem::Security for details.
170
+ #
171
+ # s.signing_key = nil
172
+
173
+ # The certificate chain used to sign this gem. See Gem::Security for
174
+ # details.
175
+ #
176
+ # s.cert_chain = []
177
+
178
+ ################################################################### RDOC
179
+
180
+ # An ARGV style array of options to RDoc
181
+ #
182
+ # See 'rdoc --help' about this
183
+ #
184
+ s.rdoc_options = []
185
+
186
+ # Extra files to add to RDoc such as README
187
+ #
188
+ s.extra_rdoc_files = Dir["README.md"] + Dir["CHANGELOG.md"] + Dir["LICENCE.md"]
189
+
190
+ end
@@ -0,0 +1,28 @@
1
+ # Noe template for ruby gem libraries (https://github.com/blambeau/noe) - short version
2
+ # Run 'noe show-spec' and 'noe help show-spec' for additional details.
3
+ template-info:
4
+ name: "ruby"
5
+ version: 1.3.0
6
+ variables:
7
+ lower:
8
+ summaryse
9
+ upper:
10
+ summaryse
11
+ version:
12
+ 1.0.0
13
+ summary: |-
14
+ Array#summaryse
15
+ description: |-
16
+ summaryse arrays with full power. Among others, this gem allows merging YAML
17
+ configuration files,...
18
+ authors:
19
+ - { name: "Bernard Lambeau", email: blambeau@gmail.com }
20
+ links:
21
+ - http://github.com/blambeau/summaryse
22
+ dependencies:
23
+ - {name: rake, version: "~> 0.9.2", groups: [development]}
24
+ - {name: bundler, version: "~> 1.0", groups: [development]}
25
+ - {name: rspec, version: "~> 2.4.0", groups: [development]}
26
+ - {name: yard, version: "~> 0.7.2", groups: [development]}
27
+ - {name: bluecloth, version: "~> 2.0.9", groups: [development]}
28
+ - {name: wlang, version: "~> 0.10.1", groups: [development]}