redis-attrs 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -133,6 +133,23 @@ it must be declared with the singular version of the method `redis_attrs`, like
133
133
  For more details about the supported configuration options for each of the complex
134
134
  data types, please refer to the [redis-objects][redis-objects] gem.
135
135
 
136
+ ### Filtering collection values
137
+
138
+ There's an attribute configuration option for lists and sets, the `:filter` option,
139
+ that allows the user to define a function that will modify the items upon insertion
140
+ into the collection.
141
+
142
+ class Film
143
+ redis_attr :genres, :set, :filter => lambda { |v| v.strip.downcase.gsub(/\s+/, ' ') }
144
+ end
145
+
146
+ After the above declaration we could do:
147
+
148
+ >> film = Film.new(1)
149
+ >> film.genres = ["Action ", " drama", "film Noir", "Drama", "Film noir "]
150
+ >> puts film.genres.members.sort
151
+ ["action", "drama", "film noir"]
152
+
136
153
  ## Contributing
137
154
 
138
155
  1. Fork it
@@ -4,6 +4,7 @@ require "redis/set"
4
4
  require "redis/sorted_set"
5
5
  require "redis/lock"
6
6
  require "redis/counter"
7
+ require "redis-attrs/objects_extensions"
7
8
 
8
9
  class Redis
9
10
  module Attrs
@@ -36,8 +37,8 @@ class Redis
36
37
  lock: Redis::Lock,
37
38
  counter: Redis::Counter,
38
39
  hash: Redis::HashKey,
39
- list: Redis::List,
40
- set: Redis::Set,
40
+ list: Redis::Attrs::FilteredList,
41
+ set: Redis::Attrs::FilteredSet,
41
42
  sorted_set: Redis::SortedSet,
42
43
  }
43
44
  end
@@ -0,0 +1,57 @@
1
+ class Redis
2
+ module Attrs
3
+
4
+ class FilteredList < Redis::List
5
+ def push(value)
6
+ value = options[:filter].call(value) if options[:filter]
7
+ super
8
+ end
9
+
10
+ # Add a member before or after pivot in the list. Redis: LINSERT
11
+ def insert(where,pivot,value)
12
+ if options[:filter]
13
+ value = options[:filter].call(value)
14
+ pivot = options[:filter].call(pivot)
15
+ end
16
+ super
17
+ end
18
+
19
+ # Add a member to the start of the list. Redis: LPUSH
20
+ def unshift(value)
21
+ value = options[:filter].call(value) if options[:filter]
22
+ super
23
+ end
24
+
25
+ def delete(name, count=0)
26
+ name = options[:filter].call(name) if options[:filter]
27
+ super
28
+ end
29
+ end
30
+
31
+ class FilteredSet < Redis::Set
32
+ def add(value)
33
+ value = options[:filter].call(value) if options[:filter]
34
+ super
35
+ end
36
+
37
+ def merge(*values)
38
+ if options[:filter]
39
+ filter = options[:filter]
40
+ values = values.map { |value| filter.call(value) }
41
+ end
42
+ super
43
+ end
44
+
45
+ def member?(value)
46
+ value = options[:filter].call(value) if options[:filter]
47
+ super
48
+ end
49
+
50
+ def delete(value)
51
+ value = options[:filter].call(value) if options[:filter]
52
+ super
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  class Redis
2
2
  module Attrs
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -168,5 +168,15 @@ describe Redis::Attrs do
168
168
  Film.redis_attr :watching, :lock, :expiration => 3.hours
169
169
  film.watching.options[:expiration].should == 3.hours
170
170
  end
171
+
172
+ it "supports filtering the values inserted into a list or set" do
173
+ Film.redis_attr :genres, :set, filter: lambda { |genre| genre.strip.downcase.gsub(/\s+/, ' ') }
174
+ film.genres = ["Action ", " drama", "film Noir", "Drama", "Film noir "]
175
+ film.genres.members.sort.should == ["action", "drama", "film noir"]
176
+ film.genres << " ACTION " << "Western"
177
+ film.genres.should_not include("Western")
178
+ film.genres.should include("western")
179
+ film.genres.members.sort.should == ["action", "drama", "film noir", "western"]
180
+ end
171
181
  end
172
182
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-attrs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -98,6 +98,7 @@ files:
98
98
  - lib/redis-attrs/hash.rb
99
99
  - lib/redis-attrs/integer.rb
100
100
  - lib/redis-attrs/list.rb
101
+ - lib/redis-attrs/objects_extensions.rb
101
102
  - lib/redis-attrs/scalar.rb
102
103
  - lib/redis-attrs/string.rb
103
104
  - lib/redis-attrs/time.rb