redis-objects 0.2.1 → 0.3.0

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.
@@ -9,11 +9,16 @@ class Roster
9
9
  counter :available_slots, :start => 10
10
10
  counter :pitchers, :limit => :max_pitchers
11
11
  counter :basic
12
- counter :all_players_online, :global => true
13
12
  lock :resort, :timeout => 2
14
- value :starting_pitcher
15
- list :player_stats
16
- set :outfielders
13
+ value :starting_pitcher, :marshal => true
14
+ list :player_stats, :marshal => true
15
+ set :outfielders, :marshal => true
16
+
17
+ # global class counters
18
+ counter :total_players_online, :global => true
19
+ list :all_player_stats, :global => true
20
+ set :all_players_online, :global => true
21
+ value :last_player, :global => true
17
22
 
18
23
  def initialize(id=1) @id = id end
19
24
  def id; @id; end
@@ -43,11 +48,17 @@ describe Redis::Objects do
43
48
  @roster_3.outfielders.clear
44
49
  @roster.redis.del(UNIONSTORE_KEY)
45
50
  @roster.redis.del(INTERSTORE_KEY)
51
+ @roster.redis.del(DIFFSTORE_KEY)
52
+
53
+ Roster.total_players_online.reset
54
+ Roster.all_player_stats.clear
55
+ Roster.all_players_online.clear
56
+ Roster.last_player.delete
46
57
  end
47
58
 
48
59
  it "should provide a connection method" do
49
60
  Roster.redis.should == Redis::Objects.redis
50
- Roster.redis.should be_kind_of(Redis)
61
+ # Roster.redis.should be_kind_of(Redis)
51
62
  end
52
63
 
53
64
  it "should create counter accessors" do
@@ -97,6 +108,24 @@ describe Redis::Objects do
97
108
  Roster.get_counter(:available_slots, @roster.id).should == 10
98
109
  end
99
110
 
111
+ it "should support class-level increment/decrement of global counters" do
112
+ Roster.total_players_online.should == 0
113
+ Roster.total_players_online.increment.should == 1
114
+ Roster.total_players_online.decrement.should == 0
115
+ Roster.total_players_online.increment(3).should == 3
116
+ Roster.total_players_online.decrement(2).should == 1
117
+ Roster.total_players_online.reset.should be_true
118
+ Roster.total_players_online.should == 0
119
+
120
+ Roster.get_counter(:total_players_online).should == 0
121
+ Roster.increment_counter(:total_players_online).should == 1
122
+ Roster.increment_counter(:total_players_online, nil, 3).should == 4
123
+ Roster.decrement_counter(:total_players_online, nil, 2).should == 2
124
+ Roster.decrement_counter(:total_players_online).should == 1
125
+ Roster.reset_counter(:total_players_online).should == true
126
+ Roster.get_counter(:total_players_online).should == 0
127
+ end
128
+
100
129
  it "should take an atomic block for increment/decrement" do
101
130
  a = false
102
131
  @roster.available_slots.should == 10
@@ -253,12 +282,27 @@ describe Redis::Objects do
253
282
  end
254
283
  error.should be_kind_of(NoMethodError)
255
284
  end
285
+
286
+ it "should support obtain_lock as a class method" do
287
+ error = nil
288
+ begin
289
+ Roster.obtain_lock(:resort, 2) do
290
+ Roster.redis.get("roster:2:resort_lock").should_not be_nil
291
+ end
292
+ rescue => error
293
+ end
294
+
295
+ error.should be_nil
296
+ Roster.redis.get("roster:2:resort_lock").should be_nil
297
+ end
256
298
 
257
299
  it "should handle simple values" do
258
300
  @roster.starting_pitcher.should == nil
259
301
  @roster.starting_pitcher = 'Trevor Hoffman'
260
302
  @roster.starting_pitcher.should == 'Trevor Hoffman'
261
303
  @roster.starting_pitcher.get.should == 'Trevor Hoffman'
304
+ @roster.starting_pitcher = 'Tom Selleck'
305
+ @roster.starting_pitcher.should == 'Tom Selleck'
262
306
  @roster.starting_pitcher.del.should be_true
263
307
  @roster.starting_pitcher.should be_nil
264
308
  end
