ohm 3.0.3 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. checksums.yaml +4 -4
  2. data/.gems +2 -3
  3. data/CHANGELOG.md +12 -0
  4. data/README.md +39 -20
  5. data/lib/ohm.rb +25 -31
  6. data/ohm.gemspec +2 -2
  7. data/test/indices.rb +1 -1
  8. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94a422b709084441dabd2d9c04a38014ec6ddbef
4
- data.tar.gz: 8549e0842a0737e65a5d459f483b1002e8914ecf
3
+ metadata.gz: 65d8dd7c7555b0a26c8f4eb8845c08354b174acc
4
+ data.tar.gz: b12b6410534f2031ae211eff20604e9ac6da4473
5
5
  SHA512:
6
- metadata.gz: 404357ab09aa51976a53ddc7fe43a31b29030f6067386daca0787fe8cd7d6cb1dbb80fabaee8c839aa23d576f9772dc66584df17e27c37317392aed7ac7f1927
7
- data.tar.gz: 5cffd617b9e7e3119d1dc2d1f241a1107bafd7bdb7b53d4e78745e9d7f1570ceb1c887d488353c9d15782da0c27b750987299c321bb770e0e2eaf34326fb4d84
6
+ metadata.gz: 322e82928c55dc4fbaa9a370220c8e7c0bc382ef4a709fd96456add67a4b26f56617f7f54af4c5a3c14af59fc27fe50fa0ad6339db9246be7c1bee87fa743ddd
7
+ data.tar.gz: dbf21bd5c4984b2d6cbbb8c379aa88696c0a2a154d1fcf5cccfad35ca2e24856d223bb6e3b4642c556b163e82ae344e1cf9d45e2fcfe5b625aca6ef8b5c36d3f
data/.gems CHANGED
@@ -1,5 +1,4 @@
1
+ nest -v 3.0.0
1
2
  redic -v 1.5.0
2
- msgpack -v 0.6.2
3
- nido -v 0.0.1
4
3
  stal -v 0.1.0
5
- cutest -v 1.2.2
4
+ cutest -v 1.2.3
@@ -1,3 +1,15 @@
1
+ ## 3.1.0
2
+
3
+ - Use Nest instead of Nido
4
+
5
+ A new release of Nest (3.0.0) simplified the interaction
6
+ with Redis.
7
+
8
+ - Use Model#hash for equality
9
+
10
+ This change just removes some slight redundancy in the code.
11
+ The external behavior remains unchanged.
12
+
1
13
  ## 3.0.3
2
14
 
3
15
  - Fix bug that gave false positives for unique indices.
data/README.md CHANGED
@@ -21,6 +21,7 @@ Related projects
21
21
 
22
22
  These are libraries in other languages that were inspired by Ohm.
23
23
 
