active_redis_orm 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69575da090b72886a83b27078d14b461c6750d5a
4
- data.tar.gz: fd66ae1837b07736e6e93d89877a28cfa7cabbab
3
+ metadata.gz: 93c66b738cb2ef6d799069c81595d3ab66f25c55
4
+ data.tar.gz: 575fb512ebd7a16ad02b5aa1ff42630ce9be7c52
5
5
  SHA512:
6
- metadata.gz: f675563efa18c94e2e3e698d08f102ca213c4dafd1f22bff7a95fbbcd433952e756583b85423c3048a54167a9105aedf6fb4206b7b36ad43e9b5057e4afa31be
7
- data.tar.gz: b98a86c0b724064805350d80598aeb8af7c958598765e77308d8b816ea0df8a6f1baf1f9c8f94c029c2badfc28ef376d51b11846c720674b9e40230926d63117
6
+ metadata.gz: a5d004a2dab86ec0d53272b4f8f10e5c8357fc4a89a8c139fbb8aca8398f472c2df29e093d81ac330bb5dad60dfe7494c43af70f1b3dc81cb0c189713007366a
7
+ data.tar.gz: ff3af39d4b678b13c49191b3afdc81c5f7c34251513f0f4feeb545c800c066c6e0886758f055c5a86b4beb88927d3714e6d5105358753b7b7d433311cecc54ae
data/README.md CHANGED
@@ -52,6 +52,18 @@ User.create(username: "some_guy")
52
52
  User.find_by_username("some_guy") #=> our user
