Module: AttributesDSL

Defined in:
lib/attributes_dsl.rb,
lib/attributes_dsl/attribute.rb,
lib/attributes_dsl/attributes.rb,
lib/attributes_dsl/transprocs.rb

Overview

Simple DSL for PORO attributes

Author:

Defined Under Namespace

Modules: InstanceMethods, Transprocs Classes: Attribute, Attributes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



53
54
55
# File 'lib/attributes_dsl.rb', line 53

def self.extended(klass)
  klass.instance_eval { include InstanceMethods }
end

Instance Method Details

#attribute(name, options = {}, &coercer) ⇒ undefined

Retisters an attribute by name, options and coercer

Examples:

class MyClass
  extend AttributeDSL

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

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

Parameters:

  • name (#to_sym)

    The unique name of the attribute

  • options (Hash) (defaults to: {})
  • coercer (Proc)

    The proc to coerce values (including the default ones)

Returns:

  • (undefined)


47
48
49
50
# 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

#attributesAttributeDSL::Attributes

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The mutable collection of declared attributes

Returns:

  • (AttributeDSL::Attributes)


24
25
26
# File 'lib/attributes_dsl.rb', line 24

def attributes
  @attributes ||= Attributes.new
end

#inherited(klass) ⇒ Object



58
59
60
# File 'lib/attributes_dsl.rb', line 58

def inherited(klass)
  klass.instance_variable_set(:@attributes, attributes)
end