mcommons-enum_for 0.0.5 → 0.0.6

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.
Files changed (4) hide show
  1. data/README.rdoc +6 -4
  2. data/enum_for.gemspec +1 -1
  3. data/lib/enum_for.rb +49 -39
  4. metadata +1 -1
@@ -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 enabled models, valid states are able to be read for an attribute by calling the plural of
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
- == Predicate methods
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
- = TODO
97
+ == Notes
98
98
 
99
- * Add option for default value
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
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{enum_for}
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Mal McKay and Justin Leitgeb"]
@@ -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 follows:
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 this
13
- # attribute. Requires a column of type VARCHAR with the name of the first argument
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
- options = args.extract_options!
17
- additives = options[:add].respond_to?(:include?) ? options[:add] : ( options[:add].nil? ? [ ] : [ options[:add] ] )
18
- attribute = args.first
19
+ opts = args.extract_options!
20
+ attribute = args.first
19
21
 
20
- plural_attribute = attribute.to_s.pluralize
21
-
22
- # Accept both symbol and string forms for setting. They'll both be saved as strings in the DB anyway,
23
- # so no need to accept only one form in the validation.
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
- prefixed_values = options[:has].inject({}) do |res, elem|
31
- res.merge( [prefix, elem].compact.join('_') => elem.to_sym )
32
- end
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
- prefixed_values.each do |pv, v|
35
- const_set(pv.upcase, v) if additives.include?(:constants)
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 named scope for this value
38
- named_scope pv, :conditions => { attribute => v.to_s } if additives.include?(:named_scopes)
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 for values
41
- if additives.include?(:predicate_methods) && !self.instance_methods.include?(pv)
42
- define_method("#{pv}?") do
43
- read_attribute(attribute) == v.to_s
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
- end
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
- # Define a custom setter method which casts input to a String. Since we present the configuration
54
- # of enum_fu using Symbols, this makes sense to provide to the user.
55
- define_method("#{attribute}=") do |other|
56
- self[attribute] = other.to_s
57
- end
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}", options[:has])
70
+ class_variable_set(:"@@#{plural_attribute}", opts[:has])
61
71
  end
62
72
  end
63
73
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mcommons-enum_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mal McKay and Justin Leitgeb