module Virtuaservices::Concerns::Enumerable::ClassMethods
Submodule holding all the static methods add to the current subclass. @author Vincent Courtois <courtois.vincent@outlook.com>
Public Instance Methods
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/virtuaservices/concerns/enumerable.rb, line 16 def enum_field(field_name, values, options = {}) field :"enum_#{field_name}", type: Symbol, default: options[:default] validates :"enum_#{field_name}", inclusion: {in: values.map(&:to_sym), message: 'inclusion'} define_method field_name do return self["enum_#{field_name}"] end define_method "#{field_name}=" do |value| if values.include? value.to_sym self["enum_#{field_name}"] = value.to_sym end end 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