offline_lookup 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/offline_lookup.rb +6 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2db35bb5e31d4cfde77e91c6f67988c5cdf6dfe9
|
4
|
+
data.tar.gz: 7743e8b15d2f1c93f121b99f5cb9cca0d302a2ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c1f72f28029f3d4e7d9bcad89996e3fc962147e886f9cdad6262b2e86b4fc3725aff4cfc6116e18a61312ed0d578f9cd0f29943da7c30f95a33b34a27161846
|
7
|
+
data.tar.gz: c426d15af5a5a675f28aada0d1acb760ec3e68f83bc521f3b8e565d6df4f73310ab643f8f0fc342dc7a83af23dd33fc8943333af1e66c4501d9b00290b784a3c
|
data/lib/offline_lookup.rb
CHANGED
@@ -119,14 +119,17 @@ module OfflineLookup
|
|
119
119
|
### define value-named methods such as :two_hour_id and :two_hour?
|
120
120
|
|
121
121
|
self.offline_lookup_values.each do |key, value|
|
122
|
+
# class method: get key value (e.g. FooType.bar_id)
|
122
123
|
define_singleton_method(builder.key_method_name(value)) do
|
123
124
|
key
|
124
125
|
end
|
125
126
|
|
126
|
-
|
127
|
+
# instance method: true if instance is of named type (e.g. FooType.first.bar?)
|
128
|
+
define_method(builder.indentiy_method_name(value)) do
|
127
129
|
self.attributes[self.offline_lookup_options[:key]] == key
|
128
130
|
end
|
129
131
|
|
132
|
+
# class method: get instance by named method (e.g. FooType.bar)
|
130
133
|
# not "Offline", but lookup by indexed key. Also, synactic sugar.
|
131
134
|
if self.offline_lookup_options[:lookup_methods]
|
132
135
|
define_singleton_method(builder.lookup_method_name(value)) do
|
@@ -138,24 +141,17 @@ module OfflineLookup
|
|
138
141
|
|
139
142
|
|
140
143
|
### define statically-named methods where you pass in the named value, e.g., id_for_name(:two_hour)
|
141
|
-
|
144
|
+
# e.g. FooType.name_for_id(1)
|
142
145
|
define_singleton_method(builder.field_for_key_method_name) do |key_value|
|
143
146
|
self.offline_lookup_values[key_value]
|
144
147
|
end
|
145
148
|
|
149
|
+
# e.g. FooType.id_for_name("Bar")
|
146
150
|
define_singleton_method(builder.key_for_field_method_name) do |field_value|
|
147
151
|
self.offline_lookup_values.find{|k, v| v.to_s == field_value.to_s}.first
|
148
152
|
end
|
149
153
|
|
150
154
|
end
|
151
|
-
|
152
|
-
module ClassMethods
|
153
|
-
def quick_lookup(value)
|
154
|
-
key = self.offline_lookup_values.find{|k, v| v.to_s == value.to_s}.first
|
155
|
-
find(key)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
155
|
end
|
160
156
|
end
|
161
157
|
|