metrix_db 0.0.6 → 0.0.7
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 +4 -6
- data/lib/metrix_db/version.rb +1 -1
- data/lib/metrix_db.rb +3 -8
- data/spec/metrix_db/data_spec.rb +4 -0
- data/spec/metrix_db/query_spec.rb +9 -15
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -38,12 +38,6 @@ Options for field definition
|
|
38
38
|
Query
|
39
39
|
-----
|
40
40
|
|
41
|
-
mydb = MyDB.first(:col1 => 1, :col2 => 2) # nil or instance of MyDB.
|
42
|
-
mydb.col1 # => 1
|
43
|
-
mydb.col2 # => 2
|
44
|
-
|
45
|
-
or alias :on
|
46
|
-
|
47
41
|
mydb = MyDB.on(:col1 => 1) # nil or instance of MyDB.
|
48
42
|
mydb.col1 # => 1
|
49
43
|
mydb.col2 # => 2
|
@@ -58,6 +52,8 @@ use `[]` as query syntax, it will use ref field to query data.
|
|
58
52
|
Iteration
|
59
53
|
-----
|
60
54
|
|
55
|
+
You can use MyDB as Array, because of it include Enumerable.
|
56
|
+
|
61
57
|
MyDB.each do |inst|
|
62
58
|
puts inst.col1
|
63
59
|
end
|
@@ -65,6 +61,8 @@ Iteration
|
|
65
61
|
MyDB.each_with_index do |inst, index|
|
66
62
|
puts inst.col1
|
67
63
|
end
|
64
|
+
|
65
|
+
MyDB.map { |inst| inst.col1 } # [1,2]
|
68
66
|
|
69
67
|
|
70
68
|
|
data/lib/metrix_db/version.rb
CHANGED
data/lib/metrix_db.rb
CHANGED
@@ -14,6 +14,7 @@ module MetrixDB
|
|
14
14
|
end
|
15
15
|
|
16
16
|
module ClassMethods
|
17
|
+
include Enumerable
|
17
18
|
def field(name, options = {})
|
18
19
|
default_options = {:uniq => true}
|
19
20
|
options = default_options.merge(options)
|
@@ -52,7 +53,7 @@ module MetrixDB
|
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
55
|
-
def
|
56
|
+
def on(conditions={})
|
56
57
|
return nil if @dataset.nil? || @fields.nil?
|
57
58
|
@dataset.each do |data|
|
58
59
|
conditions.each_pair do |k, v|
|
@@ -61,10 +62,9 @@ module MetrixDB
|
|
61
62
|
end
|
62
63
|
return nil
|
63
64
|
end
|
64
|
-
alias :on :first
|
65
65
|
|
66
66
|
def [](value)
|
67
|
-
self.
|
67
|
+
self.on(@ref_field => value)
|
68
68
|
end
|
69
69
|
|
70
70
|
def each
|
@@ -73,10 +73,5 @@ module MetrixDB
|
|
73
73
|
end
|
74
74
|
end
|
75
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
|
81
76
|
end
|
82
77
|
end
|
data/spec/metrix_db/data_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MetrixDB do
|
4
|
-
context ".
|
4
|
+
context ".on" do
|
5
5
|
before do
|
6
6
|
dataset = [[1,2],[2,3]]
|
7
7
|
@dataset = dataset
|
@@ -13,35 +13,29 @@ describe MetrixDB do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
it "find data from
|
17
|
-
@class.
|
18
|
-
@class.
|
16
|
+
it "find data from on col" do
|
17
|
+
@class.on(:col1 => 1).data.should == [1,2]
|
18
|
+
@class.on(:col1 => 2).data.should == [2,3]
|
19
19
|
end
|
20
20
|
|
21
21
|
it "find data from second col" do
|
22
|
-
@class.
|
23
|
-
@class.
|
22
|
+
@class.on(:col2 => 2).data.should == [1,2]
|
23
|
+
@class.on(:col2 => 3).data.should == [2,3]
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should initialize a instance" do
|
27
|
-
@class.
|
27
|
+
@class.on(:col2 => 2).should be_kind_of @class
|
28
28
|
end
|
29
29
|
|
30
30
|
it "newlly created instance should have instance methods according to field definition" do
|
31
|
-
inst = @class.first(:col2 => 2)
|
32
|
-
inst.col1.should == 1
|
33
|
-
inst.col2.should == 2
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should alias :on to :first" do
|
37
31
|
inst = @class.on(:col2 => 2)
|
38
32
|
inst.col1.should == 1
|
39
33
|
inst.col2.should == 2
|
40
34
|
end
|
41
|
-
|
35
|
+
end
|
42
36
|
|
43
37
|
context "ref query syntax []" do
|
44
|
-
it "use
|
38
|
+
it "use on field as search key" do
|
45
39
|
klass = Class.new do
|
46
40
|
include MetrixDB
|
47
41
|
field :field1
|
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: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Allen Wei
|