Class: AttributesDSL::Attributes Private

Inherits:
Object
  • Object
show all
Defined in:
lib/attributes_dsl/attributes.rb

Overview

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

Describes a collection of attributes declaration with methods to validate and extract instance attributes from a hash.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ 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.

Initializes an immutable collection with an initial set of attributes

Parameters:

  • attributes (Hash) (defaults to: {})


26
27
28
29
# File 'lib/attributes_dsl/attributes.rb', line 26

def initialize(attributes = {})
  @attributes = attributes
  IceNine.deep_freeze(self)
end

Instance Attribute Details

#attributesSet (readonly)

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.

Uses the set of attributes to ensure their uniqueness (by name)

Returns:

  • (Set)

    the set of registered attributes



20
21
22
# File 'lib/attributes_dsl/attributes.rb', line 20

def attributes
  @attributes
end

Instance Method Details

#extract(input) ⇒ Hash

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.

Extracts instance attributes from the input hash

Assigns default values and uses coercions when applicable.

Parameters:

  • input (Hash)

Returns:

  • (Hash)


52
53
54
55
56
57
58
# File 'lib/attributes_dsl/attributes.rb', line 52

def extract(input)
  validate(input).inject({}) do |a, e|
    key = e.name
    value = input.fetch(key) { e.default }
    a.merge(key => e.value(value))
  end
end

#register(name, options = {}, &coercer) ⇒ AttributesDSL::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.

Initializes the attribute from given arguments and returns new immutable collection with the attribute

Parameters:

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

Returns:



38
39
40
41
42
# File 'lib/attributes_dsl/attributes.rb', line 38

def register(name, options = {}, &coercer)
  self.class.new(
    attributes.merge(name => Attribute.new(name, options, &coercer))
  )
end