@@ -409,6 +453,162 @@ describe Redis::Objects do
409
453
  @roster_1.redis.smembers(UNIONSTORE_KEY).sort.should == ['a','b','c','d','e','f','g','l','m']
410
454
  end
411
455
 
456
+ it "should handle class-level global lists of simple values" do
457
+ Roster.all_player_stats.should be_empty
458
+ Roster.all_player_stats << 'a'
459
+ Roster.all_player_stats.should == ['a']
460
+ Roster.all_player_stats.get.should == ['a']
461
+ Roster.all_player_stats.unshift 'b'
462
+ Roster.all_player_stats.to_s.should == 'b, a'
463
+ Roster.all_player_stats.should == ['b','a']
464
+ Roster.all_player_stats.get.should == ['b','a']
465
+ Roster.all_player_stats.push 'c'
466
+ Roster.all_player_stats.should == ['b','a','c']
467
+ Roster.all_player_stats.get.should == ['b','a','c']
468
+ Roster.all_player_stats.first.should == 'b'
469
+ Roster.all_player_stats.last.should == 'c'
470
+ Roster.all_player_stats << 'd'
471
+ Roster.all_player_stats.should == ['b','a','c','d']
472
+ Roster.all_player_stats[1].should == 'a'
473
+ Roster.all_player_stats[0].should == 'b'
474
+ Roster.all_player_stats[2].should == 'c'
475
+ Roster.all_player_stats[3].should == 'd'
476
+ Roster.all_player_stats.include?('c').should be_true
477
+ Roster.all_player_stats.include?('no').should be_false
478
+ Roster.all_player_stats.pop.should == 'd'
479
+ Roster.all_player_stats[0].should == Roster.all_player_stats.at(0)
480
+ Roster.all_player_stats[1].should == Roster.all_player_stats.at(1)
481
+ Roster.all_player_stats[2].should == Roster.all_player_stats.at(2)
482
+ Roster.all_player_stats.should == ['b','a','c']
483
+ Roster.all_player_stats.get.should == ['b','a','c']
484
+ Roster.all_player_stats.shift.should == 'b'
485
+ Roster.all_player_stats.should == ['a','c']
486
+ Roster.all_player_stats.get.should == ['a','c']
487
+ Roster.all_player_stats << 'e' << 'f' << 'e'
488
+ Roster.all_player_stats.should == ['a','c','e','f','e']
489
+ Roster.all_player_stats.get.should == ['a','c','e','f','e']
490
+ Roster.all_player_stats.delete('e').should == 2
491
+ Roster.all_player_stats.should == ['a','c','f']
492
+ Roster.all_player_stats.get.should == ['a','c','f']
493
+ Roster.all_player_stats << 'j'
494
+ Roster.all_player_stats.should == ['a','c','f','j']
495
+ Roster.all_player_stats[0..2].should == ['a','c','f']
496
+ Roster.all_player_stats[1, 3].should == ['c','f','j']
497
+ Roster.all_player_stats.length.should == 4
498
+ Roster.all_player_stats.size.should == 4
499
+ Roster.all_player_stats.should == ['a','c','f','j']
500
+ Roster.all_player_stats.get.should == ['a','c','f','j']
501
+
502
+ i = -1
503
+ Roster.all_player_stats.each do |st|
504
+ st.should == Roster.all_player_stats[i += 1]
505
+ end
506
+ Roster.all_player_stats.should == ['a','c','f','j']
507
+ Roster.all_player_stats.get.should == ['a','c','f','j']
508
+
509
+ Roster.all_player_stats.each_with_index do |st,i|
510
+ st.should == Roster.all_player_stats[i]
511
+ end
512
+ Roster.all_player_stats.should == ['a','c','f','j']
513
+ Roster.all_player_stats.get.should == ['a','c','f','j']
514
+
515
+ coll = Roster.all_player_stats.collect{|st| st}
516
+ coll.should == ['a','c','f','j']
517
+ Roster.all_player_stats.should == ['a','c','f','j']
518
+ Roster.all_player_stats.get.should == ['a','c','f','j']
519
+
520
+ Roster.all_player_stats << 'a'
521
+ coll = Roster.all_player_stats.select{|st| st == 'a'}
522
+ coll.should == ['a','a']
523
+ Roster.all_player_stats.should == ['a','c','f','j','a']
524
+ Roster.all_player_stats.get.should == ['a','c','f','j','a']
525
+ end
526
+
527
+ it "should handle class-level global sets of simple values" do
528
+ Roster.all_players_online.should be_empty
529
+ Roster.all_players_online << 'a' << 'a' << 'a'
530
+ Roster.all_players_online.should == ['a']
531
+ Roster.all_players_online.get.should == ['a']
532
+ Roster.all_players_online << 'b' << 'b'
533
+ Roster.all_players_online.to_s.should == 'a, b'
534
+ Roster.all_players_online.should == ['a','b']
535
+ Roster.all_players_online.members.should == ['a','b']
536
+ Roster.all_players_online.get.should == ['a','b']
537
+ Roster.all_players_online << 'c'
538
+ Roster.all_players_online.sort.should == ['a','b','c']
539
+ Roster.all_players_online.get.sort.should == ['a','b','c']
540
+ Roster.all_players_online.delete('c')
541
+ Roster.all_players_online.should == ['a','b']
542
+ Roster.all_players_online.get.sort.should == ['a','b']
543
+ Roster.all_players_online.length.should == 2
544
+ Roster.all_players_online.size.should == 2
545
+
546
+ i = 0
547
+ Roster.all_players_online.each do |st|
548
+ i += 1
549
+ end
550
+ i.should == Roster.all_players_online.length
551
+
552
+ coll = Roster.all_players_online.collect{|st| st}
553
+ coll.should == ['a','b']
554
+ Roster.all_players_online.should == ['a','b']
555
+ Roster.all_players_online.get.should == ['a','b']
556
+
557
+ Roster.all_players_online << 'c'
558
+ Roster.all_players_online.member?('c').should be_true
559
+ Roster.all_players_online.include?('c').should be_true
560
+ Roster.all_players_online.member?('no').should be_false
561
+ coll = Roster.all_players_online.select{|st| st == 'c'}
562
+ coll.should == ['c']
563
+ Roster.all_players_online.sort.should == ['a','b','c']
564
+ end
565
+
566
+ it "should handle class-level global values" do
567
+ Roster.last_player.should == nil
568
+ Roster.last_player = 'Trevor Hoffman'
569
+ Roster.last_player.should == 'Trevor Hoffman'
570
+ Roster.last_player.get.should == 'Trevor Hoffman'
571
+ Roster.last_player = 'Tom Selleck'
572
+ Roster.last_player.should == 'Tom Selleck'
573
+ Roster.last_player.del.should be_true
574
+ Roster.last_player.should be_nil
575
+ end
576
+
577
+ it "should easily enable @object.class.global_objects" do
578
+ @roster.class.all_players_online.should be_empty
579
+ @roster.class.all_players_online << 'a' << 'a' << 'a'
580
+ @roster.class.all_players_online.should == ['a']
581
+ @roster2.class.all_players_online.should == ['a']
582
+
583
+ @roster.all_players_online.should == ['a']
584
+ @roster2.all_players_online.should == ['a']
585
+
586
+ @roster.class.all_player_stats.should be_empty
587
+ @roster.class.all_player_stats << 'a'
588
+ @roster.class.all_player_stats.should == ['a']
589
+ @roster.class.all_player_stats.get.should == ['a']
590
+ @roster.class.all_player_stats.unshift 'b'
591
+ @roster.class.all_player_stats.to_s.should == 'b, a'
592
+ @roster.class.all_player_stats.should == ['b','a']
593
+ @roster2.class.all_player_stats.should == ['b','a']
594
+
595
+ @roster.all_player_stats.should == ['b','a']
596
+ @roster2.all_player_stats.should == ['b','a']
597
+ @roster2.all_player_stats << 'b'
598
+ @roster.all_player_stats.should == ['b','a','b']
599
+
600
+ @roster.last_player.should == nil
601
+ @roster.class.last_player = 'Trevor Hoffman'
602
+ @roster.last_player.should == 'Trevor Hoffman'
603
+ @roster.last_player.get.should == 'Trevor Hoffman'
604
+ @roster2.last_player.get.should == 'Trevor Hoffman'
605
+ @roster2.last_player = 'Tom Selleck'
606
+ @roster.last_player.should == 'Tom Selleck'
607
+ @roster.last_player.del.should be_true
608
+ @roster.last_player.should be_nil
609
+ @roster2.last_player.should be_nil
610
+ end
611
+
412
612
  it "should handle lists of complex data types" do
