ohm 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ohm.rb CHANGED
@@ -195,7 +195,7 @@ module Ohm
195
195
  # them.
196
196
  #
197
197
  def sort_by(att, options = {})
198
- sort(options.merge(:by => namespace["*->%s" % att]))
198
+ sort(options.merge(:by => to_key(att)))
199
199
  end
200
200
 
201
201
  # Allows you to sort your models using their IDs. This is much
@@ -223,7 +223,7 @@ module Ohm
223
223
  #
224
224
  def sort(options = {})
225
225
  if options.has_key?(:get)
226
- options[:get] = namespace["*->%s" % options[:get]]
226
+ options[:get] = to_key(options[:get])
227
227
  return execute { |key| db.sort(key, options) }
228
228
  end
229
229
 
@@ -296,6 +296,14 @@ module Ohm
296
296
  def exists?(id)
297
297
  execute { |key| db.sismember(key, id) }
298
298
  end
299
+
300
+ def to_key(att)
301
+ if model.counters.include?(att)
302
+ namespace["*:counters->%s" % att]
303
+ else
304
+ namespace["*->%s" % att]
305
+ end
306
+ end
299
307
  end
300
308
 
301
309
  class List < Struct.new(:key, :namespace, :model)
@@ -1020,6 +1028,8 @@ module Ohm
1020
1028
  # try to do it, you'll receive an Ohm::MissingID error.
1021
1029
  #
1022
1030
  def self.counter(name)
1031
+ counters << name unless counters.include?(name)
1032
+
1023
1033
  define_method(name) do
1024
1034
  return 0 if new?
1025
1035
 
@@ -1358,6 +1368,10 @@ module Ohm
1358
1368
  @uniques ||= []
1359
1369
  end
1360
1370
 
1371
+ def self.counters
1372
+ @counters ||= []
1373
+ end
1374
+
1361
1375
  def self.collections
1362
1376
  @collections ||= []
1363
1377
  end
data/test/filtering.rb CHANGED
@@ -52,7 +52,7 @@ test "#[]" do |john, jane|
52
52
  end
53
53
 
54
54
  test "#except" do |john, jane|
55
- out = User.create(:status => "inactive", :lname => "Doe")
55
+ User.create(:status => "inactive", :lname => "Doe")
56
56
 
57
57
  res = User.find(:lname => "Doe").except(:status => "inactive")
58
58
 
@@ -80,8 +80,8 @@ test "indices bug related to a nil attribute" do |john, jane|
80
80
  end
81
81
 
82
82
  test "#union" do |john, jane|
83
+ User.create(:status => "super", :lname => "Doe")
83
84
  included = User.create(:status => "inactive", :lname => "Doe")
84
- excluded = User.create(:status => "super", :lname => "Doe")
85
85
 
86
86
  res = User.find(:status => "active").union(:status => "inactive")
87
87
 
@@ -112,9 +112,9 @@ scope do
112
112
  book1 = Book.create
113
113
  book2 = Book.create
114
114
 
115
- auth1 = Author.create(:book => book1, :mood => "happy")
116
- auth2 = Author.create(:book => book1, :mood => "sad")
117
- auth3 = Author.create(:book => book2, :mood => "sad")
115
+ Author.create(:book => book1, :mood => "happy")
116
+ Author.create(:book => book1, :mood => "sad")
117
+ Author.create(:book => book2, :mood => "sad")
118
118
 
119
119
  [book1, book2]
120
120
  end
data/test/helper.rb CHANGED
@@ -15,7 +15,7 @@ def silence_warnings
15
15
  yield
16
16
  ensure
17
17
  $VERBOSE = original_verbose
18
- end
18
+ end unless defined?(silence_warnings)
19
19
 
20
20
  $VERBOSE = true
21
21
 
data/test/model.rb CHANGED
@@ -16,6 +16,7 @@ end
16
16
 
17
17
  class Person < Ohm::Model
18
18
  attribute :name
19
+ counter :logins
19
20
  index :initial
20
21
 
21
22
  def initial
@@ -70,7 +71,7 @@ test "empty model is ok" do
70
71
  class Foo < Ohm::Model
71
72
  end
72
73
 
73
- foo = Foo.create
74
+ Foo.create
74
75
  end
75
76
 
76
77
  test "counters are cleaned up during deletion" do
@@ -551,25 +552,36 @@ end
551
552
  # Sorting lists and sets by model attributes
552
553
  setup do
553
554
  @event = Event.create(:name => "Ruby Tuesday")
