Class: ClassyEnum::Base
- Inherits:
-
Object
- Object
- ClassyEnum::Base
- Includes:
- Collection, Conversion, Predicate, Translation, Comparable
- Defined in:
- lib/classy_enum/base.rb
Instance Attribute Summary collapse
-
#allow_blank ⇒ Object
Returns the value of attribute allow_blank.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#serialize_as_json ⇒ Object
Returns the value of attribute serialize_as_json.
Class Method Summary collapse
-
.build(value, options = {}) ⇒ Object
Used internally to build a new ClassyEnum child instance It is preferred that you use ChildClass.new instead.
- .inherited(klass) ⇒ Object
-
.owner(owner) ⇒ Object
DSL setter method for overriding reference to enum owner (ActiveRecord model).
Methods included from Collection
Methods included from Translation
Methods included from Predicate
Methods included from Conversion
#as_json, #to_i, #to_s, #to_sym
Instance Attribute Details
#allow_blank ⇒ Object
Returns the value of attribute allow_blank.
12 13 14 |
# File 'lib/classy_enum/base.rb', line 12 def allow_blank @allow_blank end |
#owner ⇒ Object
Returns the value of attribute owner.
12 13 14 |
# File 'lib/classy_enum/base.rb', line 12 def owner @owner end |
#serialize_as_json ⇒ Object
Returns the value of attribute serialize_as_json.
12 13 14 |
# File 'lib/classy_enum/base.rb', line 12 def serialize_as_json @serialize_as_json end |
Class Method Details
.build(value, options = {}) ⇒ Object
Used internally to build a new ClassyEnum child instance It is preferred that you use ChildClass.new instead
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
end
class Priority::Low < Priority
end
Priority.build(:low) # => Priority::Low.new
Priority.build(:invalid_option) # => :invalid_option
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/classy_enum/base.rb', line 67 def build(value, ={}) object = find(value) if object.nil? || ([:allow_blank] && object.nil?) if value.blank? object = build_null_object(value) else return value end end object.owner = [:owner] object.serialize_as_json = [:serialize_as_json] object.allow_blank = [:allow_blank] object end |
.inherited(klass) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/classy_enum/base.rb', line 15 def inherited(klass) return if klass.anonymous? if self == ClassyEnum::Base klass.base_class = klass else # Ensure subclasses follow expected naming conventions unless klass.name.start_with? "#{base_class.name}::" raise SubclassNameError, "subclass must be namespaced with #{base_class.name}::" end # Add visit_EnumMember methods to support validates_uniqueness_of with enum field # This is due to a bug in Rails where it uses the method result as opposed to the # database value for validation scopes. A fix will be released in Rails 4, but # this will remain until Rails 3.x is no longer prevalent. if defined?(Arel::Visitors::ToSql) visitor_method = "visit_#{klass.name.split('::').join('_')}" Arel::Visitors::ToSql.class_eval do define_method visitor_method, lambda {|value| quote(value.to_s) } end Arel::Visitors::DepthFirst.class_eval do define_method visitor_method, lambda {|value| terminal(value.to_s) } end end # Convert from MyEnumClass::NumberTwo to :number_two enum = klass.name.split('::').last.underscore.to_sym Predicate.define_predicate_method(klass, enum) klass.instance_variable_set('@option', enum) end super end |
.owner(owner) ⇒ Object
DSL setter method for overriding reference to enum owner (ActiveRecord model)
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
owner :alarm
end
class Priority::High < Priority
def send_alarm?
alarm.enabled?
end
end
97 98 99 |
# File 'lib/classy_enum/base.rb', line 97 def owner(owner) define_method owner, lambda { @owner } end |