module PleaseRun::Configurable::ClassMixin

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.

Public Instance Methods

all_attributes() click to toggle source
# 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) click to toggle source

Define an attribute on this class.

# File lib/pleaserun/configurable.rb, line 65
def attribute(name, description, options = {}, &validator)
  facet = Facet.new(name, description, options, &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() click to toggle source
# File lib/pleaserun/configurable.rb, line 87
def attributes
  return @attributes ||= []
end