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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/classy_enum/base.rb', line 61 def build(value, ={}) object = find(value) if object.nil? || ([:allow_blank] && object.nil?) return value unless value.blank? # Subclass the base class and make it behave like the value that it is object = Class.new(base_class) { instance_variable_set(:@option, value) delegate :blank?, :nil?, :to => :option }.new 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 |
# 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) Arel::Visitors::ToSql.class_eval do define_method "visit_#{klass.name.split('::').join('_')}", lambda {|value| quote(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
93 94 95 |
# File 'lib/classy_enum/base.rb', line 93 def owner(owner) define_method owner, lambda { @owner } end |