Class: Democritus::ClassBuilder::Commands::Attribute

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

Overview

Command to assign an attribute to the given built class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, **options) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • builder (#defer, Democritus::ClassBuilder)
  • name (#to_sym)

    The name of the attribute

  • options (Hash)

    The configuration options of the attribute



11
12
13
14
15
# File 'lib/democritus/class_builder/commands/attribute.rb', line 11

def initialize(name:, **options)
  self.builder = options.fetch(:builder)
  self.name = name
  self.options = options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/democritus/class_builder/commands/attribute.rb', line 17

def name
  @name
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/democritus/class_builder/commands/attribute.rb', line 18

def options
  @options
end

Instance Method Details

#callObject

Generate the code for the attribute

:reek:NestedIterators: { exclude: [ ‘Democritus::ClassBuilder::Commands::Attribute#call’ ] }

Returns:

  • void



24
25
26
27
28
29
30
31
32
# File 'lib/democritus/class_builder/commands/attribute.rb', line 24

def call
  defer do |subject|
    subject.module_exec(@name) do |name|
      attr_reader name
      private
      attr_writer name
    end
  end
end