power_enum 0.8.3 → 0.8.4
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.
- data/README.md +3 -3
- data/lib/active_record/acts/enumerated.rb +27 -2
- metadata +3 -19
data/README.md
CHANGED
@@ -217,7 +217,7 @@ is the equivalent of
|
|
217
217
|
class BookingStatus < ActiveRecord::Base
|
218
218
|
acts_as_enumerated :conditions => 'optional_sql_conditions',
|
219
219
|
:order => 'optional_sql_order_by',
|
220
|
-
:on_lookup_failure => :optional_class_method,
|
220
|
+
:on_lookup_failure => :optional_class_method, #This also works: lambda{ |arg| some_custom_action }
|
221
221
|
:name_column => 'optional_name_column' #If required, may override the default name column
|
222
222
|
end
|
223
223
|
|
@@ -239,8 +239,8 @@ an exception if the arg is a Fixnum or Symbol), `:enforce_strict_ids` (raises an
|
|
239
239
|
`:enforce_strict_symbols` (raises an exception if the arg is a Symbol).
|
240
240
|
|
241
241
|
The purpose of the `:on_lookup_failure` option is that a) under some circumstances a lookup failure is a Bad Thing and
|
242
|
-
action should be taken,
|
243
|
-
|
242
|
+
action should be taken, therefore b) a fallback action should be easily configurable. As of version 0.8.4, you can
|
243
|
+
also set `:on_lookup_failure` to a lambda that takes in a single argument (The arg that was passed to `[]`).
|
244
244
|
|
245
245
|
As of version 0.8.0, you can pass in multiple arguments to `[]`. This returns a list of enums corresponding to the
|
246
246
|
passed in values. Duplicates are filtered out. For example `BookingStatus[arg1, arg2, arg3]` would be equivalent to
|
@@ -23,7 +23,8 @@ module ActiveRecord
|
|
23
23
|
# exception if the arg is a Fixnum or Symbol), :enforce_strict_ids (raises and exception if the arg is a
|
24
24
|
# Fixnum) and :enforce_strict_symbols (raises an exception if the arg is a Symbol). The purpose of the
|
25
25
|
# :on_lookup_failure option is that a) under some circumstances a lookup failure is a Bad Thing and action
|
26
|
-
# should be taken, therefore b) a fallback action should be easily configurable.
|
26
|
+
# should be taken, therefore b) a fallback action should be easily configurable. You can also give it a
|
27
|
+
# lambda that takes in a single argument (The arg that was passed to +[]+).
|
27
28
|
# [:name_column]
|
28
29
|
# Override for the 'name' column. By default, assumed to be 'name'.
|
29
30
|
#
|
@@ -51,6 +52,14 @@ module ActiveRecord
|
|
51
52
|
# nil
|
52
53
|
# end
|
53
54
|
# end
|
55
|
+
#
|
56
|
+
# ====Example 4
|
57
|
+
# class BookingStatus < ActiveRecord::Base
|
58
|
+
# acts_as_enumerated :conditions => [:exclude => false],
|
59
|
+
# :order => 'created_at DESC',
|
60
|
+
# :on_lookup_failure => lambda { |arg| raise CustomError, "BookingStatus lookup failed; #{arg}" },
|
61
|
+
# :name_column => :status_code
|
62
|
+
# end
|
54
63
|
def acts_as_enumerated(options = {})
|
55
64
|
valid_keys = [:conditions, :order, :on_lookup_failure, :name_column]
|
56
65
|
options.assert_valid_keys(*valid_keys)
|
@@ -139,12 +148,28 @@ module ActiveRecord
|
|
139
148
|
else
|
140
149
|
raise TypeError, "#{self.name}[]: argument should be a String, Symbol or Fixnum but got a: #{arg.class.name}"
|
141
150
|
end
|
142
|
-
|
151
|
+
|
152
|
+
handle_lookup_failure(arg)
|
143
153
|
else
|
144
154
|
args.map{ |item| self[item] }.uniq
|
145
155
|
end
|
146
156
|
end
|
147
157
|
|
158
|
+
# Deals with a lookup failure for the given argument.
|
159
|
+
def handle_lookup_failure(arg)
|
160
|
+
if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
|
161
|
+
case lookup_failure_handler
|
162
|
+
when Proc
|
163
|
+
lookup_failure_handler.call(arg)
|
164
|
+
else
|
165
|
+
self.send(lookup_failure_handler, arg)
|
166
|
+
end
|
167
|
+
else
|
168
|
+
self.send(:enforce_none, arg)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
private :handle_lookup_failure
|
172
|
+
|
148
173
|
# Enum lookup by id
|
149
174
|
def lookup_id(arg)
|
150
175
|
all_by_id[arg]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-08-
|
15
|
+
date: 2012-08-23 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
@@ -94,22 +94,6 @@ dependencies:
|
|
94
94
|
- - ! '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 0.2.7
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: pry
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
none: false
|
101
|
-
requirements:
|
102
|
-
- - ! '>='
|
103
|
-
- !ruby/object:Gem::Version
|
104
|
-
version: '0'
|
105
|
-
type: :development
|
106
|
-
prerelease: false
|
107
|
-
version_requirements: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
|
-
requirements:
|
110
|
-
- - ! '>='
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
version: '0'
|
113
97
|
description: ! 'Power Enum allows you to treat instances of your ActiveRecord models
|
114
98
|
as though they were an enumeration of values.
|
115
99
|
|
@@ -159,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
143
|
version: '0'
|
160
144
|
segments:
|
161
145
|
- 0
|
162
|
-
hash:
|
146
|
+
hash: 4154336654451249829
|
163
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
148
|
none: false
|
165
149
|
requirements:
|