413
613
  @roster.player_stats << {:json => 'data'}
414
614
  @roster.player_stats << {:json2 => 'data2'}
@@ -448,4 +648,5 @@ describe Redis::Objects do
448
648
  error.should_not be_nil
449
649
  error.should be_kind_of(Redis::Lock::LockTimeout)
450
650
  end
651
+
451
652
  end
data/spec/spec_helper.rb CHANGED
@@ -5,3 +5,4 @@ $redis = Redis.new(:host => ENV['REDIS_HOST'], :port => ENV['REDIS_PORT'])
5
5
 
6
6
  UNIONSTORE_KEY = 'test:unionstore'
7
7
  INTERSTORE_KEY = 'test:interstore'
8
+ DIFFSTORE_KEY = 'test:diffstore'
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-objects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Nate Wiger
@@ -9,19 +14,23 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-11-27 00:00:00 -08:00
17
+ date: 2010-04-14 00:00:00 -07:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: redis
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
23
- version: "0.1"
24
- version:
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 3
31
+ version: 1.0.3
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  description: Map Redis types directly to Ruby objects. Works with any class or ORM.
26
35
  email: nate@wiger.org
27
36
  executables: []
@@ -30,25 +39,29 @@ extensions: []
30
39
 
31
40
  extra_rdoc_files:
