ohm 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.markdown +43 -5
  2. data/lib/ohm.rb +24 -6
  3. data/test/model_test.rb +15 -1
  4. metadata +3 -3
data/README.markdown CHANGED
@@ -7,7 +7,7 @@ Object-hash mapping library for Redis.
7
7
  Description
8
8
  -----------
9
9
 
10
- Ohm is a library that allows to store an object in
10
+ Ohm is a library for storing objects in
11
11
  [Redis](http://code.google.com/p/redis/), a persistent key-value
12
12
  database. It includes an extensible list of validations and has very
13
13
  good performance.
@@ -28,6 +28,8 @@ If you don't have Ohm, try this:
28
28
 
29
29
  $ sudo gem install ohm
30
30
 
31
+ Or you can grab the code from [http://github.com/soveran/ohm](http://github.com/soveran/ohm).
32
+
31
33
  Now, in an irb session you can test the Redis adapter directly:
32
34
 
33
35
  >> require "ohm"
@@ -62,10 +64,25 @@ the example below:
62
64
  end
63
65
  end
64
66
 
67
+ All models have the `id` attribute built in, you don't need to declare it.
68
+
69
+ This is how you interact with ids:
70
+
71
+ event = Event.create :name => "Ohm Worldwide Conference 2031"
72
+ event.id
73
+ # => 1
74
+
75
+ # Find an event by id
76
+ event == Event[1]
77
+ # => true
78
+
79
+ # Trying to find a non existent event
80
+ Event[2]
81
+ # => nil
82
+
65
83
  This example shows some basic features, like attribute declarations and
66
84
  validations. Keep reading to find out what you can do with models.
67
85
 
68
-
69
86
  Attribute types
70
87
  ---------------
71
88
 
@@ -99,6 +116,24 @@ the value, but you can not assign it. In the example above, we used a
99
116
  counter attribute for tracking votes. As the incr and decr operations
100
117
  are atomic, you can rest assured a vote won't be counted twice.
101
118
 
119
+ Associations
120
+ ------------
121
+
122
+ Ohm lets you use collections (lists and sets) to represent associations.
123
+ For this, you only need to provide a second paramenter when declaring a
124
+ list or a set:
125
+
126
+ set :attendees, Person
127
+
128
+ After this, everytime you refer to `event.attendees` you will be talking
129
+ about instances of the model `Person`. If you want to get the raw values
130
+ of the set, you can use `event.attendees.raw`.
131
+
132
+ The `attendees` collection also exposes two sorting methods: `sort`
133
+ returns the elements ordered by `id`, and `sort_by` receives a parameter
134
+ with an attribute name, which will determine the sorting order. Both
135
+ methods receive an options hash which is explained in the documentation
136
+ for {Ohm::Attributes::Collection#sort}.
102
137
 
103
138
  Indexes
104
139
  -------
@@ -206,7 +241,8 @@ Given the following example:
206
241
 
207
242
  If all the assertions fail, the following errors will be present:
208
243
 
209
- obj.errors #=> [[:foo, :not_present], [:bar, :not_numeric], [:baz, :format], [[:qux], :not_unique]]
244
+ obj.errors
245
+ # => [[:foo, :not_present], [:bar, :not_numeric], [:baz, :format], [[:qux], :not_unique]]
210
246
 
211
247
  Note that the error for assert_unique wraps the field in an array.
212
248
  The purpose for this is to standardize the format for both single and
@@ -229,7 +265,8 @@ is as follows:
229
265
  e.on [:account, :not_present], "You must supply an account"
230
266
  end
231
267
 
232
- error_messages #=> ["Name must be present", "You must supply an account"]
268
+ error_messages
269
+ # => ["Name must be present", "You must supply an account"]
233
270
 
234
271
  Having the error message definitions in the views means you can use any
235
272
  sort of helpers. You can also use blocks instead of strings for the
@@ -241,4 +278,5 @@ values. The result of the block is used as the error message:
241
278
  end
242
279
  end
243
280
 
244
- error_messages #=> ["The email foo@example.com is already registered."]
281
+ error_messages
282
+ # => ["The email foo@example.com is already registered."]
data/lib/ohm.rb CHANGED
@@ -84,14 +84,32 @@ module Ohm
84
84
  instantiate(db.sort(key, options))
85
85
  end
86
86
 
87
- # Sort the model instances by the given value.
87
+ # Sort the model instances by the given attribute.
88
+ #
89
+ # @example Sorting elements by name:
90
+ #
91
+ # User.create :name => "B"
92
+ # User.create :name => "A"
93
+ #
94
+ # user = User.all.sort_by :name, :order => "ALPHA"
95
+ # user.name == "A" #=> true
88
96
  def sort_by(att, options = {})
89
97
  sort(options.merge(:by => model.key("*", att)))
90
98
  end
91
99
 
100
+ # Sort the model instances by id and return the first instance
101
+ # found. If a :by option is provided with a valid attribute name, the
102
+ # method sort_by is used instead and the option provided is passed as the
103
+ # first parameter.
104
+ #
105
+ # @see #sort
106
+ # @see #sort_by
92
107
  # @return [Ohm::Model, nil] Returns the first instance found or nil.
93
- def first
94
- sort(:limit => 1).first
108
+ def first(options = {})
109
+ options = options.merge(:limit => 1)
110
+ options[:by] ?
111
+ sort_by(options.delete(:by), options).first :
112
+ sort(options).first
95
113
  end
96
114
 
97
115
  def to_ary
@@ -172,8 +190,8 @@ module Ohm
172
190
  # company = Company.create :name => "Redis Co."
173
191
  # company.employees << "Albert"
174
192
  # company.employees << "Benoit"
175
- # company.employees.all #=> ["Albert", "Benoit"]
176
- # company.include?("Albert") #=> true
193
+ # company.employees.all #=> ["Albert", "Benoit"]
194
+ # company.include?("Albert") #=> true
177
195
  class Set < Collection
178
196
 
179
197
  # @param value [#to_s] Adds value to the list.
@@ -386,7 +404,7 @@ module Ohm
386
404
  end
387
405
 
388
406
  def save
389
- create if new?
407
+ return create if new?
390
408
  return unless valid?
391
409
  update_indices
392
410
  save!
data/test/model_test.rb CHANGED
@@ -186,6 +186,20 @@ class TestRedis < Test::Unit::TestCase
186
186
  Ohm.flush
187
187
  assert_equal [], Person.all.sort_by(:name)
188
188
  end
189
+
190
+ should "return the first element sorted by id when using first" do
191
+ Ohm.flush
192
+ Person.create :name => "A"
193
+ Person.create :name => "B"
194
+ assert_equal "A", Person.all.first.name
195
+ end
196
+
197
+ should "return the first element sorted by name if first receives a sorting option" do
198
+ Ohm.flush
199
+ Person.create :name => "B"
200
+ Person.create :name => "A"
201
+ assert_equal "A", Person.all.first(:by => :name, :order => "ALPHA").name
202
+ end
189
203
  end
190
204
 
191
205
  context "Loading attributes" do
@@ -399,7 +413,7 @@ class TestRedis < Test::Unit::TestCase
399
413
  end
400
414
 
401
415
  should "return instances of the passed model" do
402
- assert_equal Post, @user.posts.all.first.class
416
+ assert_equal Post, @user.posts.first.class
403
417
  end
404
418
  end
405
419
 
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: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-08-04 00:00:00 -03:00
13
+ date: 2009-08-25 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  requirements: []
66
66
 
67
67
  rubyforge_project: ohm
68
- rubygems_version: 1.3.5
68
+ rubygems_version: 1.3.4
69
69
  signing_key:
70
70
  specification_version: 3
71
71
  summary: Object-hash mapping library for Redis.