554
- @event.attendees.add(Person.create(:name => "D"))
555
- @event.attendees.add(Person.create(:name => "C"))
556
- @event.attendees.add(Person.create(:name => "B"))
557
- @event.attendees.add(Person.create(:name => "A"))
555
+ {'D' => 4, 'C' => 2, 'B' => 5, 'A' => 3}.each_pair do |name, logins|
556
+ person = Person.create(:name => name)
557
+ person.incr :logins, logins
558
+ @event.attendees.add(person)
559
+ end
558
560
  end
559
561
 
560
562
  test "sort the model instances by the values provided" do
561
563
  people = @event.attendees.sort_by(:name, :order => "ALPHA")
562
- assert %w[A B C D] == people.map { |person| person.name }
564
+ assert %w[A B C D] == people.map(&:name)
563
565
  end
564
566
 
565
567
  test "accept a number in the limit parameter" do
566
568
  people = @event.attendees.sort_by(:name, :limit => [0, 2], :order => "ALPHA")
567
- assert %w[A B] == people.map { |person| person.name }
569
+ assert %w[A B] == people.map(&:name)
568
570
  end
569
571
 
570
572
  test "use the start parameter as an offset if the limit is provided" do
571
573
  people = @event.attendees.sort_by(:name, :limit => [1, 2], :order => "ALPHA")
572
- assert %w[B C] == people.map { |person| person.name }
574
+ assert %w[B C] == people.map(&:name)
575
+ end
576
+
577
+ test "use counter attributes for sorting" do
578
+ people = @event.attendees.sort_by(:logins, :limit => [0, 3], :order => "ALPHA")
579
+ assert %w[C A D] == people.map(&:name)
580
+ end
581
+
582
+ test "use counter attributes for sorting with key option" do
583
+ people = @event.attendees.sort_by(:logins, :get => :logins, :limit => [0, 3], :order => "ALPHA")
584
+ assert %w[2 3 4] == people
573
585
  end
574
586
 
575
587
  # Collections initialized with a Model parameter
data/test/validations.rb CHANGED
@@ -103,7 +103,7 @@ end
103
103
  # An existing model with a valid name
104
104
  scope do
105
105
  setup do
106
- event = Event.create(:name => "original")
106
+ Event.create(:name => "original")
107
107
  end
108
108
 
109
109
  # That has the name changed
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: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-07-24 00:00:00.000000000 Z
14
+ date: 2012-08-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: redis
18
- requirement: &2152343760 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -23,10 +23,15 @@ dependencies:
23
23
  version: '0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2152343760
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
27
32
  - !ruby/object:Gem::Dependency
28
33
  name: nest
29
- requirement: &2152342280 !ruby/object:Gem::Requirement
34
+ requirement: !ruby/object:Gem::Requirement
30
35
  none: false
31
36
  requirements:
32
37
  - - ~>
@@ -34,10 +39,15 @@ dependencies:
34
39
  version: '1.0'
35
40
  type: :runtime
36
41
  prerelease: false
37
- version_requirements: *2152342280
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
38
48
  - !ruby/object:Gem::Dependency
39
49
  name: scrivener
40
- requirement: &2152341580 !ruby/object:Gem::Requirement
50
+ requirement: !ruby/object:Gem::Requirement
41
51
  none: false
42
52
  requirements:
43
53
  - - ~>
@@ -45,18 +55,28 @@ dependencies:
45
55
  version: 0.0.3
46
56
  type: :runtime
47
57
  prerelease: false
48
- version_requirements: *2152341580
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: 0.0.3
49
64
  - !ruby/object:Gem::Dependency
50
65
  name: cutest
51
- requirement: &2152340840 !ruby/object:Gem::Requirement
66
+ requirement: !ruby/object:Gem::Requirement
52
67
  none: false
53
68
  requirements:
54
69
  - - ~>
55
70
  - !ruby/object:Gem::Version
56
- version: '0.1'
71
+ version: '1.1'
57
72
  type: :development
58
73
  prerelease: false
59
- version_requirements: *2152340840
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: '1.1'
60
80
  description: Ohm is a library that allows to store an object in Redis, a persistent
61
81
  key-value database. It includes an extensible list of validations and has very good
62
82
  performance.
@@ -115,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
135
  version: '0'
116
136
  requirements: []
117
137
  rubyforge_project: ohm
118
- rubygems_version: 1.8.11
138
+ rubygems_version: 1.8.23
119
139
  signing_key:
120
140
  specification_version: 3
121
141
  summary: Object-hash mapping library for Redis.