ohm-zset 0.3 → 0.4

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/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
+ *.gem
2
+
1
3
  ## MAC OS
2
4
  .DS_Store
data/README.md CHANGED
@@ -59,6 +59,35 @@ b.zsmalls.to_a.map(&:name)
59
59
  # => ['S5', 'S4', 'S3', 'S6', 'S2', 'S1']
60
60
  ```
61
61
 
62
+ You can also add an element with a custom score.
63
+
64
+ ```ruby
65
+ class Big < Ohm::Model
66
+ include Ohm::ZScores
67
+
68
+ zset :zcustom, :Bool, nil, ZScore::DateTime
69
+ end
70
+
71
+ class Bool < Ohm::Model
72
+ attribute :name
73
+ attribute :is_valid
74
+ end
75
+
76
+ b = Big.create
77
+ b1 = Bool.create(name: 'B1', is_valid: "false")
78
+ b2 = Bool.create(name: 'B2', is_valid: "true")
79
+ b3 = Bool.create(name: 'B3', is_valid: "false")
80
+ b4 = Bool.create(name: 'B4', is_valid: "true")
81
+
82
+ b.zcustom.add(b1, "2012-07-29 06:24:20 +0800")
83
+ b.zcustom.add(b2, "2012-07-25 06:24:20 +0800")
84
+ b.zcustom.add(b3, "2012-07-30 06:24:20 +0800")
85
+ b.zcustom.add(b4, "2012-07-27 06:24:20 +0800")
86
+
87
+ b.zcustom.to_a.map(&:name)
88
+ # => ['B2', 'B4', 'B1', 'B3']
89
+ ```
90
+
62
91
  You can update the score of an element by using *update*. There is also a *count* function that returns the number of elements with scores inside a specified range.
63
92
 
64
93
  ## Deleting Elements
@@ -1,5 +1,5 @@
1
1
  module Ohm
2
2
  module Zset
3
- VERSION = "0.3"
3
+ VERSION = "0.4"
4
4
  end
5
5
  end
data/lib/ohm-zset.rb CHANGED
@@ -234,18 +234,22 @@ module Ohm
234
234
  size == 0
235
235
  end
236
236
 
237
- def add(model)
237
+ def add(model, custom_score = nil)
238
238
  score_value = model
239
239
 
240
240
  lambda_function = score_field.each do |field|
241
241
  break field if field.is_a? Proc
242
242
 
243
- score_value = model.send(field)
243
+ score_value = model.send(field) if custom_score.nil?
244
244
 
245
245
  break lambda{ |x| x.to_i } if field == score_field.last
246
246
  end
247
247
 
248
- db.zadd(key, lambda_function.call(score_value), model.id)
248
+ if custom_score.nil?
249
+ db.zadd(key, lambda_function.call(score_value), model.id)
250
+ else
251
+ db.zadd(key, lambda_function.call(custom_score), model.id)
252
+ end
249
253
  end
250
254
 
251
255
  def add_list(*models)
@@ -14,6 +14,7 @@ class Big < Ohm::Model
14
14
  zset :zbools2, :Bool, :name, ZScore::String
15
15
  zset :zbools3, :Bool, :name, ZScore::StringInsensitive
16
16
  zset :zbools4, :Bool, :name, ZScore::StringInsensitiveHigh
17
+ zset :zcustom, :Bool, nil, ZScore::DateTime
17
18
  end
18
19
 
19
20
  class Bool < Ohm::Model
@@ -660,4 +661,20 @@ describe Ohm do
660
661
 
661
662
  assert_equal sorted_set_2.to_a.map(&:name), sorted_set.to_a.map(&:name)
662
663
  end
664
+
665
+ it "can add element with custom score" do
666
+ b = Big.create
667
+ b1 = Bool.create(name: 'B1', is_valid: "false")
668
+ b2 = Bool.create(name: 'B2', is_valid: "true")
669
+ b3 = Bool.create(name: 'B3', is_valid: "false")
670
+ b4 = Bool.create(name: 'B4', is_valid: "true")
671
+
672
+ b.zcustom.add(b1, "2012-07-29 06:24:20 +0800")
673
+ b.zcustom.add(b2, "2012-07-25 06:24:20 +0800")
674
+ b.zcustom.add(b3, "2012-07-30 06:24:20 +0800")
675
+ b.zcustom.add(b4, "2012-07-27 06:24:20 +0800")
676
+
677
+ sorted_set = b.zcustom
678
+ assert_equal ['B2', 'B4', 'B1', 'B3'], sorted_set.to_a.map(&:name)
679
+ end
663
680
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm-zset
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
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: 2012-10-04 00:00:00.000000000 Z
12
+ date: 2012-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ohm