class Dry::Initializer::Config

Gem-related configuration of some class

Attributes

definitions[R]

@!attribute [r] definitions @return [Hash<Symbol, Dry::Initializer::Definition>]

hash of attribute definitions with their source names
extended_class[R]

@!attribute [r] definitions @return [Hash<Symbol, Dry::Initializer::Definition>]

hash of attribute definitions with their source names
null[R]

@!attribute [r] definitions @return [Hash<Symbol, Dry::Initializer::Definition>]

hash of attribute definitions with their source names
parent[R]

@!attribute [r] definitions @return [Hash<Symbol, Dry::Initializer::Definition>]

hash of attribute definitions with their source names

Public Class Methods

new(extended_class = nil, null: UNDEFINED) click to toggle source
# File lib/dry/initializer/config.rb, line 131
def initialize(extended_class = nil, null: UNDEFINED)
  @extended_class = extended_class.tap { |klass| klass&.include mixin }
  sklass          = extended_class&.superclass
  @parent         = sklass.dry_initializer if sklass.is_a? Dry::Initializer
  @null           = null || parent&.null
  @definitions    = {}
  finalize
end

Public Instance Methods

attributes(instance) click to toggle source

The hash of assigned attributes for an instance of the [#extended_class] @param [Dry::Initializer::Instance] instance @return [Hash<Symbol, Object>]

# 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
children() click to toggle source

List of configs of all subclasses of the [#extended_class] @return [Array<Dry::Initializer::Config>]

# File lib/dry/initializer/config.rb, line 39
def children
  @children ||= Set.new
end
code() click to toggle source

Code of the ‘#__initialize__` method @return [String]

# File lib/dry/initializer/config.rb, line 103
def code
  Builders::Initializer[self]
end
finalize() click to toggle source

Finalizes config @return [self]

# 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
inch() click to toggle source

Human-readable representation of configured params and options @return [String]

# 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
mixin() click to toggle source

@!attribute [r] mixin @return [Module] reference to the module to be included into class

# 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
option(name, type = nil, **opts, &block) click to toggle source

Adds or redefines an option of [#dry_initializer]

@param (see param) @option (see param) @return (see param)

# File lib/dry/initializer/config.rb, line 73
def option(name, type = nil, **opts, &block)
  add_definition(true, name, type, block, **opts)
end
options() click to toggle source

List of definitions for initializer options @return [Array<Dry::Initializer::Definition>]

# File lib/dry/initializer/config.rb, line 51
def options
  definitions.values.select(&:option)
end
param(name, type = nil, **opts, &block) click to toggle source

Adds or redefines a parameter @param [Symbol] name @param [#call, nil] type (nil) @option opts [Proc] :default @option opts [Boolean] :optional @option opts [Symbol] :as @option opts [true, false, :protected, :public, :private] :reader @return [self] itself

# File lib/dry/initializer/config.rb, line 63
def param(name, type = nil, **opts, &block)
  add_definition(false, name, type, block, **opts)
end
params() click to toggle source

List of definitions for initializer params @return [Array<Dry::Initializer::Definition>]

# File lib/dry/initializer/config.rb, line 45
def params
  definitions.values.reject(&:option)
end
public_attributes(instance) click to toggle source

The hash of public attributes for an instance of the [#extended_class] @param [Dry::Initializer::Instance] instance @return [Hash<Symbol, Object>]

# 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

Private Instance Methods

add_definition(option, name, type, block, **opts) click to toggle source
# File lib/dry/initializer/config.rb, line 140
def add_definition(option, name, type, block, **opts)
  opts = {
    parent: extended_class,
    option: option,
    null: null,
    source: name,
    type: type,
    block: block,
    **opts
  }

  options = Dispatchers.call(**opts)
  definition = Definition.new(**options)
  definitions[definition.source] = definition
  finalize
  mixin.class_eval definition.code
end
check_order_of_params() click to toggle source
# File lib/dry/initializer/config.rb, line 174
def check_order_of_params
  params.inject(nil) do |optional, current|
    if current.optional
      current
    elsif optional
      raise SyntaxError, "#{extended_class}: required #{current}" \
                         " goes after optional #{optional}"
    else
      optional
    end
  end
end
check_type(previous, current) click to toggle source
# File lib/dry/initializer/config.rb, line 165
def check_type(previous, current)
  return current unless previous
  return current if previous.option == current.option

  raise SyntaxError,
        "cannot reload #{previous} of #{extended_class.superclass}" \
        " by #{current} of its subclass #{extended_class}"
end
final_definitions() click to toggle source
# File lib/dry/initializer/config.rb, line 158
def final_definitions
  parent_definitions = Hash(parent&.definitions&.dup)
  definitions.each_with_object(parent_definitions) do |(key, val), obj|
    obj[key] = check_type(obj[key], val)
  end
end