32
41
  - ATOMICITY.rdoc
42
+ - CHANGELOG.rdoc
33
43
  - README.rdoc
34
44
  files:
35
45
  - lib/redis/counter.rb
46
+ - lib/redis/helpers/core_commands.rb
47
+ - lib/redis/helpers/serialize.rb
36
48
  - lib/redis/list.rb
37
49
  - lib/redis/lock.rb
38
- - lib/redis/objects/core.rb
39
50
  - lib/redis/objects/counters.rb
40
51
  - lib/redis/objects/lists.rb
41
52
  - lib/redis/objects/locks.rb
42
53
  - lib/redis/objects/sets.rb
54
+ - lib/redis/objects/sorted_sets.rb
43
55
  - lib/redis/objects/values.rb
44
56
  - lib/redis/objects.rb
45
- - lib/redis/serialize.rb
46
57
  - lib/redis/set.rb
58
+ - lib/redis/sorted_set.rb
47
59
  - lib/redis/value.rb
48
60
  - spec/redis_objects_instance_spec.rb
49
61
  - spec/redis_objects_model_spec.rb
50
62
  - spec/spec_helper.rb
51
63
  - ATOMICITY.rdoc
64
+ - CHANGELOG.rdoc
52
65
  - README.rdoc
53
66
  has_rdoc: true
54
67
  homepage: http://github.com/nateware/redis-objects
@@ -64,18 +77,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
77
  requirements:
65
78
  - - ">="
66
79
  - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
67
82
  version: "0"
68
- version:
69
83
  required_rubygems_version: !ruby/object:Gem::Requirement
70
84
  requirements:
71
85
  - - ">="
72
86
  - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
73
89
  version: "0"
74
- version:
75
90
  requirements:
76
- - redis, v0.1 or greater
91
+ - redis, v1.0.3 or greater
77
92
  rubyforge_project: redis-objects
78
- rubygems_version: 1.3.5
93
+ rubygems_version: 1.3.6
79
94
  signing_key:
80
95
  specification_version: 3
81
96
  summary: Maps Redis types to Ruby objects
File without changes
@@ -1,23 +0,0 @@
1
- class Redis
2
- module Serialize
3
- include Marshal
4
-
5
- def to_redis(value)
6
- case value
7
- when String, Fixnum, Bignum, Float
8
- value
9
- else
10
- dump(value)
11
- end
12
- end
13
-
14
- def from_redis(value)
15
- case value
16
- when Array
17
- value.collect{|v| from_redis(v)}
18
- else
19
- restore(value) rescue value
20
- end
21
- end
22
- end
23
- end