power_enum 0.4.5 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -5
- data/lib/active_record/acts/enumerated.rb +38 -8
- data/lib/power_enum/reflection.rb +2 -2
- data/lib/power_enum/schema/schema_statements.rb +19 -2
- metadata +17 -14
data/README.md
CHANGED
@@ -88,7 +88,7 @@ If you're using Rails prior to 3.1, your migration file will look something like
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def self.down
|
91
|
-
|
91
|
+
remove_enum :booking_status
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
@@ -124,7 +124,7 @@ Now, when you create your Booking model, your migration should create a referenc
|
|
124
124
|
|
125
125
|
There are two methods added to Rails migrations:
|
126
126
|
|
127
|
-
##### `create_enum(enum_name, options = {})`
|
127
|
+
##### `create_enum(enum_name, options = {}, &block)`
|
128
128
|
|
129
129
|
Creates a new enum table. `enum_name` will be automatically pluralized. The following options are supported:
|
130
130
|
|
@@ -135,6 +135,8 @@ Creates a new enum table. `enum_name` will be automatically pluralized. The fo
|
|
135
135
|
- [:active] Set this to `true` to have a boolean 'active' column generated. The 'active' column will have the options of NOT NULL and DEFAULT TRUE.
|
136
136
|
- [:timestamps] Set this to `true` to have the timestamp columns (created\_at and updated\_at) generated
|
137
137
|
|
138
|
+
You can also pass in a block that takes a table object as an argument, like `create_table`.
|
139
|
+
|
138
140
|
Example:
|
139
141
|
|
140
142
|
create_enum :booking_status
|
@@ -165,7 +167,21 @@ is the equivalent of
|
|
165
167
|
end
|
166
168
|
add_index :booking_statuses, [:booking_name], :unique => true
|
167
169
|
|
168
|
-
|
170
|
+
You can also customize the creation process by using a block:
|
171
|
+
|
172
|
+
create_enum :booking_status do |t|
|
173
|
+
t.boolean :first_booking, :null => false
|
174
|
+
end
|
175
|
+
|
176
|
+
is the equivalent of
|
177
|
+
|
178
|
+
create_table :booking_statuses do |t|
|
179
|
+
t.string :name, :null => false
|
180
|
+
t.boolean :first_booking, :null => false
|
181
|
+
end
|
182
|
+
add_index :booking_statuses, [:name], :unique => true
|
183
|
+
|
184
|
+
Notice that a unique index is automatically created on the specified name column.
|
169
185
|
|
170
186
|
##### `remove_enum(enum_name)`
|
171
187
|
|
@@ -224,8 +240,20 @@ Each enumeration model gets the following instance methods.
|
|
224
240
|
|
225
241
|
##### `===(arg)`
|
226
242
|
|
227
|
-
|
228
|
-
|
243
|
+
Behavior depends on the type of `arg`.
|
244
|
+
|
245
|
+
* If `arg` is `nil`, returns `false`.
|
246
|
+
* If `arg` is an instance of `Symbol`, `Fixnum` or `String`, returns the result of `BookingStatus[:foo] == BookingStatus[arg]`.
|
247
|
+
* If `arg` is an `Array`, returns `true` if any member of the array returns `true` for `===(arg)`, `false` otherwise.
|
248
|
+
* In all other cases, delegates to `===(arg)` of the superclass.
|
249
|
+
|
250
|
+
Examples:
|
251
|
+
|
252
|
+
BookingStatus[:foo] === :foo #Returns true
|
253
|
+
BookingStatus[:foo] === 'foo' #Returns true
|
254
|
+
BookingStatus[:foo] === :bar #Returns false
|
255
|
+
BookingStatus[:foo] === [:foo, :bar, :baz] #Returns true
|
256
|
+
BookingStatus[:foo] === nil #Returns false
|
229
257
|
|
230
258
|
You should note that defining an `:on_lookup_failure` method that raises an exception will cause `===` to also raise an exception for any lookup failure of `BookingStatus[arg]`.
|
231
259
|
|
@@ -235,6 +263,10 @@ You should note that defining an `:on_lookup_failure` method that raises an exce
|
|
235
263
|
|
236
264
|
Returns true if any element in the list returns true for `===(arg)`, false otherwise.
|
237
265
|
|
266
|
+
Example:
|
267
|
+
|
268
|
+
BookingStatus[:foo].in? :foo, :bar, :baz #Returns true
|
269
|
+
|
238
270
|
##### `name`
|
239
271
|
|
240
272
|
Returns the 'name' of the enum, i.e. the value in the `:name_column` attribute of the enumeration model.
|
@@ -129,55 +129,85 @@ module ActiveRecord
|
|
129
129
|
@name_column ||= read_inheritable_attribute( :acts_enumerated_name_column )
|
130
130
|
end
|
131
131
|
|
132
|
-
|
133
|
-
|
134
|
-
def all_by_id
|
135
|
-
|
136
|
-
@all_by_id = all.inject({}) { |memo, item| memo[item.id] = item; memo }.freeze
|
132
|
+
# ---Private methods---
|
133
|
+
|
134
|
+
def all_by_id
|
135
|
+
@all_by_id ||= all_by_attribute( :id )
|
137
136
|
end
|
137
|
+
private :all_by_id
|
138
138
|
|
139
139
|
def all_by_name
|
140
|
-
return @all_by_name if @all_by_name
|
141
140
|
begin
|
142
|
-
@all_by_name
|
141
|
+
@all_by_name ||= all_by_attribute( :name )
|
143
142
|
rescue NoMethodError => err
|
144
143
|
if err.name == name_column
|
145
144
|
raise TypeError, "#{self.name}: you need to define a '#{name_column}' column in the table '#{table_name}'"
|
146
145
|
end
|
147
146
|
raise
|
148
147
|
end
|
149
|
-
end
|
148
|
+
end
|
149
|
+
private :all_by_name
|
150
|
+
|
151
|
+
def all_by_attribute(attr)
|
152
|
+
all.inject({}) { |memo, item|
|
153
|
+
memo[item.send(attr)] = item
|
154
|
+
memo
|
155
|
+
}.freeze
|
156
|
+
end
|
157
|
+
private :all_by_attribute
|
150
158
|
|
151
159
|
def enforce_none(arg)
|
152
160
|
nil
|
153
161
|
end
|
162
|
+
private :enforce_none
|
154
163
|
|
155
164
|
def enforce_strict(arg)
|
156
165
|
raise_record_not_found(arg)
|
157
166
|
end
|
167
|
+
private :enforce_strict
|
158
168
|
|
159
169
|
def enforce_strict_literals(arg)
|
160
170
|
raise_record_not_found(arg) if (Fixnum === arg) || (Symbol === arg)
|
161
171
|
nil
|
162
172
|
end
|
173
|
+
private :enforce_strict_literals
|
163
174
|
|
164
175
|
def enforce_strict_ids(arg)
|
165
176
|
raise_record_not_found(arg) if Fixnum === arg
|
166
177
|
nil
|
167
178
|
end
|
179
|
+
private :enforce_strict_ids
|
168
180
|
|
169
181
|
def enforce_strict_symbols(arg)
|
170
182
|
raise_record_not_found(arg) if Symbol === arg
|
171
183
|
nil
|
172
184
|
end
|
185
|
+
private :enforce_strict_symbols
|
173
186
|
|
174
187
|
def raise_record_not_found(arg)
|
175
188
|
raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})"
|
176
189
|
end
|
190
|
+
private :raise_record_not_found
|
177
191
|
|
178
192
|
end
|
179
193
|
|
180
194
|
module InstanceMethods
|
195
|
+
# Behavior depends on the type of +arg+.
|
196
|
+
#
|
197
|
+
# * If +arg+ is +nil+, returns +false+.
|
198
|
+
# * If +arg+ is an instance of +Symbol+, +Fixnum+ or +String+, returns the result of +BookingStatus[:foo] == BookingStatus[arg]+.
|
199
|
+
# * If +arg+ is an +Array+, returns +true+ if any member of the array returns +true+ for +===(arg)+, +false+ otherwise.
|
200
|
+
# * In all other cases, delegates to +===(arg)+ of the superclass.
|
201
|
+
#
|
202
|
+
# Examples:
|
203
|
+
#
|
204
|
+
# BookingStatus[:foo] === :foo #Returns true
|
205
|
+
# BookingStatus[:foo] === 'foo' #Returns true
|
206
|
+
# BookingStatus[:foo] === :bar #Returns false
|
207
|
+
# BookingStatus[:foo] === [:foo, :bar, :baz] #Returns true
|
208
|
+
# BookingStatus[:foo] === nil #Returns false
|
209
|
+
#
|
210
|
+
# You should note that defining an +:on_lookup_failure+ method that raises an exception will cause +===+ to also raise an exception for any lookup failure of +BookingStatus[arg]+.
|
181
211
|
def ===(arg)
|
182
212
|
case arg
|
183
213
|
when nil
|
@@ -8,13 +8,13 @@ module PowerEnum::Reflection
|
|
8
8
|
reflections.values.grep(PowerEnum::Reflection::EnumerationReflection)
|
9
9
|
end
|
10
10
|
|
11
|
-
def reflect_on_enumerated enumerated
|
11
|
+
def reflect_on_enumerated( enumerated )
|
12
12
|
reflections[enumerated.to_sym].is_a?(PowerEnum::Reflection::EnumerationReflection) ? reflections[enumerated.to_sym] : nil
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
class EnumerationReflection < ActiveRecord::Reflection::MacroReflection
|
17
|
-
def initialize name, options, active_record
|
17
|
+
def initialize( name, options, active_record )
|
18
18
|
super :has_enumerated, name, options, active_record
|
19
19
|
end
|
20
20
|
|
@@ -31,6 +31,8 @@ module PowerEnum::Schema
|
|
31
31
|
# [:timestamps]
|
32
32
|
# Set this to <tt>true</tt> to have timestamp columns (created_at and updated_at) generated.
|
33
33
|
#
|
34
|
+
# You can also pass in a block that takes a table object as an argument, like <tt>create_table</tt>.
|
35
|
+
#
|
34
36
|
# ===== Examples
|
35
37
|
# ====== Basic Enum
|
36
38
|
# create_enum :connector_type
|
@@ -56,8 +58,20 @@ module PowerEnum::Schema
|
|
56
58
|
# end
|
57
59
|
# add_index :connector_types, [:connector], :unique => true
|
58
60
|
#
|
59
|
-
#
|
60
|
-
|
61
|
+
# ====== Customizing Enum with a block
|
62
|
+
# create_enum :connector_type, :description => true do |t|
|
63
|
+
# t.boolean :has_sound
|
64
|
+
# end
|
65
|
+
# is the equivalent of
|
66
|
+
# create_table :connector_types do |t|
|
67
|
+
# t.string :name, :null => false
|
68
|
+
# t.string :description
|
69
|
+
# t.boolean :has_sound
|
70
|
+
# end
|
71
|
+
# add_index :connector_types, [:connector], :unique => true
|
72
|
+
#
|
73
|
+
# Notice that a unique index is automatically created in each case on the proper name column.
|
74
|
+
def create_enum(enum_name, options = {}, &block)
|
61
75
|
enum_table_name = enum_name.pluralize
|
62
76
|
name_column = options[:name_column] || :name
|
63
77
|
generate_description = !!options[:description]
|
@@ -77,6 +91,9 @@ module PowerEnum::Schema
|
|
77
91
|
if generate_timestamps
|
78
92
|
t.timestamps
|
79
93
|
end
|
94
|
+
if block_given?
|
95
|
+
yield t
|
96
|
+
end
|
80
97
|
end
|
81
98
|
|
82
99
|
add_index enum_table_name, [name_column], :unique => true
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,11 +12,11 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2011-
|
15
|
+
date: 2011-11-12 00:00:00.000000000Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
19
|
-
requirement: &
|
19
|
+
requirement: &10818300 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: 3.0.0
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *10818300
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: jeweler
|
30
|
-
requirement: &
|
30
|
+
requirement: &10817360 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ! '>='
|
@@ -35,10 +35,10 @@ dependencies:
|
|
35
35
|
version: '0'
|
36
36
|
type: :development
|
37
37
|
prerelease: false
|
38
|
-
version_requirements: *
|
38
|
+
version_requirements: *10817360
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: rspec
|
41
|
-
requirement: &
|
41
|
+
requirement: &10816140 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ! '>='
|
@@ -46,10 +46,10 @@ dependencies:
|
|
46
46
|
version: '0'
|
47
47
|
type: :development
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
49
|
+
version_requirements: *10816140
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: sqlite3
|
52
|
-
requirement: &
|
52
|
+
requirement: &10813220 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
55
|
- - ! '>='
|
@@ -57,18 +57,18 @@ dependencies:
|
|
57
57
|
version: '0'
|
58
58
|
type: :development
|
59
59
|
prerelease: false
|
60
|
-
version_requirements: *
|
60
|
+
version_requirements: *10813220
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: genspec
|
63
|
-
requirement: &
|
63
|
+
requirement: &10812140 !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - =
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 0.2.1
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
|
-
version_requirements: *
|
71
|
+
version_requirements: *10812140
|
72
72
|
description: ! 'Power Enum allows you to treat instances of your ActiveRecord models
|
73
73
|
as though they were an enumeration of values.
|
74
74
|
|
@@ -115,6 +115,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
115
|
- - ! '>='
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: -2560307407996447465
|
118
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
122
|
none: false
|
120
123
|
requirements:
|