ohm 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -24,9 +24,20 @@ Related projects
24
24
  These are libraries in other languages that were inspired by Ohm.
25
25
 
26
26
  * [JOhm](https://github.com/xetorthio/johm) for Java, created by xetorthio
27
+ * [Lohm](https://github.com/slact/lua-ohm) for Lua, created by slact
27
28
  * [Nohm](https://github.com/maritz/nohm) for Node.js, created by maritz
28
29
  * [Redisco](https://github.com/iamteem/redisco) for Python, created by iamteem
29
30
 
31
+ Articles and Presentations
32
+ --------------------------
33
+
34
+ * [Simplicty](http://files.soveran.com/simplicity)
35
+ * [How to Redis](http://www.paperplanes.de/2009/10/30/how_to_redis.html)
36
+ * [Redis and Ohm](http://carlopecchia.eu/blog/2010/04/30/redis-and-ohm-part1/)
37
+ * [Ohm (Redis ORM)](http://blog.s21g.com/articles/1717) (Japanese)
38
+ * [Redis and Ohm](http://www.slideshare.net/awksedgreep/redis-and-ohm)
39
+ * [Ruby off Rails](http://www.slideshare.net/cyx.ucron/ruby-off-rails)
40
+
30
41
  Getting started
31
42
  ---------------
32
43
 
@@ -66,35 +77,42 @@ set the environment variable `REDIS_URL`.
66
77
 
67
78
  Here are the options for {Ohm.connect} in detail:
68
79
 
69
- **:url**
70
- : A Redis URL of the form `redis://:<passwd>@<host>:<port>/<db>`.
71
- Note that if you specify a URL and one of the other options at
72
- the same time, the other options will take precedence. Also, if
73
- you try and do `Ohm.connect` without any arguments, it will check
74
- if `ENV["REDIS_URL"]` is set, and will use it as the argument for
75
- `:url`.
80
+ ### :url
81
+
82
+ A Redis URL of the form `redis://:<passwd>@<host>:<port>/<db>`.
83
+ Note that if you specify a URL and one of the other options at
84
+ the same time, the other options will take precedence. Also, if
85
+ you try and do `Ohm.connect` without any arguments, it will check
86
+ if `ENV["REDIS_URL"]` is set, and will use it as the argument for
87
+ `:url`.
88
+
89
+ ### :host
90
+
91
+ Host where the Redis server is running, defaults to `"127.0.0.1"`.
92
+
93
+ ### :port
94
+
95
+ Port number, defaults to `6379`.
96
+
97
+ ### :db
76
98
 
77
- **:host**
78
- : Host where the Redis server is running, defaults to `"127.0.0.1"`.
99
+ Database number, defaults to `0`.
79
100
 
80
- **:port**
81
- : Port number, defaults to `6379`.
101
+ ### :password
82
102
 
83
- **:db**
84
- : Database number, defaults to `0`.
103
+ It is the secret that will be sent to the Redis server. Use it if the server
104
+ configuration requires it. Defaults to `nil`.
85
105
 
86
- **:password**
87
- : It is the secret that will be sent to the Redis server. Use it if the server
88
- configuration requires it. Defaults to `nil`.
106
+ ### :timeout
89
107
 
90
- **:timeout**
91
- : Database timeout in seconds, defaults to `0`.
108
+ Database timeout in seconds, defaults to `0`.
92
109
 
93
- **:thread_safe**
94
- : Initializes the client with a monitor. It has a small performance penalty, and
95
- it's off by default. For thread safety, it is recommended to use a different
96
- instance per thread. I you have no choice, then pass `:thread_safe => true`
97
- when connecting.
110
+ ### :thread_safe
111
+
112
+ Initializes the client with a monitor. It has a small performance penalty, and
113
+ it's off by default. For thread safety, it is recommended to use a different
114
+ instance per thread. I you have no choice, then pass `:thread_safe => true`
115
+ when connecting.
98
116
 
99
117
  Models
100
118
  ------
@@ -143,6 +161,10 @@ This is how you interact with IDs:
143
161
  Event[2]
144
162
  # => nil
145
163
 
164
+ # Finding all the events
165
+ Event.all
166
+ # => [#<Event @values={:id=>1, :name=>"Ohm Worldwide Conference 2031"}>]
167
+
146
168
  This example shows some basic features, like attribute declarations and
147
169
  validations. Keep reading to find out what you can do with models.
148
170
 
@@ -249,68 +271,73 @@ ordered by `id`, and {Ohm::Model::Collection#sort_by sort_by} receives
249
271
  a parameter with an attribute name, which will determine the sorting
250
272
  order. Both methods receive an options hash which is explained below:
251
273
 
252
- **:order**
253
- : Order direction and strategy. You can pass in any of
254
- the following:
274
+ ### :order
275
+
276
+ Order direction and strategy. You can pass in any of the following:
255
277
 
256
278
  1. ASC
257
279
  2. ASC ALPHA (or ALPHA ASC)
258
280
  3. DESC
259
281
  4. DESC ALPHA (or ALPHA DESC)
260
282
 
261
- It defaults to `ASC`.
283
+ It defaults to `ASC`.
284
+
285
+ ### :start
286
+
287
+ The offset from which we should start with. Note that
288
+ this is 0-indexed. It defaults to `0`.
289
+
290
+ ### :limit
291
+
292
+ The number of entries to get. If you don't pass in anything, it will
293
+ get all the results from the LIST or SET that you are sorting.
294
+
295
+ ### :by
262
296
 
263
- **:start**
264
- : The offset from which we should start with. Note that
265
- this is 0-indexed. It defaults to `0`.
297
+ Key or Hash key with which to sort by. An important distinction with
298
+ using {Ohm::Model::Collection#sort sort} and
299
+ {Ohm::Model::Collection#sort_by sort_by} is that `sort_by` automatically
300
+ converts the passed argument with the assumption that it is a hash key
301
+ and it's within the current model you are sorting.
266
302
 
267
- **:limit**
268
- : The number of entries to get. If you don't pass in anything, it will
269
- get all the results from the LIST or SET that you are sorting.
303
+ Post.all.sort_by(:title) # SORT Post:all BY Post:*->title
304
+ Post.all.sort(:by => :title) # SORT Post:all BY title
270
305
 
271
- **:by**
272
- : Key or Hash key with which to sort by. An important distinction with
273
- using {Ohm::Model::Collection#sort sort} and
274
- {Ohm::Model::Collection#sort_by sort_by} is that `sort_by` automatically
275
- converts the passed argument with the assumption that it is a hash key
276
- and it's within the current model you are sorting.
306
+ ### :get
277
307
 
278
- Post.all.sort_by(:title) # SORT Post:all BY Post:*->title
279
- Post.all.sort(:by => :title) # SORT Post:all BY title
308
+ A key pattern to return, e.g. `Post:*->title`. As is the case with
309
+ the `:by` option, using {Ohm::Model::Collection#sort sort} and
310
+ {Ohm::Model::Collection#sort_by sort_by} has distinct differences in
311
+ that `sort_by` does much of the hand-coding for you.
280
312
 
281
- **:get**
282
- : A key pattern to return, e.g. `Post:*->title`. As is the case with
283
- the `:by` option, using {Ohm::Model::Collection#sort sort} and
284
- {Ohm::Model::Collection#sort_by sort_by} has distinct differences in
285
- that `sort_by` does much of the hand-coding for you.
313
+ Post.all.sort_by(:title, :get => :title)
314
+ # SORT Post:all BY Post:*->title GET Post:*->title
286
315
 
287
- Post.all.sort_by(:title, :get => :title)
288
- # SORT Post:all BY Post:*->title GET Post:*->title
316
+ Post.all.sort(:by => :title, :get => :title)
317
+ # SORT Post:all BY title GET title
289
318
 
290
- Post.all.sort(:by => :title, :get => :title)
291
- # SORT Post:all BY title GET title
292
319
 
320
+ ### :store
293
321
 
294
- **:store**
295
- : An optional key which you may use to cache the sorted result. The key
296
- may or may not exist.
322
+ An optional key which you may use to cache the sorted result. The key
323
+ may or may not exist.
297
324
 
298
- This option can only be used together with `:get`.
325
+ This option can only be used together with `:get`.
299
326
 
300
- The type that is used for the STORE key is a LIST.
327
+ The type that is used for the STORE key is a LIST.
301
328
 
302
- Post.all.sort_by(:title, :store => "FOO")
329
+ Post.all.sort_by(:title, :store => "FOO")
303
330
 
304
- # Get all the results stored in FOO.
305
- Post.db.lrange("FOO", 0, -1)
331
+ # Get all the results stored in FOO.
332
+ Post.db.lrange("FOO", 0, -1)
306
333
 
307
- When using temporary values, it might be a good idea to use a `volatile`
308
- key. In Ohm, a volatile key means it just starts with a `~` character.
334
+ When using temporary values, it might be a good idea to use a `volatile`
335
+ key. In Ohm, a volatile key means it just starts with a `~` character.
309
336
 
310
- Post.all.sort_by(:title, :get => :title,
311
- :store => Post.key.volatile["FOO"])
337
+ Post.all.sort_by(:title, :get => :title,
338
+ :store => Post.key.volatile["FOO"])
312
339
 
313
- Post.key.volatile["FOO"].lrange 0, -1
340
+ Post.key.volatile["FOO"].lrange 0, -1
314
341
 
315
342
 
316
343
  Associations
@@ -353,8 +380,8 @@ the following:
353
380
  end
354
381
  end
355
382
 
356
- _(The only difference with the actual implementation is that the model
357
- is memoized.)_
383
+ The only difference with the actual implementation is that the model
384
+ is memoized.
358
385
 
359
386
  The net effect here is we can conveniently set and retrieve `Post` objects,
360
387
  and also search comments using the `post_id` index.
@@ -433,7 +460,7 @@ Note that calling these methods results in new sets being created
433
460
  on the fly. This is important so that you can perform further operations
434
461
  before reading the items to the client.
435
462
 
436
- For more information, see [SINTERSTORE](http://code.google.com/p/redis/wiki/SinterstoreCommand) and [SDIFFSTORE](http://code.google.com/p/redis/wiki/SdiffstoreCommand).
463
+ For more information, see [SINTERSTORE](http://redis.io/commands/sinterstore) and [SDIFFSTORE](http://redis.io/commands/sdiffstore).
437
464
 
438
465
  Validations
439
466
  -----------
@@ -548,7 +575,7 @@ Ohm Extensions
548
575
 
549
576
  Ohm is rather small and can be extended in many ways.
550
577
 
551
- A lot of amazing contributions are available at [Ohm Contrib](http://labs.sinefunc.com/ohm-contrib/doc/), make sure to check them if you need to extend Ohm's functionality.
578
+ A lot of amazing contributions are available at [Ohm Contrib](http://cyx.github.com/ohm-contrib/doc/), make sure to check them if you need to extend Ohm's functionality.
552
579
 
553
580
  Tutorials
554
581
  =========
data/Rakefile CHANGED
@@ -30,6 +30,53 @@ task :test do
30
30
  Cutest.run(Dir["test/*_test.rb"])
31
31
  end
32
32
 
33
+ desc "Generate documentation"
34
+ task :doc => [:yard, :rocco]
35
+
36
+ desc "Generated YARD documentation"
37
+ task :yard do
38
+ require "yard"
39
+
40
+ opts = []
41
+ opts.push("--protected")
42
+ opts.push("--no-private")
43
+ opts.push("--private")
44
+ opts.push("--title", "Ohm &mdash; Object-hash mapping library for Redis")
45
+
46
+ YARD::CLI::Yardoc.run(*opts)
47
+
48
+ Dir["doc/**/*.html"].each do |file|
49
+ contents = File.read(file)
50
+
51
+ contents.sub! %r{</body>}, <<-EOS
52
+ <script type="text/javascript">
53
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
54
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
55
+ </script>
56
+ <script type="text/javascript">
57
+ try {
58
+ var pageTracker = _gat._getTracker("UA-11356145-1");
59
+ pageTracker._trackPageview();
60
+ } catch(err) {}</script>
61
+ </body>
62
+ EOS
63
+
64
+ File.open(file, "w") { |f| f.write(contents) }
65
+ end
66
+ end
67
+
68
+ desc "Generate ROCCO documentation"
69
+ task :rocco do
70
+ `mkdir doc/examples` unless Dir.exists?("doc/examples")
71
+ `rocco examples/*.*`
72
+ `mv examples/*.html doc/examples`
73
+ end
74
+
75
+ desc "Deploy documentation"
76
+ task :deploy do
77
+ system "rsync --del -avz doc/* ohm.keyvalue.org:deploys/ohm.keyvalue.org/"
78
+ end
79
+
33
80
  namespace :examples do
34
81
  desc "Run all the examples"
35
82
  task :run do
data/lib/ohm.rb CHANGED
@@ -470,7 +470,7 @@ module Ohm
470
470
  # @see http://code.google.com/p/redis/wiki/SmembersCommand SMEMBERS
471
471
  # in Redis Command Reference.
472
472
  def each(&block)
473
- key.smembers.each { |id| block.call(model[id]) }
473
+ key.smembers.each { |id| block.call(model.to_proc[id]) }
474
474
  end
475
475
 
476
476
  # Convenient way to scope access to a predefined set, useful for access
@@ -784,7 +784,7 @@ module Ohm
784
784
  # @see http://code.google.com/p/redis/wiki/LrangeCommand LRANGE
785
785
  # in Redis Command Reference.
786
786
  def each(&block)
787
- key.lrange(0, -1).each { |id| block.call(model[id]) }
787
+ key.lrange(0, -1).each { |id| block.call(model.to_proc[id]) }
788
788
  end
789
789
 
790
790
  # Thin wrapper around *RPUSH*.
@@ -845,9 +845,9 @@ module Ohm
845
845
  def [](index, limit = nil)
846
846
  case [index, limit]
847
847
  when Pattern[Fixnum, Fixnum] then
848
- key.lrange(index, limit).collect { |id| model[id] }
848
+ key.lrange(index, limit).collect { |id| model.to_proc[id] }
849
849
  when Pattern[Range, nil] then
850
- key.lrange(index.first, index.last).collect { |id| model[id] }
850
+ key.lrange(index.first, index.last).collect { |id| model.to_proc[id] }
851
851
  when Pattern[Fixnum, nil] then
852
852
  model[key.lindex(index)]
853
853
  end
@@ -932,7 +932,7 @@ module Ohm
932
932
  end
933
933
  end
934
934
 
935
- # All validations which need to access the _Redis_ database goes here.
935
+ # All validations that need access to the _Redis_ database go here.
936
936
  # As of this writing, {Ohm::Model::Validations#assert_unique} is the only
937
937
  # assertion contained within this module.
938
938
  module Validations
@@ -1287,7 +1287,7 @@ module Ohm
1287
1287
 
1288
1288
  # @private Used for conveniently doing [1, 2].map(&Post) for example.
1289
1289
  def self.to_proc
1290
- Proc.new { |id| self[id] }
1290
+ lambda { |id| new(:id => id) }
1291
1291
  end
1292
1292
 
1293
1293
  # Returns a {Ohm::Model::Set set} containing all the members of a given
@@ -1714,8 +1714,8 @@ module Ohm
1714
1714
  # Post.connect(:port => 6380, :db => 2)
1715
1715
  #
1716
1716
  # @see file:README.html#connecting Ohm.connect options documentation.
1717
- def self.connect(*options)
1718
- self.db = Ohm.connection(*options)
1717
+ def self.connect(options = {})
1718
+ @options = options
1719
1719
  end
1720
1720
 
1721
1721
  # @return [Ohm::Key] A key scoped to the model which uses this object's
@@ -1803,7 +1803,9 @@ module Ohm
1803
1803
 
1804
1804
  # Provides access to the Redis database. This is shared accross all models and instances.
1805
1805
  def self.db
1806
- Ohm.threaded[self] || Ohm.redis
1806
+ return Ohm.redis unless defined?(@options)
1807
+
1808
+ Redis.connect(@options)
1807
1809
  end
1808
1810
 
1809
1811
  def self.db=(connection)
@@ -1999,4 +2001,3 @@ module Ohm
1999
2001
  end
2000
2002
  end
2001
2003
  end
2002
-
data/lib/ohm/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Ohm
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
data/test/model_test.rb CHANGED
@@ -659,6 +659,8 @@ class ::Calendar < Ohm::Model
659
659
  list :holidays, lambda { |v| Date.parse(v) }
660
660
  list :subscribers, lambda { |id| MyActiveRecordModel.find(id) }
661
661
  list :appointments, Appointment
662
+
663
+ set :events, lambda { |id| MyActiveRecordModel.find(id) }
662
664
  end
663
665
 
664
666
  class ::Appointment < Ohm::Model
@@ -673,6 +675,8 @@ setup do
673
675
  @calendar.holidays.key.rpush "2009-07-09"
674
676
 
675
677
  @calendar.subscribers << MyActiveRecordModel.find(1)
678
+
679
+ @calendar.events << MyActiveRecordModel.find(1)
676
680
  end
677
681
 
678
682
  test "apply a transformation" do
@@ -682,6 +686,24 @@ test "apply a transformation" do
682
686
  assert [MyActiveRecordModel.find(1)] == @calendar.subscribers.all
683
687
  end
684
688
 
689
+ test "doing an each on lists" do
690
+ arr = []
691
+ @calendar.subscribers.each do |sub|
692
+ arr << sub
693
+ end
694
+
695
+ assert [MyActiveRecordModel.find(1)] == arr
696
+ end
697
+
698
+ test "doing an each on sets" do
699
+ arr = []
700
+ @calendar.events.each do |sub|
701
+ arr << sub
702
+ end
703
+
704
+ assert [MyActiveRecordModel.find(1)] == arr
705
+ end
706
+
685
707
  test "allow lambdas in references" do
686
708
  appointment = Appointment.create(:subscriber => MyActiveRecordModel.find(1))
687
709
  assert MyActiveRecordModel.find(1) == appointment.subscriber
@@ -956,4 +978,4 @@ test "be persisted" do
956
978
  assert "hash" == Ohm.redis.type("SomeNamespace::Foo:1")
957
979
 
958
980
  assert "foo" == SomeNamespace::Foo[1].name
959
- end
981
+ end
metadata CHANGED
@@ -1,77 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 3
9
- version: 0.1.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Michel Martens
13
9
  - Damian Janowski
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2010-11-05 00:00:00 -03:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
13
+ date: 2012-03-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: nest
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2156327100 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
19
+ requirements:
27
20
  - - ~>
28
- - !ruby/object:Gem::Version
29
- segments:
30
- - 1
31
- - 0
32
- version: "1.0"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.0'
33
23
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: cutest
37
24
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *2156327100
26
+ - !ruby/object:Gem::Dependency
27
+ name: cutest
28
+ requirement: &2156326460 !ruby/object:Gem::Requirement
39
29
  none: false
40
- requirements:
30
+ requirements:
41
31
  - - ~>
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- - 1
46
- version: "0.1"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
47
34
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: batch
51
35
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *2156326460
37
+ - !ruby/object:Gem::Dependency
38
+ name: batch
39
+ requirement: &2156325940 !ruby/object:Gem::Requirement
53
40
  none: false
54
- requirements:
41
+ requirements:
55
42
  - - ~>
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- - 0
60
- - 1
43
+ - !ruby/object:Gem::Version
61
44
  version: 0.0.1
62
45
  type: :development
63
- version_requirements: *id003
64
- description: Ohm is a library that allows to store an object in Redis, a persistent key-value database. It includes an extensible list of validations and has very good performance.
65
- email:
46
+ prerelease: false
47
+ version_requirements: *2156325940
48
+ description: Ohm is a library that allows to store an object in Redis, a persistent
49
+ key-value database. It includes an extensible list of validations and has very good
50
+ performance.
51
+ email:
66
52
  - michel@soveran.com
67
53
  - djanowski@dimaion.com
68
54
  executables: []
69
-
70
55
  extensions: []
71
-
72
56
  extra_rdoc_files: []
73
-
74
- files:
57
+ files:
75
58
  - lib/ohm/compat-1.8.6.rb
76
59
  - lib/ohm/key.rb
77
60
  - lib/ohm/pattern.rb
@@ -97,37 +80,28 @@ files:
97
80
  - test/validations_test.rb
98
81
  - test/wrapper_test.rb
99
82
  - test/test.conf
100
- has_rdoc: true
101
83
  homepage: http://github.com/soveran/ohm
102
84
  licenses: []
103
-
104
85
  post_install_message:
105
86
  rdoc_options: []
106
-
107
- require_paths:
87
+ require_paths:
108
88
  - lib
109
- required_ruby_version: !ruby/object:Gem::Requirement
89
+ required_ruby_version: !ruby/object:Gem::Requirement
110
90
  none: false
111
- requirements:
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- segments:
115
- - 0
116
- version: "0"
117
- required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
96
  none: false
119
- requirements:
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- segments:
123
- - 0
124
- version: "0"
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
125
101
  requirements: []
126
-
127
102
  rubyforge_project: ohm
128
- rubygems_version: 1.3.7
103
+ rubygems_version: 1.8.10
129
104
  signing_key:
130
105
  specification_version: 3
131
106
  summary: Object-hash mapping library for Redis.
132
107
  test_files: []
133
-