metrix_db 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +17 -3
- data/lib/metrix_db.rb +12 -0
- data/lib/metrix_db/version.rb +1 -1
- data/spec/metrix_db/data_spec.rb +35 -0
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -30,7 +30,8 @@ Field
|
|
30
30
|
|
31
31
|
Options for field definition
|
32
32
|
|
33
|
-
`uniq`: Check if column is unique. Default value is true.
|
33
|
+
`uniq`: Check if column is unique. Default value is true.
|
34
|
+
|
34
35
|
`ref`: Support `[]` query syntax. If not set for all field will use first field as ref field
|
35
36
|
|
36
37
|
|
@@ -41,7 +42,7 @@ Query
|
|
41
42
|
mydb.col1 # => 1
|
42
43
|
mydb.col2 # => 2
|
43
44
|
|
44
|
-
or alias :
|
45
|
+
or alias :on
|
45
46
|
|
46
47
|
mydb = MyDB.on(:col1 => 1) # nil or instance of MyDB.
|
47
48
|
mydb.col1 # => 1
|
@@ -49,8 +50,21 @@ or alias :one
|
|
49
50
|
|
50
51
|
use `[]` as query syntax, it will use ref field to query data.
|
51
52
|
|
52
|
-
mydb = MyDB
|
53
|
+
mydb = MyDB[1] # nil or instance of MyDB.
|
54
|
+
mydb.col1 # => 1
|
55
|
+
mydb.col2 # => 2
|
56
|
+
|
57
|
+
|
58
|
+
Iteration
|
59
|
+
-----
|
60
|
+
|
61
|
+
MyDB.each do |inst|
|
62
|
+
puts inst.col1
|
63
|
+
end
|
53
64
|
|
65
|
+
MyDB.each_with_index do |inst, index|
|
66
|
+
puts inst.col1
|
67
|
+
end
|
54
68
|
|
55
69
|
|
56
70
|
|
data/lib/metrix_db.rb
CHANGED
@@ -66,5 +66,17 @@ module MetrixDB
|
|
66
66
|
def [](value)
|
67
67
|
self.first(@ref_field => value)
|
68
68
|
end
|
69
|
+
|
70
|
+
def each
|
71
|
+
@dataset.each do |data|
|
72
|
+
yield self.new(data) if block_given?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def each_with_index
|
77
|
+
@dataset.each_with_index do |data, index|
|
78
|
+
yield self.new(data), index if block_given?
|
79
|
+
end
|
80
|
+
end
|
69
81
|
end
|
70
82
|
end
|
data/lib/metrix_db/version.rb
CHANGED
data/spec/metrix_db/data_spec.rb
CHANGED
@@ -16,5 +16,40 @@ describe MetrixDB do
|
|
16
16
|
@class.instance_variable_get("@dataset").should == @dataset
|
17
17
|
end
|
18
18
|
end
|
19
|
+
|
20
|
+
context "iterator" do
|
21
|
+
before do
|
22
|
+
@klass = Class.new do
|
23
|
+
include MetrixDB
|
24
|
+
field :col1
|
25
|
+
field :col2
|
26
|
+
dataset [[1,2], [2,3]]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
it "should iterate all data use each_with_index" do
|
30
|
+
@klass.each_with_index do |inst, index|
|
31
|
+
if index == 0
|
32
|
+
inst.col1.should == 1
|
33
|
+
inst.col2.should == 2
|
34
|
+
elsif index == 1
|
35
|
+
inst.col1.should == 2
|
36
|
+
inst.col2.should == 3
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
it "should iterate all data use each" do
|
41
|
+
@klass.each do |inst, index|
|
42
|
+
inst.col1.should == 1
|
43
|
+
inst.col2.should == 2
|
44
|
+
break
|
45
|
+
end
|
46
|
+
@klass.each do |inst, index|
|
47
|
+
next
|
48
|
+
inst.col1.should == 2
|
49
|
+
inst.col2.should == 3
|
50
|
+
break
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
19
54
|
end
|
20
55
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metrix_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Allen Wei
|