Module: PleaseRun::Configurable::ClassMixin
- Defined in:
- lib/pleaserun/configurable.rb
Overview
A mixin to extend a class with whenever a class includes PleaseRun::Configurable.
This class provides class-level ‘attribute’ method intended for use in defining attributes as well as a class-level ‘attributes’ method for listing attributes defined in this class. Finally, a helper ‘all_attributes’ method is provided to get all attributes defined by this class and any ancestors.
Instance Method Summary collapse
- #all_attributes ⇒ Object
-
#attribute(name, description, options = {}, &validator) ⇒ Object
Define an attribute on this class.
-
#attributes ⇒ Object
def attribute.
Instance Method Details
#all_attributes ⇒ Object
91 92 93 |
# File 'lib/pleaserun/configurable.rb', line 91 def all_attributes return ancestors.select { |a| a.respond_to?(:attributes) }.collect(&:attributes).flatten end |
#attribute(name, description, options = {}, &validator) ⇒ Object
Define an attribute on this class.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pleaserun/configurable.rb', line 65 def attribute(name, description, = {}, &validator) facet = Facet.new(name, description, , &validator) attributes << facet # define accessor method define_method(name.to_sym) do # object instance, not class ivar @attributes[name.to_sym].value end # define mutator define_method("#{name}=".to_sym) do |value| # object instance, not class ivar @attributes[name.to_sym].value = value end # define presence check method define_method("#{name}?".to_sym) do return @attributes[name.to_sym].set? end end |
#attributes ⇒ Object
def attribute
87 88 89 |
# File 'lib/pleaserun/configurable.rb', line 87 def attributes return @attributes ||= [] end |