53
53
  ```
54
54
 
55
+ When finding by ID, returns the object or nil. By default, every ActiveRedis class has a #presence_field method which is defaulted to "1", you can override the need for this if you have another way to determine presence (i.e. another field which says "this object surely exists"), in which case, override the "presence_field" class variable:
56
+
57
+ ```ruby
58
+ class User < ActiveRedis::Base
59
+ field :name
60
+
61
+ self.presence_field = :name
62
+ end
63
+ ```
64
+
65
+ This way the #present? method on User will call the name method and check if it's present.
66
+
55
67
  ### Redis Data Structures
56
68
 
57
69
  Using hashes, sets, sorted sets and lists is simple-ish.
@@ -269,7 +269,7 @@ module ActiveRedis
269
269
  if !array.is_a?(Array) && array.present?
270
270
  array = [array]
271
271
  end
272
- #{field_name}.replace(array) if array.is_a?(Array)
272
+ #{field_name}.replace(array.to_a)
273
273
 
274
274
  #{field_name}
275
275
  end
@@ -308,7 +308,7 @@ module ActiveRedis
308
308
  end
309
309
 
310
310
  def #{field_name}=(array)
311
- #{field_name}.replace(array)
311
+ #{field_name}.replace(array.to_a)
312
312
  end
313
313
 
314
314
  def #{field_name}_count
@@ -340,6 +340,11 @@ module ActiveRedis
340
340
  end
341
341
  end
342
342
 
343
+ def #{field_name}=(hash)
344
+ hash = {} if hash.nil?
345
+ #{field_name}.replace(hash)
346
+ end
347
+
343
348
  def #{field_name}_count
344
349
  #{field_name}_object.count
345
350
  end
@@ -370,6 +375,7 @@ module ActiveRedis
370
375
  end
371
376
 
372
377
  def #{field_name}=(value)
378
+ value = {} if value.nil?
373
379
  #{field_name}.replace(value)
374
380
  end
375
381
 
@@ -10,6 +10,15 @@ module ActiveRedis
10
10
  @hash[value] = score
11
11
  end
12
12
 
13
+ def replace(hash)
14
+ @hash = hash
15
+ self.clear
16
+ hash.values.each do |value|
17
+ self.push(value)
18
+ end
19
+ hash
20
+ end
21
+
13
22
  def changes
14
23
  super.merge(hash: @hash)
15
24
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRedis
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -71,12 +71,25 @@ module ActiveRedis
71
71
  public_send("#{name}_val")
72
72
  end
73
73
 
74
+ def present?
75
+ if self.class.presence_field.present?
76
+ send(self.class.presence_field).present?
77
+ else
78
+ presence_field.present?
79
+ end
80
+ end
81
+
74
82
  class << self
75
83
  include ActiveRedis::Attributes
76
- attr_accessor :attribute_definitions, :attributes_module, :redis
84
+ attr_accessor :attribute_definitions, :attributes_module, :redis, :presence_field
77
85
 
78
86
  def find(id)
79
- new(id)
87
+ result = new(id)
88
+ if result.present?
89
+ result
90
+ else
91
+ nil
92
+ end
80
93
  end
81
94
 
82
95
  def create(*args)
@@ -124,8 +137,13 @@ module ActiveRedis
124
137
  end
125
138
  end
126
139
  end
140
+
127
141
  klass.attributes_module = Module.new
128
142
  klass.send(:include, klass.attributes_module)
143
+
144
+ if klass.presence_field.blank?
145
+ klass.field :presence_field, default: "1"
146
+ end
129
147
  end
130
148
  end
131
149
  end
@@ -0,0 +1,25 @@
1
+ require "active_redis_orm"
2
+ class ActiveRedisFinderObject < ActiveRedis::Base
3
+ field :foo
4
+ field :bar, finder_field: true
5
+ end
6
+
7
+ describe "Finding Active Redis objects" do
8
+ before do
9
+ ActiveRedis.redis.flushdb
10
+ end
11
+
12
+ it "returns nil if not found by finder field" do
13
+ ActiveRedisFinderObject.find_by_bar("bat").should be_nil
14
+ end
15
+
16
+ it "returns nil if not found by id" do
17
+ ActiveRedisFinderObject.find("bullshit_id").should be_nil
18
+ end
19
+
20
+ it "returns the object if found" do
21
+ object = ActiveRedisFinderObject.create(foo: "cow", bar: "baz")
22
+ ActiveRedisFinderObject.find(object.id).should == object
23
+ ActiveRedisFinderObject.find_by_bar("baz").should == object
24
+ end
25
+ end
@@ -64,6 +64,38 @@ describe ActiveRedis do
64
64
  object.reload!
65
65
  object.date_object.value.should be_nil
66
66
  end
67
+
68
+ it "nullifies sets" do
69
+ object = ActiveRedisObject.create(set: [1,2,3])
70
+ object.reload!.set.should == ["1", "2", "3"]
71
+ object.set = nil
72
+ object.save
73
+ object.reload!.set.should == []
74
+ end
75
+
76
+ it "nullifies sorted sets" do
77
+ object = ActiveRedisObject.create(sorted_set: {1 => 1, 2 => 2, 3 => 3})
78
+ object.reload!.sorted_set.should == ["1", "2", "3"]
79
+ object.sorted_set = nil
80
+ object.save
81
+ object.reload!.sorted_set.should == []
82
+ end
83
+
84
+ it "nullifies lists" do
85
+ object = ActiveRedisObject.create(list: [1,2,3])
86
+ object.reload!.list.should == ["1", "2", "3"]
87
+ object.list = nil
88
+ object.save
89
+ object.reload!.list.should == []
90
+ end
91
+
92
+ it "nullifies hashes" do
93
+ object = ActiveRedisObject.create(hash: {a: :b})
94
+ object.reload!.hash.should == {"a" => "b"}
95
+ object.hash = nil
96
+ object.save
97
+ object.reload!.hash.should == {}
98
+ end
67
99
  end
68
100
 
69
101
  context "setters" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_redis_orm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Caspy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-18 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -134,6 +134,7 @@ files:
134
134
  - lib/active_redis/timestamps.rb
135
135
  - lib/active_redis/version.rb
136
136
  - lib/active_redis_orm.rb
137
+ - spec/lib/active_redis/finders_spec.rb
137
138
  - spec/lib/active_redis/nil_object_spec.rb
138
139
  - spec/lib/active_redis/validation_spec.rb
139
140
  - spec/lib/active_redis_spec.rb
@@ -163,6 +164,7 @@ signing_key:
163
164
  specification_version: 4
164
165
  summary: ActiveRedis is an ORM for redis written in Ruby.
165
166
  test_files:
167
+ - spec/lib/active_redis/finders_spec.rb
166
168
  - spec/lib/active_redis/nil_object_spec.rb
167
169
  - spec/lib/active_redis/validation_spec.rb
168
170
  - spec/lib/active_redis_spec.rb