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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e208e1eb958222f5ffac079296fc7279f221dddf6ed8165f5004b0a90b08c4b
4
- data.tar.gz: 6e9e2509ae9ce0d277d226d55d29274b895a6b31dabfe9ac387d4be7fd97be4a
3
+ metadata.gz: 6569e9038c83dda887da734d97ef58f9ff6e94984c78e8eae993561cde3bbfcc
4
+ data.tar.gz: 85d65ca717992a132e94ae17dbdf2cc671a161c495eb5be240c3d4e342e90c3f
5
5
  SHA512:
6
- metadata.gz: d142fd93326193529359a02c9e14936ffc43dd52ceab4b44e7d890b82cc0df4561e555002f0e6c12e53f6956a4541140be5214a40745ac54e1dc046bdbd4b735
7
- data.tar.gz: 6fbbd1ad533a0beee8faf783e07c7cedcc2535ef9edf1b601f973a30a513e5ee6d00f0031c8486f4460ccb912acdbad623c86ca649d4664fdc24c63c3f5de693
6
+ metadata.gz: fbeb5215e5942626dd5115d95c091602daa57128014bc0833c38c3460697ef9dbc59b4ccd7566d4d3604e8d7a8c5e7e4812159a5bb89d3dad477e6a7b75545d8
7
+ data.tar.gz: 9e8a9eaaf3699d28890cb15faef5b12360f9bf26c5bb0e9bf42ccedba0b7b5a3c8c2eb627f8b1a4e154c8d992426f47ed82153e9f110634f0ba6aa7ceddd1353
data/lib/bmg/error.rb CHANGED
@@ -16,4 +16,7 @@ module Bmg
16
16
  # while unknown
17
17
  class UnknownAttributesError < Error; end
18
18
 
19
+ # Raised when an operator is badly used
20
+ class MisuseError < Error; end
21
+
19
22
  end
@@ -23,7 +23,7 @@ module Bmg
23
23
  return to_enum unless block_given?
24
24
  require 'csv'
25
25
  with_io do |io|
26
- ::CSV.new(io, csv_options).each do |row|
26
+ ::CSV.new(io, **csv_options).each do |row|
27
27
  yield tuple(row)
28
28
  end
29
29
  end
@@ -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 = case @functor
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'
@@ -16,7 +16,7 @@ module Bmg
16
16
  end
17
17
 
18
18
  # Returns [] as least value.
19
- def least()
19
+ def least
20
20
  @least
21
21
  end
22
22
 
@@ -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
@@ -2,7 +2,7 @@ module Bmg
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 18
5
- TINY = 4
5
+ TINY = 5
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -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
- csv = CSV.new(string_or_io, csv_options.merge(headers: headers))
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
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-05-11 00:00:00.000000000 Z
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