metrix_db 0.0.6 → 0.0.7

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- metrix_db (0.0.5)
4
+ metrix_db (0.0.6)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
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
 
@@ -1,3 +1,3 @@
1
1
  module MetrixDb
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
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 first(conditions={})
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.first(@ref_field => value)
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
@@ -50,6 +50,10 @@ describe MetrixDB do
50
50
  break
51
51
  end
52
52
  end
53
+
54
+ it "should respond_to map" do
55
+ @klass.map(&:col2).should == [2,3]
56
+ end
53
57
  end
54
58
  end
55
59
 
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MetrixDB do
4
- context ".first" do
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 first col" do
17
- @class.first(:col1 => 1).data.should == [1,2]
18
- @class.first(:col1 => 2).data.should == [2,3]
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.first(:col2 => 2).data.should == [1,2]
23
- @class.first(:col2 => 3).data.should == [2,3]
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.first(:col2 => 2).should be_kind_of @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
- end
35
+ end
42
36
 
43
37
  context "ref query syntax []" do
44
- it "use first field as search key" do
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Allen Wei