metrix_db 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- metrix_db (0.0.6)
4
+ metrix_db (0.0.7)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  Metrix DB
2
2
  =============
3
3
 
4
- Simple DB base on ruby Array. The main usage is data mapping.
4
+ Simple DB base on ruby Array. The main usage is data mapping.
5
+
6
+ Notice: Do not use it in large dataset
5
7
 
6
8
  Installation
7
9
  -----------
@@ -52,17 +54,17 @@ use `[]` as query syntax, it will use ref field to query data.
52
54
  Iteration
53
55
  -----
54
56
 
55
- You can use MyDB as Array, because of it include Enumerable.
57
+ MyDB.all #=> Array of MyDB instance
56
58
 
57
- MyDB.each do |inst|
59
+ MyDB.all.each do |inst|
58
60
  puts inst.col1
59
61
  end
60
62
 
61
- MyDB.each_with_index do |inst, index|
63
+ MyDB.all.each_with_index do |inst, index|
62
64
  puts inst.col1
63
65
  end
64
66
 
65
- MyDB.map { |inst| inst.col1 } # [1,2]
67
+ MyDB.all.map { |inst| inst.col1 } # [1,2]
66
68
 
67
69
 
68
70
 
@@ -1,3 +1,3 @@
1
1
  module MetrixDb
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/metrix_db.rb CHANGED
@@ -14,7 +14,6 @@ module MetrixDB
14
14
  end
15
15
 
16
16
  module ClassMethods
17
- include Enumerable
18
17
  def field(name, options = {})
19
18
  default_options = {:uniq => true}
20
19
  options = default_options.merge(options)
@@ -25,8 +24,12 @@ module MetrixDB
25
24
  @uniq_fields ||= {}
26
25
  @uniq_fields[index] = name.to_s if options[:uniq]
27
26
 
28
- raise UsageError.new("Already defined '#{@ref_field}' as ref column") if options[:ref] && @ref_field
29
- @ref_field = name.to_sym if options[:ref]
27
+ if !@ref_field
28
+ @ref_field = name.to_sym
29
+ end
30
+ if options[:ref]
31
+ @ref_field = name.to_sym
32
+ end
30
33
 
31
34
  define_method "#{name}" do
32
35
  @data[self.class.fields[name.to_sym]]
@@ -39,7 +42,6 @@ module MetrixDB
39
42
 
40
43
  def dataset(_dataset)
41
44
  @dataset = _dataset
42
- @ref_field ||= @fields.keys.first
43
45
  check_uniq_fields(_dataset)
44
46
  end
45
47
 
@@ -55,9 +57,11 @@ module MetrixDB
55
57
 
56
58
  def on(conditions={})
57
59
  return nil if @dataset.nil? || @fields.nil?
58
- @dataset.each do |data|
60
+ @dataset.each_with_index do |data, index|
59
61
  conditions.each_pair do |k, v|
60
- return self.new(data) if data[@fields[k.to_sym]] == v
62
+ if data[@fields[k.to_sym]] == v
63
+ return get_from_cache(index)
64
+ end
61
65
  end
62
66
  end
63
67
  return nil
@@ -67,10 +71,18 @@ module MetrixDB
67
71
  self.on(@ref_field => value)
68
72
  end
69
73
 
70
- def each
71
- @dataset.each do |data|
72
- yield self.new(data) if block_given?
74
+ def all
75
+ result = []
76
+ @dataset.each_with_index do |data, index|
77
+ result << get_from_cache(index)
73
78
  end
79
+ result
80
+ end
81
+
82
+ private
83
+ def get_from_cache(index)
84
+ @inst_cache ||= {}
85
+ @inst_cache[index] ||= self.new(@dataset[index])
74
86
  end
75
87
 
76
88
  end
@@ -27,7 +27,7 @@ describe MetrixDB do
27
27
  end
28
28
  end
29
29
  it "should iterate all data use each_with_index" do
30
- @klass.each_with_index do |inst, index|
30
+ @klass.all.each_with_index do |inst, index|
31
31
  if index == 0
32
32
  inst.col1.should == 1
33
33
  inst.col2.should == 2
@@ -38,12 +38,12 @@ describe MetrixDB do
38
38
  end
39
39
  end
40
40
  it "should iterate all data use each" do
41
- @klass.each do |inst, index|
41
+ @klass.all.each do |inst, index|
42
42
  inst.col1.should == 1
43
43
  inst.col2.should == 2
44
44
  break
45
45
  end
46
- @klass.each do |inst, index|
46
+ @klass.all.each do |inst, index|
47
47
  next
48
48
  inst.col1.should == 2
49
49
  inst.col2.should == 3
@@ -52,7 +52,11 @@ describe MetrixDB do
52
52
  end
53
53
 
54
54
  it "should respond_to map" do
55
- @klass.map(&:col2).should == [2,3]
55
+ @klass.all.map(&:col2).should == [2,3]
56
+ end
57
+
58
+ it "should respond_to all" do
59
+ @klass.all.should have(2).things
56
60
  end
57
61
  end
58
62
  end
@@ -68,15 +68,6 @@ describe MetrixDB do
68
68
  end
69
69
  klass.instance_variable_get("@ref_field").should == :field2
70
70
  end
71
-
72
- it "will raise error if define two ref field" do
73
- lambda {Class.new {
74
- include MetrixDB
75
- field :field1, :ref => true
76
- field :field2, :ref => true
77
- dataset [[1,2],[2,3]]
78
- }}.should raise_error MetrixDB::UsageError, "Already defined 'field1' as ref column"
79
- end
80
-
71
+
81
72
  end
82
73
  end
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: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Allen Wei