module SmartEnum::Attributes::ClassMethods

Public Instance Methods

attribute(name, types, coercer: nil, reader_method: nil) click to toggle source
# File lib/smart_enum/attributes.rb, line 49
def attribute(name, types, coercer: nil, reader_method: nil)
  name = name.to_sym
  # ensure `types` is an array.  From activesupport's Array#wrap.
  types = if types.nil?
    []
  elsif types.respond_to?(:to_ary)
    types.to_ary || [types]
  else
    [types]
  end
  attribute_set[name] = Attribute.new(name, types, coercer)
  define_method(reader_method || name) do
    attributes[name]
  end
  if types == Boolean
    alias_method "#{name}?".to_sym, name
  end
end
attribute_set() click to toggle source
# File lib/smart_enum/attributes.rb, line 36
def attribute_set
  @attribute_set ||= {}
end
inherited(child_class) click to toggle source
# File lib/smart_enum/attributes.rb, line 40
def inherited(child_class)
  # STI children should start with an attribute set cloned from their parent.
  # Otherwise theirs will start blank.
  child_class.instance_variable_set(:@attribute_set, self.attribute_set.dup)
  # STI children must *share* a reference to the same init_mutex as their
  # parent so that reads are correctly blocked during async loading.
  child_class.instance_variable_set(:@_init_mutex, @_init_mutex)
end
inspect() click to toggle source
# File lib/smart_enum/attributes.rb, line 68
def inspect
  lock_str = @enum_locked ? "LOCKED" : "UNLOCKED"
  "#{self}(#{lock_str} #{attribute_set.values.map(&:inspect).join(", ")})"
end