record_redis 0.0.1 → 0.0.2

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.
@@ -20,6 +20,26 @@ module RedisRecord
20
20
  model
21
21
  end
22
22
 
23
+ def self.find_all
24
+ models = Array.new
25
+ name= self.name
26
+ RedisRecord::Connection.connect unless RedisRecord::Connection.isConnected?
27
+ results= RedisRecord::Connection.connection.hgetall(name)
28
+ if results
29
+ results.each do |result|
30
+ model = self.new
31
+ begin
32
+ model.hash_of_properties= JSON.parse(result[1])
33
+ models << model
34
+ rescue JSON::ParserError
35
+ end
36
+ end
37
+ else
38
+ return nil
39
+ end
40
+ models
41
+ end
42
+
23
43
  def update(attributes)
24
44
  @hash_of_properties.merge!(attributes)
25
45
  name= self.class
@@ -32,6 +52,7 @@ module RedisRecord
32
52
 
33
53
  end
34
54
 
55
+ #TODO: returns entitiy or nil if failed
35
56
  def self.create(opts)
36
57
  @hash_of_properties= opts
37
58
  @hash_of_properties[:key]= generate_key if @hash_of_properties[:key].nil?
@@ -58,15 +79,46 @@ module RedisRecord
58
79
  def self.properties(*elems)
59
80
  elems.each do |elem|
60
81
  name= elem.to_s
61
- class_eval <<-EOD
62
- def #{name}=(value)
63
- @hash_of_properties['#{name}']=value
82
+ make_accessor(name)
83
+ make_single_selectors(name)
84
+ make_all_selectors(name)
85
+ end
86
+ end
87
+
88
+ def self.make_accessor(name)
89
+ class_eval <<-EOD
90
+ def #{name}=(value)
91
+ @hash_of_properties['#{name}']=value
92
+ end
93
+ def #{name}
94
+ @hash_of_properties['#{name}']
95
+ end
96
+ EOD
97
+ end
98
+
99
+ def self.make_single_selectors(name)
100
+ class_eval <<-EOD
101
+ def self.find_by_#{name}(value)
102
+ results= self.find_all
103
+ results.each do |result|
104
+ return result if result.#{name}==value
64
105
  end
65
- def #{name}
66
- @hash_of_properties['#{name}']
106
+ return nil
107
+ end
108
+ EOD
109
+ end
110
+
111
+ def self.make_all_selectors(name)
112
+ class_eval <<-EOD
113
+ def self.find_all_by_#{name}(value)
114
+ models= Array.new
115
+ results= self.find_all
116
+ results.each do |result|
117
+ models << result if result.#{name}==value
67
118
  end
68
- EOD
69
- end
119
+ models
120
+ end
121
+ EOD
70
122
  end
71
123
 
72
124
  def self.generate_key
@@ -1,3 +1,3 @@
1
1
  module RedisRecord
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -51,6 +51,29 @@ describe RedisRecord::Base do
51
51
  end
52
52
  end
53
53
 
54
+ describe '#find_all' do
55
+ it 'should find all entries for a key' do
56
+ networks= Network.find_all
57
+ networks.size.should > 1
58
+ end
59
+ end
60
+
61
+ describe '#find_by_' do
62
+ it 'must find a Entry with the given name' do
63
+ name= "testNetwork"
64
+ result= Network.find_by_name("testNetwork")
65
+ result.name.should == name
66
+ end
67
+ end
68
+
69
+ describe '#find_all_by_' do
70
+ it 'must find all Entries for the given attribute' do
71
+ name= "testNetwork"
72
+ results= Network.find_all_by_name("testNetwork")
73
+ results.size.should == 2
74
+ end
75
+ end
76
+
54
77
  describe '#update' do
55
78
  it 'should update a existing model entity' do
56
79
  network= Network.find("1234567890")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: record_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-01 00:00:00.000000000 Z
12
+ date: 2013-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  segments:
114
114
  - 0
115
- hash: -141422551716861098
115
+ hash: -2158523915146133774
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  segments:
123
123
  - 0
124
- hash: -141422551716861098
124
+ hash: -2158523915146133774
125
125
  requirements: []
126
126
  rubyforge_project:
127
127
  rubygems_version: 1.8.25