Class: Dry::Initializer::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/initializer/config.rb

Overview

Gem-related configuration of some class

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#definitionsHash<Symbol, Dry::Initializer::Definition> (readonly)

Returns hash of attribute definitions with their source names.

Returns:



22
# File 'lib/dry/initializer/config.rb', line 22

attr_reader :null, :extended_class, :parent, :definitions

#extended_classHash<Symbol, Dry::Initializer::Definition> (readonly)

Returns hash of attribute definitions with their source names.

Returns:



# File 'lib/dry/initializer/config.rb', line 12

#mixinModule (readonly)

Returns reference to the module to be included into class.

Returns:

  • (Module)

    reference to the module to be included into class



26
27
28
29
30
31
32
33
34
35
# File 'lib/dry/initializer/config.rb', line 26

def mixin
  @mixin ||= Module.new.tap do |mod|
    initializer = self
    mod.extend(Mixin::Local)
    mod.define_method(:__dry_initializer_config__) do
      initializer
    end
    mod.send :private, :__dry_initializer_config__
  end
end

#nullHash<Symbol, Dry::Initializer::Definition> (readonly)

Returns hash of attribute definitions with their source names.

Returns:



# File 'lib/dry/initializer/config.rb', line 9

#parentHash<Symbol, Dry::Initializer::Definition> (readonly)

Returns hash of attribute definitions with their source names.

Returns:



# File 'lib/dry/initializer/config.rb', line 15

Instance Method Details

#attributes(instance) ⇒ Hash<Symbol, Object>

The hash of assigned attributes for an instance of the [#extended_class]

Parameters:

  • instance (Dry::Initializer::Instance)

Returns:

  • (Hash<Symbol, Object>)


93
94
95
96
97
98
99
# File 'lib/dry/initializer/config.rb', line 93

def attributes(instance)
  definitions.values.each_with_object({}) do |item, obj|
    key = item.target
    val = instance.send(:instance_variable_get, item.ivar)
    obj[key] = val unless null == val
  end
end

#childrenArray<Dry::Initializer::Config>

List of configs of all subclasses of the [#extended_class]

Returns:



39
40
41
# File 'lib/dry/initializer/config.rb', line 39

def children
  @children ||= Set.new
end

#codeString

Code of the ‘#__initialize__` method

Returns:

  • (String)


103
104
105
# File 'lib/dry/initializer/config.rb', line 103

def code
  Builders::Initializer[self]
end

#finalizeself

Finalizes config

Returns:

  • (self)


109
110
111
112
113
114
115
# File 'lib/dry/initializer/config.rb', line 109

def finalize
  @definitions = final_definitions
  check_order_of_params
  mixin.class_eval(code)
  children.each(&:finalize)
  self
end

#inchString

Human-readable representation of configured params and options

Returns:

  • (String)


119
120
121
122
123
124
125
126
127
# File 'lib/dry/initializer/config.rb', line 119

def inch
  line  =  Builders::Signature[self]
  line  =  line.gsub("__dry_initializer_options__", "options")
  lines =  ["@!method initialize(#{line})"]
  lines += ["Initializes an instance of #{extended_class}"]
  lines += definitions.values.map(&:inch)
  lines += ["@return [#{extended_class}]"]
  lines.join("\n")
end

#option(name, type = nil, **opts, &block) ⇒ self

Adds or redefines an option of [#dry_initializer]

Parameters:

  • name (Symbol)
  • type (#call, nil) (defaults to: nil)

    (nil)

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :default (Proc)
  • :optional (Boolean)
  • :as (Symbol)
  • :reader (true, false, :protected, :public, :private)

Returns:

  • (self)

    itself



73
74
75
# File 'lib/dry/initializer/config.rb', line 73

def option(name, type = nil, **opts, &block)
  add_definition(true, name, type, block, **opts)
end

#optionsArray<Dry::Initializer::Definition>

List of definitions for initializer options

Returns:



51
52
53
# File 'lib/dry/initializer/config.rb', line 51

def options
  definitions.values.select(&:option)
end

#param(name, type = nil, **opts, &block) ⇒ self

Adds or redefines a parameter

Parameters:

  • name (Symbol)
  • type (#call, nil) (defaults to: nil)

    (nil)

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :default (Proc)
  • :optional (Boolean)
  • :as (Symbol)
  • :reader (true, false, :protected, :public, :private)

Returns:

  • (self)

    itself



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

def param(name, type = nil, **opts, &block)
  add_definition(false, name, type, block, **opts)
end

#paramsArray<Dry::Initializer::Definition>

List of definitions for initializer params

Returns:



45
46
47
# File 'lib/dry/initializer/config.rb', line 45

def params
  definitions.values.reject(&:option)
end

#public_attributes(instance) ⇒ Hash<Symbol, Object>

The hash of public attributes for an instance of the [#extended_class]

Parameters:

  • instance (Dry::Initializer::Instance)

Returns:

  • (Hash<Symbol, Object>)


80
81
82
83
84
85
86
87
88
# File 'lib/dry/initializer/config.rb', line 80

def public_attributes(instance)
  definitions.values.each_with_object({}) do |item, obj|
    key = item.target
    next unless instance.respond_to? key

    val = instance.send(key)
    obj[key] = val unless null == val
  end
end