relix 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/HISTORY.md +4 -0
- data/README.md +16 -1
- data/lib/relix/index.rb +28 -9
- data/lib/relix/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MzI0ZGYzYjgyYjdiMjlkN2ExZjA3MTk0ZjRkYTM3YmZiMmFjMjJlYQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmZjNDU0NmUwMGI0NGY5OGE0ZjUxMTBkNmQyOGVjY2VhMTM2NmVhMA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NDhjZjgyY2RjYTIzMWU0YmMyZjI5MDEwNTg4ZmVkYTZkYjRkYjBhYTBiZDZk
|
10
|
+
Yjk4MWQ1YjE0MjFmOTc4YzlhZWFhZjNiN2I0MWIwMTdkMmVjMDYxODIzMWNj
|
11
|
+
Y2RhMDU2MDI0ZTVmM2FiNjRhY2EzMDg5MDMzM2JhZGQ2Y2M5ZTY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MjI1MDIyN2Y1ZDZiOGIwYjJlOGJlZDQ5NjNmZjdmMjc2ZWRmOWQ5NTAxNWNi
|
14
|
+
MTUzODBiZDM4NDdkZDI2ZTNkY2ZlNWYyNDI3NmE1MTk2N2YxZDNlYjFmNTA4
|
15
|
+
MjIyZGQ2MTk3NjdiMjAzMGQwYTc4ZWZhNmFkYzQ0MWE3ZDA5YzQ=
|
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -130,7 +130,6 @@ The from option is exclusive - it does not return or count the key you pass to i
|
|
130
130
|
|
131
131
|
Indexes are inherited up the Ruby ancestor chain, so you can for instance set the primary_key in a base class and then not have to re-declare it in each subclass.
|
132
132
|
|
133
|
-
|
134
133
|
### Multiple Value Indexes
|
135
134
|
|
136
135
|
Indexes can be built over multiple attributes:
|
@@ -146,6 +145,22 @@ When there are multiple attributes, they are specified in a hash:
|
|
146
145
|
{storage_state: 'cached', account_id: 'bob'}, limit: 10)
|
147
146
|
end
|
148
147
|
|
148
|
+
### Interrogative Accessors
|
149
|
+
|
150
|
+
Relix has some special handling for interrogative accessors (i.e. those ending with a `?`). When one is used, Relix is smart and does not require the `?` when querying:
|
151
|
+
|
152
|
+
class Account
|
153
|
+
include Relix
|
154
|
+
relix do
|
155
|
+
primary_key :key
|
156
|
+
multi :active_and_admin, on: %w(active? admin?)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
accounts = Account.lookup{|q| q[:active_and_admin].eq(active: true, admin: true)}
|
161
|
+
|
162
|
+
It also auto-casts `nil` to `false` and any other object to `true` for interrogative accessors, so that lookups work like you'd expect them to in a Ruby context. If you don't want to auto-casting, just alias your interrogative to a non-interrogative name and index on that instead.
|
163
|
+
|
149
164
|
### Space efficiency
|
150
165
|
|
151
166
|
Model attributes that are indexed on but that never change can be marked as immutable to prevent them being stored (since they don't have to be reindexed). The primary key is marked immutable by default, but other attributes can be as well:
|
data/lib/relix/index.rb
CHANGED
@@ -8,13 +8,32 @@ module Relix
|
|
8
8
|
@compact_kind ||= kind[0..0]
|
9
9
|
end
|
10
10
|
|
11
|
+
class Accessor
|
12
|
+
attr_reader :identifier
|
13
|
+
def initialize(name)
|
14
|
+
@accessor = name.to_s
|
15
|
+
if @accessor =~ /^(.+)\?$/
|
16
|
+
@identifier = $1
|
17
|
+
@interrogative = true
|
18
|
+
else
|
19
|
+
@identifier = @accessor
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def read(object)
|
24
|
+
result = object.send(@accessor)
|
25
|
+
result = !!result if @interrogative
|
26
|
+
result
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
11
30
|
attr_reader :model_name
|
12
|
-
def initialize(set, base_name,
|
31
|
+
def initialize(set, base_name, accessors, options={})
|
13
32
|
@set = set
|
14
33
|
@base_name = base_name
|
15
34
|
@model_name = @set.klass.name
|
16
|
-
@
|
17
|
-
@attribute_immutable =
|
35
|
+
@accessors = Array(accessors).collect{|a| Accessor.new(a)}
|
36
|
+
@attribute_immutable = options[:immutable_attribute]
|
18
37
|
@options = options
|
19
38
|
end
|
20
39
|
|
@@ -23,7 +42,7 @@ module Relix
|
|
23
42
|
end
|
24
43
|
|
25
44
|
def read(object)
|
26
|
-
@
|
45
|
+
@accessors.inject({}){|h,e| h[e.identifier] = e.read(object); h}
|
27
46
|
end
|
28
47
|
|
29
48
|
def read_normalized(object)
|
@@ -35,13 +54,13 @@ module Relix
|
|
35
54
|
when Hash
|
36
55
|
value.inject({}){|h, (k,v)| h[k.to_s] = v; h}
|
37
56
|
else
|
38
|
-
{@
|
57
|
+
{@accessors.first.identifier => value}
|
39
58
|
end
|
40
|
-
@
|
41
|
-
if value_hash.include?(
|
42
|
-
value_hash[
|
59
|
+
@accessors.collect do |accessor|
|
60
|
+
if value_hash.include?(accessor.identifier)
|
61
|
+
value_hash[accessor.identifier].to_s
|
43
62
|
else
|
44
|
-
raise MissingIndexValueError, "Missing #{
|
63
|
+
raise MissingIndexValueError, "Missing #{accessor.identifier} when looking up by #{name}"
|
45
64
|
end
|
46
65
|
end.join(":")
|
47
66
|
end
|
data/lib/relix/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathaniel Talbott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
prerelease: false
|