Module: SmartEnum::Attributes::ClassMethods
- Defined in:
- lib/smart_enum/attributes.rb
Instance Method Summary collapse
- #attribute(name, types, coercer: nil, reader_method: nil) ⇒ Object
- #attribute_set ⇒ Object
- #inherited(child_class) ⇒ Object
- #inspect ⇒ Object
Instance Method Details
#attribute(name, types, coercer: nil, reader_method: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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 ⇒ Object
36 37 38 |
# File 'lib/smart_enum/attributes.rb', line 36 def attribute_set @attribute_set ||= {} end |
#inherited(child_class) ⇒ Object
40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
68 69 70 71 |
# 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 |