Class: Dry::Initializer::Argument Private

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/dry/initializer/argument.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.

A simple structure describes an argument (either param, or option)

Constant Summary collapse

UNDEFINED =

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

Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, option:, reader: true, **options) ⇒ Argument

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.

Returns a new instance of Argument.



36
37
38
39
40
41
42
# File 'lib/dry/initializer/argument.rb', line 36

def initialize(name, option:, reader: true, **options)
  @name   = name.to_sym
  @option = option
  @reader = reader
  assign_default_value(options)
  assign_type(options)
end

Instance Attribute Details

#defaultBoolean (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.

Returns whether the argument has a default value.

Returns:

  • (Boolean)

    whether the argument has a default value



22
23
24
# File 'lib/dry/initializer/argument.rb', line 22

def default
  @default
end

#default_valueObject (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.

Returns the default value of the argument.

Returns:

  • (Object)

    the default value of the argument



26
27
28
# File 'lib/dry/initializer/argument.rb', line 26

def default_value
  @default_value
end

#nameSymbol (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.

Returns the name of the argument.

Returns:

  • (Symbol)

    the name of the argument



18
19
20
# File 'lib/dry/initializer/argument.rb', line 18

def name
  @name
end

#optionBoolean (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.

Returns Whether this is an option, or param of the initializer.

Returns:

  • (Boolean)

    Whether this is an option, or param of the initializer



14
15
16
# File 'lib/dry/initializer/argument.rb', line 14

def option
  @option
end

#readerBoolean (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.

Returns whether an attribute reader is defined for the argument.

Returns:

  • (Boolean)

    whether an attribute reader is defined for the argument



34
35
36
# File 'lib/dry/initializer/argument.rb', line 34

def reader
  @reader
end

#typeClass? (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.

Returns a type constraint.

Returns:

  • (Class, nil)

    a type constraint



30
31
32
# File 'lib/dry/initializer/argument.rb', line 30

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

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.



44
45
46
# File 'lib/dry/initializer/argument.rb', line 44

def ==(other)
  other.name == name
end

#assignmentObject

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.



58
59
60
# File 'lib/dry/initializer/argument.rb', line 58

def assignment
  "@#{name} = #{name}"
end

#default_assignmentObject

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.



62
63
64
65
# File 'lib/dry/initializer/argument.rb', line 62

def default_assignment
  "@#{name} = instance_eval(&__arguments__[:#{name}].default_value)" \
  " if #{name} == Dry::Initializer::Argument::UNDEFINED"
end

#signatureObject

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.



48
49
50
51
52
53
54
55
56
# File 'lib/dry/initializer/argument.rb', line 48

def signature
  case [option, default]
  when [false, false] then name
  when [false, true]  then "#{name} = Dry::Initializer::Argument::UNDEFINED"
  when [true, false]  then "#{name}:"
  else
    "#{name}: Dry::Initializer::Argument::UNDEFINED"
  end
end

#type_constraintObject

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.



67
68
69
# File 'lib/dry/initializer/argument.rb', line 67

def type_constraint
  "__arguments__[:#{name}].type.call(@#{name})"
end