bmg 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6fe9a06a0c6fbf5f5f0523b3ade5d59ac077c3b
4
- data.tar.gz: 3d0dd15b128fe42a6eb603b922de1c8889d266c8
3
+ metadata.gz: bc75e7b5602e1f76e3581009746302b3a546f3f8
4
+ data.tar.gz: 5dee61974de7699b5ef4957cd2714d5d241df830
5
5
  SHA512:
6
- metadata.gz: e608c650a868dc5d4870a6847675dbae8742525a53e255732c6634c85b911f31528d7a57cbd580eb74c3f85610590abfb0e792532a71dee23cf61b20e15aa713
7
- data.tar.gz: 1b082e58ffce43e55e85ff36753b0d2d5e579e947a44010dd85a839e82ca4f6a371cb882ae32bc6b34e159936e0f21e1e8f8e944ef4ada4d43ff92ee48fa9b3d
6
+ metadata.gz: cdad32c048da1719f153a16f355a79b13cd68cd18cf017c47041b7d73992f18c19cbdf9c5c32cf366e8801a0ac98a0ef84237120a7f8e59e9d3274feecd8a715
7
+ data.tar.gz: 28e09b93f01a6aaa0c8e95adba0bf8b417dbf501e9b5b221c8593a5f53f9cdebe04fb636b7fe641ddf41ba3ff6a706de0ab6c9d4c70b70f49f6a9c055e3041cd
data/lib/bmg.rb CHANGED
@@ -13,6 +13,7 @@ module Bmg
13
13
 
14
14
  end
15
15
  require_relative 'bmg/version'
16
+ require_relative 'bmg/error'
16
17
  require_relative 'bmg/operator'
17
18
  require_relative 'bmg/relation'
18
19
  require_relative 'bmg/reader'
@@ -0,0 +1,9 @@
1
+ module Bmg
2
+
3
+ # Main parent of all Bmg errors
4
+ class Error < StandardError; end
5
+
6
+ # Raised by Relation#one when the relation is not a singleton
7
+ class OneError < Error; end
8
+
9
+ end
@@ -12,3 +12,4 @@ require_relative 'operator/autosummarize'
12
12
  require_relative 'operator/autowrap'
13
13
  require_relative 'operator/project'
14
14
  require_relative 'operator/rename'
15
+ require_relative 'operator/extend'
@@ -0,0 +1,38 @@
1
+ module Bmg
2
+ module Operator
3
+ #
4
+ # Extend operator.
5
+ #
6
+ # Extends operand's tuples with attributes
7
+ # resulting from given computations
8
+ #
9
+ # Example:
10
+ #
11
+ # [{ a: 1 }] extend { b: ->(t){ 2 } } => [{ a: 1, b: 2 }]
12
+ #
13
+ class Extend
14
+ include Operator
15
+
16
+ def initialize(operand, extension)
17
+ @operand = operand
18
+ @extension = extension
19
+ end
20
+
21
+ def each
22
+ @operand.each do |tuple|
23
+ yield extend_it(tuple)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def extend_it(tuple)
30
+ @extension.each_with_object(tuple.dup) { |(k,v), memo|
31
+ memo[k] = v.call(tuple)
32
+ memo
33
+ }
34
+ end
35
+
36
+ end # class Extend
37
+ end # module Operator
38
+ end # module Bmg
@@ -6,10 +6,38 @@ module Bmg
6
6
  @operand = operand
7
7
  end
8
8
 
9
+ ## Consumption methods
10
+
9
11
  def each(&bl)
10
12
  @operand.each(&bl)
11
13
  end
12
14
 
15
+ # Private helper to implement `one` and `one_or_nil`
16
+ def one_or_yield(&bl)
17
+ first = nil
18
+ each do |x|
19
+ raise OneError, "Relation has more than one tuple" unless first.nil?
20
+ first = x
21
+ end
22
+ first.nil? ? bl.call : first
23
+ end
24
+ private :one_or_yield
25
+
26
+ # Returns the only tuple that the relation contains.
27
+ # Throws a OneException when there is no tuple or more than one
28
+ def one
29
+ one_or_yield{ raise OneError, "Relation is empty" }
30
+ end
31
+
32
+ # Returns the only tuple that the relation contains.
33
+ # Returns nil if the relation is empty.
34
+ # Throws a OneException when the relation contains more than one tuple
35
+ def one_or_nil
36
+ one_or_yield{ nil }
37
+ end
38
+
39
+ ## Relational algebra
40
+
13
41
  def allbut(butlist = [])
14
42
  Relation.new Operator::Allbut.new(@operand, butlist)
15
43
  end
@@ -22,6 +50,10 @@ module Bmg
22
50
  Relation.new Operator::Autosummarize.new(@operand, by, summarization)
23
51
  end
24
52
 
53
+ def extend(extension = {})
54
+ Relation.new Operator::Extend.new(@operand, extension)
55
+ end
56
+
25
57
  def project(attrlist = [])
26
58
  Relation.new Operator::Project.new(@operand, attrlist)
27
59
  end
@@ -1,8 +1,8 @@
1
1
  module Bmg
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
5
- TINY = 1
4
+ MINOR = 2
5
+ TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
@@ -78,10 +78,12 @@ files:
78
78
  - README.md
79
79
  - Rakefile
80
80
  - lib/bmg.rb
81
+ - lib/bmg/error.rb
81
82
  - lib/bmg/operator.rb
82
83
  - lib/bmg/operator/allbut.rb
83
84
  - lib/bmg/operator/autosummarize.rb
84
85
  - lib/bmg/operator/autowrap.rb
86
+ - lib/bmg/operator/extend.rb
85
87
  - lib/bmg/operator/project.rb
86
88
  - lib/bmg/operator/rename.rb
87
89
  - lib/bmg/reader.rb