bmg 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bmg.rb +1 -0
- data/lib/bmg/error.rb +9 -0
- data/lib/bmg/operator.rb +1 -0
- data/lib/bmg/operator/extend.rb +38 -0
- data/lib/bmg/relation.rb +32 -0
- data/lib/bmg/version.rb +2 -2
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc75e7b5602e1f76e3581009746302b3a546f3f8
|
4
|
+
data.tar.gz: 5dee61974de7699b5ef4957cd2714d5d241df830
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdad32c048da1719f153a16f355a79b13cd68cd18cf017c47041b7d73992f18c19cbdf9c5c32cf366e8801a0ac98a0ef84237120a7f8e59e9d3274feecd8a715
|
7
|
+
data.tar.gz: 28e09b93f01a6aaa0c8e95adba0bf8b417dbf501e9b5b221c8593a5f53f9cdebe04fb636b7fe641ddf41ba3ff6a706de0ab6c9d4c70b70f49f6a9c055e3041cd
|
data/lib/bmg.rb
CHANGED
data/lib/bmg/error.rb
ADDED
data/lib/bmg/operator.rb
CHANGED
@@ -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
|
data/lib/bmg/relation.rb
CHANGED
@@ -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
|
data/lib/bmg/version.rb
CHANGED
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.
|
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
|