mcommons-enum_for 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -4
- data/enum_for.gemspec +1 -1
- data/lib/enum_for.rb +49 -39
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -31,7 +31,7 @@ migration has created a column of type VARCHAR on the table "taco".
|
|
31
31
|
enum_for :state, :has => [ :new, :composed, :served, :eaten ]
|
32
32
|
end
|
33
33
|
|
34
|
-
For all enum_for
|
34
|
+
For all enum_for-enabled models, valid states are able to be read for an attribute by calling the plural of
|
35
35
|
the attribute name:
|
36
36
|
|
37
37
|
>> Taco.states
|
@@ -65,7 +65,7 @@ Afterwards, you may use the named scopes as follows. Remember to add the prefix
|
|
65
65
|
|
66
66
|
The prefix may be overridden or omitted by using the :prefix option.
|
67
67
|
|
68
|
-
|
68
|
+
=== Predicate methods
|
69
69
|
|
70
70
|
enum_for also creates predicate methods on instances of your class so that you can ask if they have any
|
71
71
|
of the given enumerable states. For example, if a Taco class has an enumerable type :taste with values
|
@@ -94,9 +94,11 @@ also omit the addition of the prefix by passing :prefix => nil to enum_for. Exam
|
|
94
94
|
This makes Taco respond to named scopes like Taco.wacky_composed instead of the default, which
|
95
95
|
would have prefixed the named_scopes with the name of the attribute.
|
96
96
|
|
97
|
-
|
97
|
+
== Notes
|
98
98
|
|
99
|
-
|
99
|
+
This plugin doesn't implement default values for a model. If this behavior is desired, you may be interested
|
100
|
+
in the default_value_for plugin (available http://github.com/FooBarWidget/default_value_for/tree/master),
|
101
|
+
which has been successfully tested with enum_for.
|
100
102
|
|
101
103
|
== Authors
|
102
104
|
|
data/enum_for.gemspec
CHANGED
data/lib/enum_for.rb
CHANGED
@@ -6,58 +6,68 @@ module EnumFor
|
|
6
6
|
|
7
7
|
module SingletonMethods
|
8
8
|
|
9
|
-
# Creates an enumerable attribute on an ActiveRecord model. Usage is as
|
9
|
+
# Creates an enumerable attribute on an ActiveRecord model. Usage is as
|
10
|
+
# follows:
|
11
|
+
#
|
10
12
|
# enum_for :state, :has => [ :new, :composed, :served, :eaten ]
|
13
|
+
#
|
11
14
|
# Any additional options will be passed directly to the ActiveRecord method
|
12
|
-
# :validates_inclusion_of, which is used to validate the assigned values to
|
13
|
-
# attribute. Requires a column of type VARCHAR with the name of the
|
14
|
-
# to +enum_for+.
|
15
|
+
# :validates_inclusion_of, which is used to validate the assigned values to
|
16
|
+
# this attribute. Requires a column of type VARCHAR with the name of the
|
17
|
+
# first argument to +enum_for+.
|
15
18
|
def enum_for(*args)
|
16
|
-
|
17
|
-
|
18
|
-
attribute = args.first
|
19
|
+
opts = args.extract_options!
|
20
|
+
attribute = args.first
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
all_valid_values = options[:has].map{|v| v.to_s} | options[:has].map{|v| v.to_sym}
|
25
|
-
|
26
|
-
validates_inclusion_of attribute, options.except(:has, :prefix, :add).merge(:in => all_valid_values )
|
27
|
-
|
28
|
-
prefix = options.has_key?(:prefix) ? options[:prefix] : attribute.to_s
|
22
|
+
attribute_s = attribute.to_s
|
23
|
+
|
24
|
+
additives = Array.wrap(opts[:add])
|
25
|
+
plural_attribute = attribute_s.pluralize
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
prefix = opts.has_key?(:prefix) ? opts[:prefix] : attribute_s
|
28
|
+
|
29
|
+
# Valid values are most likely Symbols anyway, but coerce them to be safe.
|
30
|
+
valid_symbols = opts[:has].map{|v| v.to_sym }
|
33
31
|
|
34
|
-
|
35
|
-
|
32
|
+
valid_symbols.each do |val_sym|
|
33
|
+
val_s = val_sym.to_s
|
34
|
+
|
35
|
+
prefixed_val = [ prefix, val_s ].compact.join('_')
|
36
36
|
|
37
|
-
# Create
|
38
|
-
|
37
|
+
# Create +optional+ constants
|
38
|
+
const_set(prefixed_val.upcase, val_sym) if additives.include?(:constants)
|
39
|
+
|
40
|
+
# Create +optional+ named scopes
|
41
|
+
named_scope prefixed_val, :conditions => { attribute => val_s } if additives.include?(:named_scopes)
|
39
42
|
|
40
|
-
# Create predicate methods
|
41
|
-
if additives.include?(:predicate_methods) && !self.instance_methods.include?(
|
42
|
-
define_method(
|
43
|
-
read_attribute(attribute) ==
|
44
|
-
end
|
43
|
+
# Create +optional+ predicate methods, but don't overwrite existing methods
|
44
|
+
if additives.include?(:predicate_methods) && !self.instance_methods.include?(prefixed_val)
|
45
|
+
define_method(prefixed_val + '?') do # def foo?
|
46
|
+
read_attribute(attribute) == val_s # read_attribute(:foo) == 'foo'
|
47
|
+
end # end
|
45
48
|
end
|
46
|
-
|
47
|
-
|
48
|
-
# Attributes returned are cast as Symbols from their VARCHAR representation in the DB.
|
49
|
-
define_method(attribute.to_s) do
|
50
|
-
self[attribute].to_sym unless self[attribute].nil?
|
49
|
+
|
51
50
|
end
|
52
51
|
|
53
|
-
#
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
52
|
+
# Accepts assignment both from String and Symbol form of valid values.
|
53
|
+
validates_inclusion_of attribute, opts.except(:has, :prefix, :add).
|
54
|
+
merge(:in => valid_symbols | valid_symbols.map{|s| s.to_s } )
|
55
|
+
|
56
|
+
# Custom reader method presents attribute value in Symbol form.
|
57
|
+
define_method(attribute_s) do # def foo
|
58
|
+
self[attribute].to_sym unless self[attribute].nil? # self[:foo].to_sym unless self[:foo].nil?
|
59
|
+
end # end
|
60
|
+
|
61
|
+
# Custom setter method casting all attribute input to String, allows
|
62
|
+
# assignment from Symbol form.
|
63
|
+
define_method(attribute_s + '=') do |other| # def foo=(other)
|
64
|
+
self[attribute] = other.to_s # self[foo] = other
|
65
|
+
end # end
|
58
66
|
|
67
|
+
# Make collection of all valid attribute Symbols available to user
|
68
|
+
# from plural name of attribute as class method.
|
59
69
|
cattr_reader plural_attribute.to_sym
|
60
|
-
class_variable_set(:"@@#{plural_attribute}",
|
70
|
+
class_variable_set(:"@@#{plural_attribute}", opts[:has])
|
61
71
|
end
|
62
72
|
end
|
63
73
|
|