module Arkaan::Concerns::Enumerable::ClassMethods
Submodule holding all the static methods add to the current subclass. @author Vincent Courtois <courtois.vincent@outlook.com>
Public Instance Methods
define_getter(field_name)
click to toggle source
# File lib/arkaan/concerns/enumerable.rb, line 33 def define_getter(field_name) define_method field_name do return self["enum_#{field_name}"] end end
define_setter(field_name, values)
click to toggle source
# File lib/arkaan/concerns/enumerable.rb, line 39 def define_setter(field_name, values) define_method "#{field_name}=" do |value| value = value.to_sym self["enum_#{field_name}"] = value if values.include? value end end
define_values_methods(field_name, values)
click to toggle source
# File lib/arkaan/concerns/enumerable.rb, line 46 def define_values_methods(field_name, values) values.map(&:to_sym).each do |value| define_method "#{field_name}_#{value}!" do self["enum_#{field_name}"] = value end define_method "#{field_name}_#{value}?" do self["enum_#{field_name}"] == value end end end
enum_field(field_name, values, options = {})
click to toggle source
Creates the field with the given name, set of possible values, and options. @param field_name [String] the name of the enumerated field. @param values [Array<Symbol>] the possible values of the enumerated field. @param options [Hash<Symbol, Any>] the possible options for the field.
# File lib/arkaan/concerns/enumerable.rb, line 17 def enum_field(field_name, values, options = {}) returned = field :"enum_#{field_name}", type: Symbol, default: options[:default] validates :"enum_#{field_name}", inclusion: { in: values.map(&:to_sym), message: 'inclusion' } define_getter(field_name) define_setter(field_name, values) define_values_methods(field_name, values) # This is to make enumerations historizable by # returning the field object created by Mongoid. returned end