module Assertion::DSL::Attributes

Allows adding aliases to values of the ‘#attributes` hash.

Public Class Methods

extended(klass) click to toggle source

@private

# File lib/assertion/dsl/attributes.rb, line 34
def self.extended(klass)
  klass.__send__(:define_method, :attributes) { @attributes ||= {} }
end

Public Instance Methods

attribute(*names) click to toggle source

Declares new attribute(s) by name(s)

@param [#to_sym, Array<#to_sym>] names

@return [undefined]

@raise [NameError]

When a given name is either used by instance methods,
or reserved by the `#check` method to be implemented later.
# File lib/assertion/dsl/attributes.rb, line 29
def attribute(*names)
  names.flatten.map(&:to_sym).each(&method(:__add_attribute__))
end
attributes() click to toggle source

List of declared attributes

@return [Array<Symbol>]

# File lib/assertion/dsl/attributes.rb, line 15
def attributes
  @attributes ||= []
end

Private Instance Methods

__add_attribute__(name) click to toggle source
# File lib/assertion/dsl/attributes.rb, line 40
def __add_attribute__(name)
  __check_attribute__(name)
  define_method(name) { attributes.fetch(name) }
  attributes << name
end
__check_attribute__(name) click to toggle source
# File lib/assertion/dsl/attributes.rb, line 46
def __check_attribute__(name)
  return unless (instance_methods << :check).include? name
  fail NameError.new "#{self}##{name} is already defined"
end