Class: Democritus::ClassBuilder::Commands::Attributes

Inherits:
Democritus::ClassBuilder::Command show all
Defined in:
lib/democritus/class_builder/commands/attributes.rb

Overview

Command to assign attributes as part of the initialize method.

Examples:

Democritus::ClassBuilder::Commands::Attributes.new(builder: a_builder) do
  attribute(:name)
  attribute(:coolness_factor)
end

Commands Available Within the pre_deferment_operation collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder:, &pre_deferment_operation) ⇒ Attributes

Returns a new instance of Attributes.

Parameters:



16
17
18
19
20
# File 'lib/democritus/class_builder/commands/attributes.rb', line 16

def initialize(builder:, &pre_deferment_operation)
  self.builder = builder
  self.pre_deferment_operation = pre_deferment_operation
  self.attribute_names = []
end

Instance Method Details

#attribute(name:, **options) ⇒ Object

Exposes a mechanism for assigning individual attributes as part of the #attributes command.

Parameters:

  • name (#to_sym)

    the name of the attribute

  • options (Hash)

    additional options passed to the builder#attribute command

See Also:



69
70
71
72
73
# File 'lib/democritus/class_builder/commands/attributes.rb', line 69

def attribute(name:, **options)
  name = name.to_sym
  attribute_names << name
  builder.attribute(name: name, **options)
end

#callObject

Method that generates the behavior for the Attributes command

Returns:

  • void

See Also:



28
29
30
31
32
33
34
# File 'lib/democritus/class_builder/commands/attributes.rb', line 28

def call
  # In order to register any nested attributes, the pre_deferment_operation must be performed
  # outside of the defer method call. If it were done inside the defer block, then the pre_deferment_operation
  # might get lost.
  execute_pre_deferment_operation
  defer { |subject| configure(subject: subject) }
end