module AttributesDSL

Simple DSL for PORO attributes

@api public

@author Andrew Kozin <Andrew.Kozin@gmail.com>

Constants

VERSION

The semantic version of the module. @see semver.org/ Semantic versioning 2.0

Public Class Methods

extended(klass) click to toggle source

@private

# File lib/attributes_dsl.rb, line 53
def self.extended(klass)
  klass.instance_eval { include InstanceMethods }
end

Public Instance Methods

attribute(name, options = {}, &coercer) click to toggle source

Retisters an attribute by name, options and coercer

@example

class MyClass
  extend AttributeDSL

  attribute :foo, required: true do |value|
    value.to_i % 5 # value coercer
  end

  attribute :bar, default: :BAR, reader: false
end

@param [#to_sym] name The unique name of the attribute @param [Hash] options @param [Proc] coercer The proc to coerce values (including the default ones)

@return [undefined]

# File lib/attributes_dsl.rb, line 47
def attribute(name, options = {}, &coercer)
  @attributes = attributes.add(name, options, &coercer)
  define_method(name) { attributes.fetch(name) } if attributes.reader? name
end
attributes() click to toggle source

The mutable collection of declared attributes

@return [AttributeDSL::Attributes]

@api private

# File lib/attributes_dsl.rb, line 24
def attributes
  @attributes ||= Attributes.new
end
inherited(klass) click to toggle source

@private

# File lib/attributes_dsl.rb, line 58
def inherited(klass)
  klass.instance_variable_set(:@attributes, attributes)
end