24
+ * [Ohm](https://github.com/soveran/ohm-crystal) for Crystal, created by soveran
24
25
  * [JOhm](https://github.com/xetorthio/johm) for Java, created by xetorthio
25
26
  * [Lohm](https://github.com/slact/lua-ohm) for Lua, created by slact
26
27
  * [ohm.lua](https://github.com/amakawa/ohm.lua) for Lua, created by amakawa
@@ -174,15 +175,15 @@ Attribute types
174
175
 
175
176
  Ohm::Model provides 4 attribute types:
176
177
 
177
- * {Ohm::Model.attribute attribute},
178
- * {Ohm::Model.set set}
179
- * {Ohm::Model.list list}
180
- * {Ohm::Model.counter counter}
178
+ * `Ohm::Model.attribute`,
179
+ * `Ohm::Model.set`
180
+ * `Ohm::Model.list`
181
+ * `Ohm::Model.counter`
181
182
 
182
183
  and 2 meta types:
183
184
 
184
- * {Ohm::Model.reference reference}
185
- * {Ohm::Model.collection collection}.
185
+ * `Ohm::Model.reference`
186
+ * `Ohm::Model.collection`.
186
187
 
187
188
  ### attribute
188
189
 
@@ -304,9 +305,9 @@ end
304
305
 
305
306
  ## Sorting
306
307
 
307
- Since `attendees` is a {Ohm::Model::Set Set}, it exposes two sorting
308
- methods: {Ohm::Model::Collection#sort sort} returns the elements
309
- ordered by `id`, and {Ohm::Model::Collection#sort_by sort_by} receives
308
+ Since `attendees` is a `Ohm::Model::Set`, it exposes two sorting
309
+ methods: `Ohm::Model::Collection#sort` returns the elements
310
+ ordered by `id`, and `Ohm::Model::Collection#sort_by` receives
310
311
  a parameter with an attribute name, which will determine the sorting
311
312
  order. Both methods receive an options hash which is explained below:
312
313
 
@@ -337,8 +338,8 @@ Example:
337
338
  ### :by
338
339
 
339
340
  Key or Hash key with which to sort by. An important distinction with
340
- using {Ohm::Model::Collection#sort sort} and
341
- {Ohm::Model::Collection#sort_by sort_by} is that `sort_by` automatically
341
+ using `Ohm::Model::Collection#sort` and
342
+ `Ohm::Model::Collection#sort_by` is that `sort_by` automatically
342
343
  converts the passed argument with the assumption that it is a hash key
343
344
  and it's within the current model you are sorting.
344
345
 
@@ -354,8 +355,8 @@ otherwise.
354
355
  ### :get
355
356
 
356
357
  A key pattern to return, e.g. `Post:*->title`. As is the case with
357
- the `:by` option, using {Ohm::Model::Collection#sort sort} and
358
- {Ohm::Model::Collection#sort_by sort_by} has distinct differences in
358
+ the `:by` option, using `Ohm::Model::Collection#sort` and
359
+ `Ohm::Model::Collection#sort_by` has distinct differences in
359
360
  that `sort_by` does much of the hand-coding for you.
360
361
 
361
362
  ```ruby
@@ -391,7 +392,7 @@ you can use `post.comments.ids`.
391
392
 
392
393
  ### References explained
393
394
 
394
- Doing a {Ohm::Model.reference reference} is actually just a shortcut for
395
+ Doing a `Ohm::Model.reference` is actually just a shortcut for
395
396
  the following:
396
397
 
397
398
  ```ruby
@@ -423,10 +424,10 @@ Comment.find(post_id: 1)
423
424
 
424
425
  ### Collections explained
425
426
 
426
- The reason a {Ohm::Model.reference reference} and a
427
- {Ohm::Model.collection collection} go hand in hand, is that a collection is
427
+ The reason a `Ohm::Model.reference` and a
428
+ `Ohm::Model.collection` go hand in hand, is that a collection is
428
429
  just a macro that defines a finder for you, and we know that to find a model
429
- by a field requires an {Ohm::Model.index index} to be defined for the field
430
+ by a field requires an `Ohm::Model.index` to be defined for the field
430
431
  you want to search.
431
432
 
432
433
  ```ruby
@@ -460,18 +461,36 @@ Post.to_reference == :post
460
461
  # => true
461
462
  ```
462
463
 
464
+ Modules
465
+ -------
466
+
467
+ If your models are defined inside a module, you will have to define
468
+ the references and collections as in the following example:
469
+
470
+ ```ruby
471
+ module SomeNamespace
472
+ class Foo < Ohm::Model
473
+ attribute :name
474
+ end
475
+
476
+ class Bar < Ohm::Model
477
+ reference :foo, 'SomeNamespace::Foo'
478
+ end
479
+ end
480
+ ```
481
+
463
482
  Indices
464
483
  -------
465
484
 
466
- An {Ohm::Model.index index} is a set that's handled automatically by Ohm. For
485
+ An `Ohm::Model.index` is a set that's handled automatically by Ohm. For
467
486
  any index declared, Ohm maintains different sets of objects IDs for quick
468
487
  lookups.
469
488
 
470
489
  In the `Event` example, the index on the name attribute will
471
490
  allow for searches like `Event.find(name: "some value")`.
472
491
 
473
- Note that the methods {Ohm::Model::Set#find find} and
474
- {Ohm::Model::Set#except except} need a corresponding index in order to work.
492
+ Note that the methods `Ohm::Model::Set#find` and
493
+ `Ohm::Model::Set#except` need a corresponding index in order to work.
475
494
 
476
495
  ### Finding records
477
496
 
data/lib/ohm.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require "json"
4
- require "nido"
4
+ require "nest"
5
5
  require "redic"
6
6
  require "stal"
7
7
 
@@ -173,17 +173,17 @@ module Ohm
173
173
 
174
174
  # Returns the total size of the list using LLEN.
175
175
  def size
176
- redis.call("LLEN", key)
176
+ key.call("LLEN")
177
177
  end
178
178
 
179
179
  # Returns the first element of the list using LINDEX.
180
180
  def first
181
- model[redis.call("LINDEX", key, 0)]
181
+ model[key.call("LINDEX", 0)]
182
182
  end
183
183
 
184
184
  # Returns the last element of the list using LINDEX.
185
185
  def last
186
- model[redis.call("LINDEX", key, -1)]
186
+ model[key.call("LINDEX", -1)]
187
187
  end
188
188
 
189
189
  # Returns an array of elements from the list using LRANGE.
@@ -211,7 +211,7 @@ module Ohm
211
211
  # [c1, c2] == post.comments.range(0, 1)
212
212
  # # => true
213
213
  def range(start, stop)
214
- fetch(redis.call("LRANGE", key, start, stop))
214
+ fetch(key.call("LRANGE", start, stop))
215
215
  end
216
216
 
217
217
  # Checks if the model is part of this List.
@@ -255,12 +255,12 @@ module Ohm
255
255
 
256
256
  # Pushes the model to the _end_ of the list using RPUSH.
257
257
  def push(model)
258
- redis.call("RPUSH", key, model.id)
258
+ key.call("RPUSH", model.id)
259
259
  end
260
260
 
261
261
  # Pushes the model to the _beginning_ of the list using LPUSH.
262
262
  def unshift(model)
263
- redis.call("LPUSH", key, model.id)
263
+ key.call("LPUSH", model.id)
264
264
  end
265
265
 
266
266
  # Delete a model from the list.
@@ -292,7 +292,7 @@ module Ohm
292
292
 
293
293
  # LREM key 0 <id> means remove all elements matching <id>
294
294
  # @see http://redis.io/commands/lrem
295
- redis.call("LREM", key, 0, model.id)
295
+ key.call("LREM", 0, model.id)
296
296
  end
297
297
 
298
298
  # Returns an array with all the ID's of the list.
@@ -316,7 +316,7 @@ module Ohm
316
316
  # # => ["1", "2", "3"]
317
317
  #
318
318
  def ids
319
- redis.call("LRANGE", key, 0, -1)
319
+ key.call("LRANGE", 0, -1)
320
320
  end
321
321
 
322
322
  private
@@ -378,7 +378,7 @@ module Ohm
378
378
  if Array === key
379
379
  Stal.solve(redis, key)
380
380
  else
381
- redis.call("SMEMBERS", key)
381
+ key.call("SMEMBERS")
382
382
  end
383
383
  end
384
384
 
@@ -599,7 +599,7 @@ module Ohm
599
599
  # user.posts.add(post)
600
600
  #
601
601
  def add(model)
602
- redis.call("SADD", key, model.id)
602
+ key.call("SADD", model.id)
603
603
  end
604
604
 
605
605
  alias_method :<<, :add
@@ -614,7 +614,7 @@ module Ohm
614
614
  # user.posts.delete(post)
615
615
  #
616
616
  def delete(model)
617
- redis.call("SREM", key, model.id)
617
+ key.call("SREM", model.id)
618
618
  end
619
619
 
620
620
  # Replace all the existing elements of a set with a different
@@ -719,18 +719,14 @@ module Ohm
719
719
  # class User < Ohm::Model
720
720
  # end
721
721
  #
722
- # User.key == "User"
723
- # User.key.kind_of?(String)
722
+ # User.key.kind_of?(Nest)
724
723
  # # => true
725
724
  #
726
- # User.key.kind_of?(Nido)
727
- # # => true
728
- #
729
- # To find out more about Nido, see:
730
- # http://github.com/soveran/nido
725
+ # To find out more about Nest, see:
726
+ # http://github.com/soveran/nest
731
727
  #
732
728
  def self.key
733
- @key ||= Nido.new(self.name)
729
+ @key ||= Nest.new(self.name, redis)
734
730
  end
735
731
 
736
732
  # Retrieve a record by ID.
@@ -763,7 +759,7 @@ module Ohm
763
759
 
764
760
  # Check if the ID exists within <Model>:all.
765
761
  def self.exists?(id)
766
- redis.call("SISMEMBER", key[:all], id) == 1
762
+ key[:all].call("SISMEMBER", id) == 1
767
763
  end
768
764
 
769
765
  # Find values in `unique` indices.
@@ -781,7 +777,7 @@ module Ohm
781
777
  def self.with(att, val)
782
778
  raise IndexNotFound unless uniques.include?(att)
783
779
 
784
- id = redis.call("HGET", key[:uniques][att], val)
780
+ id = key[:uniques][att].call("HGET", val)
785
781
  new(:id => id).load! if id
786
782
  end
787
783
 
@@ -1079,7 +1075,7 @@ module Ohm
1079
1075
  define_method(name) do
1080
1076
  return 0 if new?
1081
1077
 
1082
- redis.call("HGET", key[:counters], name).to_i
1078
+ key[:counters].call("HGET", name).to_i
1083
1079
  end
1084
1080
  end
1085
1081
 
@@ -1141,15 +1137,13 @@ module Ohm
1141
1137
  # 2. That they represent the same Redis key.
1142
1138
  #
1143
1139
  def ==(other)
1144
- other.kind_of?(model) && other.key == key
1145
- rescue MissingID
1146
- false
1140
+ other.kind_of?(model) && other.hash == hash
1147
1141
  end
1148
1142
 
1149
1143
  # Preload all the attributes of this model from Redis. Used
1150
1144
  # internally by `Model::[]`.
1151
1145
  def load!
1152
- update_attributes(Utils.dict(redis.call("HGETALL", key))) unless new?
1146
+ update_attributes(Utils.dict(key.call("HGETALL"))) unless new?
1153
1147
  return self
1154
1148
  end
1155
1149
 
@@ -1170,7 +1164,7 @@ module Ohm
1170
1164
  # | u.get(:name) == "B"
1171
1165
  #
1172
1166
  def get(att)
1173
- @attributes[att] = redis.call("HGET", key, att)
1167
+ @attributes[att] = key.call("HGET", att)
1174
1168
  end
1175
1169
 
1176
1170
  # Update an attribute value atomically. The best usecase for this
@@ -1181,9 +1175,9 @@ module Ohm
1181
1175
  #
1182
1176
  def set(att, val)
1183
1177
  if val.to_s.empty?
1184
- redis.call("HDEL", key, att)
1178
+ key.call("HDEL", att)
1185
1179
  else
1186
- redis.call("HSET", key, att, val)
1180
+ key.call("HSET", att, val)
1187
1181
  end
1188
1182
 
1189
1183
  @attributes[att] = val
@@ -1224,7 +1218,7 @@ module Ohm
1224
1218
  # ad.hits # => 3
1225
1219
  #
1226
1220
  def increment(att, count = 1)
1227
- redis.call("HINCRBY", key[:counters], att, count)
1221
+ key[:counters].call("HINCRBY", att, count)
1228
1222
  end
1229
1223
 
1230
1224
  # Decrements a counter atomically. Internally uses `HINCRBY`.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ohm"
3
- s.version = "3.0.3"
3
+ s.version = "3.1.0"
4
4
  s.summary = %{Object-hash mapping library for Redis.}
5
5
  s.description = %Q{Ohm is a library that allows to store an object in Redis, a persistent key-value database. It has very good performance.}
6
6
  s.authors = ["Michel Martens", "Damian Janowski", "Cyril David"]
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.rubyforge_project = "ohm"
14
14
 
15
15
  s.add_dependency "redic", "~> 1.5.0"
16
- s.add_dependency "nido"
16
+ s.add_dependency "nest", "~> 3"
17
17
  s.add_dependency "stal"
18
18
 
19
19
  s.add_development_dependency "cutest"
@@ -51,7 +51,7 @@ test "raise an error if the parameter supplied is not a hash" do
51
51
  end
52
52
 
53
53
  test "avoid intersections with the all collection" do
54
- assert_equal "User:indices:email:foo", User.find(:email => "foo").key
54
+ assert_equal "User:indices:email:foo", User.find(:email => "foo").key.to_s
55
55
  end
56
56
 
57
57
  test "allow multiple chained finds" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-06-15 00:00:00.000000000 Z
13
+ date: 2016-12-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: redic
@@ -27,19 +27,19 @@ dependencies:
27
27
  - !ruby/object:Gem::Version
28
28
  version: 1.5.0
29
29
  - !ruby/object:Gem::Dependency
30
- name: nido
30
+ name: nest
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - ">="
33
+ - - "~>"
34
34
  - !ruby/object:Gem::Version
35
- version: '0'
35
+ version: '3'
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ">="
40
+ - - "~>"
41
41
  - !ruby/object:Gem::Version
42
- version: '0'
42
+ version: '3'
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: stal
45
45
  requirement: !ruby/object:Gem::Requirement