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
- .base_class ⇒ Object
- .base_class=(klass) ⇒ Object
-
.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).
Instance Method Summary collapse
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.
11 12 13 |
# File 'lib/classy_enum/base.rb', line 11 def allow_blank @allow_blank end |
#owner ⇒ Object
Returns the value of attribute owner.
11 12 13 |
# File 'lib/classy_enum/base.rb', line 11 def owner @owner end |
#serialize_as_json ⇒ Object
Returns the value of attribute serialize_as_json.
11 12 13 |
# File 'lib/classy_enum/base.rb', line 11 def serialize_as_json @serialize_as_json end |
Class Method Details
.base_class ⇒ Object
18 19 20 |
# File 'lib/classy_enum/base.rb', line 18 def base_class @base_class ||= superclass.base_class end |
.base_class=(klass) ⇒ Object
22 23 24 |
# File 'lib/classy_enum/base.rb', line 22 def base_class=(klass) @base_class = klass end |
.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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/classy_enum/base.rb', line 88 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
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/classy_enum/base.rb', line 26 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 {|*values| values[0] = values[0].to_s begin quoted(*values) rescue NoMethodError quote(*values) end } end Arel::Visitors::DepthFirst.class_eval do define_method visitor_method, lambda {|*values| values[0] = values[0].to_s terminal(*values) } 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
118 119 120 |
# File 'lib/classy_enum/base.rb', line 118 def owner(owner) define_method owner, lambda { @owner } end |
Instance Method Details
#base_class ⇒ Object
13 14 15 |
# File 'lib/classy_enum/base.rb', line 13 def base_class self.class.base_class end |