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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Yzc4YzAwNWQ2OTc0NmU4NjYxOTRjMzNiMTliNGYwZTQyM2E1NDYzOA==
4
+ MzI0ZGYzYjgyYjdiMjlkN2ExZjA3MTk0ZjRkYTM3YmZiMmFjMjJlYQ==
5
5
  data.tar.gz: !binary |-
6
- OWU3YjljMmY1NjZlNmZlMjBhYTk5MjFmOWY2ZmYxZjQxZjg1ZDljOQ==
6
+ YmZjNDU0NmUwMGI0NGY5OGE0ZjUxMTBkNmQyOGVjY2VhMTM2NmVhMA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzYzOTk2YjQyZWI3MjU1MjEyZmEyNmIwYjQxYmFjZWFlNWRjYWFmZmRlYzlh
10
- NjM2M2Y5ZjBhYjgxNGE5NzdiZjdkNmQ3ZjE1N2Y4NDFjZTI3Nzk1ZWY3YjUx
11
- YjY3YTRjMDA0YmI0MTc0NzFiNWYxZTAyODViZjFlZmMzZDhkZjA=
9
+ NDhjZjgyY2RjYTIzMWU0YmMyZjI5MDEwNTg4ZmVkYTZkYjRkYjBhYTBiZDZk
10
+ Yjk4MWQ1YjE0MjFmOTc4YzlhZWFhZjNiN2I0MWIwMTdkMmVjMDYxODIzMWNj
11
+ Y2RhMDU2MDI0ZTVmM2FiNjRhY2EzMDg5MDMzM2JhZGQ2Y2M5ZTY=
12
12
  data.tar.gz: !binary |-
13
- OTQ1MzljMWFhZjBjMjQ3ZWI5MGE4NDRmOWVkODQwNmRiMDdkYThlNjM2YTk5
14
- ZmE0Zjc0M2ZkYjI3MzE5MTI1NjNiYjk0NTA2YTc4NGJkZDg3MjAyNTAyNmU5
15
- NmMzZjEyMjJlN2FiN2M5MjdmZGExMmZhMTQxMDQzMDlkNjBkZmM=
13
+ MjI1MDIyN2Y1ZDZiOGIwYjJlOGJlZDQ5NjNmZjdmMjc2ZWRmOWQ5NTAxNWNi
14
+ MTUzODBiZDM4NDdkZDI2ZTNkY2ZlNWYyNDI3NmE1MTk2N2YxZDNlYjFmNTA4
15
+ MjIyZGQ2MTk3NjdiMjAzMGQwYTc4ZWZhNmFkYzQ0MWE3ZDA5YzQ=
data/HISTORY.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 1.5.0
2
+
3
+ * Add special handling for interrogative methods. (ntalbott)
4
+
1
5
  ### 1.4.1
2
6
 
3
7
  * Fix double #deindex_by_primary_key when there's a multi index. (ntalbott)
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, accessor, options={})
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
- @accessor = [accessor].flatten.collect{|a| a.to_s}
17
- @attribute_immutable = !!options[:immutable_attribute]
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
- @accessor.inject({}){|h,e| h[e] = object.send(e); h}
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
- {@accessor.first => value}
57
+ {@accessors.first.identifier => value}
39
58
  end
40
- @accessor.collect do |k|
41
- if value_hash.include?(k)
42
- value_hash[k].to_s
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 #{k} when looking up by #{name}"
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
@@ -1,3 +1,3 @@
1
1
  module Relix
2
- VERSION = "1.4.1"
2
+ VERSION = "1.5.0"
3
3
  end
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.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-02 00:00:00.000000000 Z
11
+ date: 2013-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  prerelease: false