bmg 0.18.4 → 0.18.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bmg/error.rb +3 -0
- data/lib/bmg/reader/csv.rb +1 -1
- data/lib/bmg/summarizer.rb +17 -10
- data/lib/bmg/summarizer/by_proc.rb +1 -1
- data/lib/bmg/summarizer/value_by.rb +62 -0
- data/lib/bmg/version.rb +1 -1
- data/lib/bmg/writer/csv.rb +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6569e9038c83dda887da734d97ef58f9ff6e94984c78e8eae993561cde3bbfcc
|
4
|
+
data.tar.gz: 85d65ca717992a132e94ae17dbdf2cc671a161c495eb5be240c3d4e342e90c3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbeb5215e5942626dd5115d95c091602daa57128014bc0833c38c3460697ef9dbc59b4ccd7566d4d3604e8d7a8c5e7e4812159a5bb89d3dad477e6a7b75545d8
|
7
|
+
data.tar.gz: 9e8a9eaaf3699d28890cb15faef5b12360f9bf26c5bb0e9bf42ccedba0b7b5a3c8c2eb627f8b1a4e154c8d992426f47ed82153e9f110634f0ba6aa7ceddd1353
|
data/lib/bmg/error.rb
CHANGED
data/lib/bmg/reader/csv.rb
CHANGED
data/lib/bmg/summarizer.rb
CHANGED
@@ -95,16 +95,7 @@ module Bmg
|
|
95
95
|
# @param the current iterated tuple
|
96
96
|
# @return updated memo value
|
97
97
|
def happens(memo, tuple)
|
98
|
-
value =
|
99
|
-
when Proc
|
100
|
-
@functor.call(tuple)
|
101
|
-
when NilClass
|
102
|
-
tuple
|
103
|
-
when Symbol
|
104
|
-
tuple[@functor]
|
105
|
-
else
|
106
|
-
tuple[@functor]
|
107
|
-
end
|
98
|
+
value = extract_value(tuple)
|
108
99
|
_happens(memo, value)
|
109
100
|
end
|
110
101
|
|
@@ -143,6 +134,21 @@ module Bmg
|
|
143
134
|
self.class.name.downcase[/::([a-z]+)$/, 1].to_sym
|
144
135
|
end
|
145
136
|
|
137
|
+
protected
|
138
|
+
|
139
|
+
def extract_value(tuple)
|
140
|
+
value = case @functor
|
141
|
+
when Proc
|
142
|
+
@functor.call(tuple)
|
143
|
+
when NilClass
|
144
|
+
tuple
|
145
|
+
when Symbol
|
146
|
+
tuple[@functor]
|
147
|
+
else
|
148
|
+
tuple[@functor]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
146
152
|
end # class Summarizer
|
147
153
|
end # module Bmg
|
148
154
|
require_relative 'summarizer/count'
|
@@ -158,3 +164,4 @@ require_relative 'summarizer/distinct'
|
|
158
164
|
require_relative 'summarizer/concat'
|
159
165
|
require_relative 'summarizer/by_proc'
|
160
166
|
require_relative 'summarizer/multiple'
|
167
|
+
require_relative 'summarizer/value_by'
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Bmg
|
2
|
+
class Summarizer
|
3
|
+
#
|
4
|
+
# ValueBy summarizer.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# # direct ruby usage
|
9
|
+
# Bmg::Summarizer.value_by(:qty, :by => :serie).summarize(...)
|
10
|
+
#
|
11
|
+
class ValueBy < Summarizer
|
12
|
+
|
13
|
+
DEFAULT_OPTIONS = {
|
14
|
+
:symbolize => false
|
15
|
+
}
|
16
|
+
|
17
|
+
# Returns {} as least value.
|
18
|
+
def least
|
19
|
+
{}
|
20
|
+
end
|
21
|
+
|
22
|
+
# Collects the value
|
23
|
+
def happens(memo, tuple)
|
24
|
+
by = tuple[options[:by]]
|
25
|
+
by = by.to_sym if by && options[:symbolize]
|
26
|
+
misuse!(tuple, memo) if memo.has_key?(by)
|
27
|
+
memo.tap{|m|
|
28
|
+
m[by] = extract_value(tuple)
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
# Finalizes the computation.
|
33
|
+
def finalize(memo)
|
34
|
+
default_tuple.merge(memo)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def default_tuple
|
40
|
+
(options[:series] || []).each_with_object({}){|s,ss|
|
41
|
+
s_def = options[:default]
|
42
|
+
s_def = s_def.to_sym if s_def && options[:symbolize]
|
43
|
+
ss[s] = s_def
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def misuse!(tuple, memo)
|
48
|
+
msg = "Summarizer.value_by: summarization key + the serie must form be a candidate key"
|
49
|
+
msg += "\n"
|
50
|
+
msg += " Tuple: #{tuple.inspect}"
|
51
|
+
msg += " Memo: #{memo.inspect}"
|
52
|
+
raise MisuseError, msg
|
53
|
+
end
|
54
|
+
|
55
|
+
end # class ValueBy
|
56
|
+
|
57
|
+
def self.value_by(*args, &bl)
|
58
|
+
ValueBy.new(*args, &bl)
|
59
|
+
end
|
60
|
+
|
61
|
+
end # class Summarizer
|
62
|
+
end # module Bmg
|
data/lib/bmg/version.rb
CHANGED
data/lib/bmg/writer/csv.rb
CHANGED
@@ -19,7 +19,8 @@ module Bmg
|
|
19
19
|
relation.each do |tuple|
|
20
20
|
if csv.nil?
|
21
21
|
headers = infer_headers(tuple) if headers.nil?
|
22
|
-
|
22
|
+
csv_opts = csv_options.merge(headers: headers)
|
23
|
+
csv = CSV.new(string_or_io, **csv_opts)
|
23
24
|
end
|
24
25
|
csv << headers.map{|h| tuple[h] }
|
25
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bmg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.18.
|
4
|
+
version: 0.18.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: predicate
|
@@ -276,6 +276,7 @@ files:
|
|
276
276
|
- lib/bmg/summarizer/percentile.rb
|
277
277
|
- lib/bmg/summarizer/stddev.rb
|
278
278
|
- lib/bmg/summarizer/sum.rb
|
279
|
+
- lib/bmg/summarizer/value_by.rb
|
279
280
|
- lib/bmg/summarizer/variance.rb
|
280
281
|
- lib/bmg/support.rb
|
281
282
|
- lib/bmg/support/keys.rb
|