Class: Dry::Initializer::Config
- Inherits:
-
Object
- Object
- Dry::Initializer::Config
- Defined in:
- lib/dry/initializer/config.rb
Overview
Gem-related configuration of some class
Instance Attribute Summary collapse
-
#definitions ⇒ Hash<Symbol, Dry::Initializer::Definition>
readonly
Hash of attribute definitions with their source names.
-
#extended_class ⇒ Hash<Symbol, Dry::Initializer::Definition>
readonly
Hash of attribute definitions with their source names.
-
#mixin ⇒ Module
readonly
Reference to the module to be included into class.
-
#null ⇒ Hash<Symbol, Dry::Initializer::Definition>
readonly
Hash of attribute definitions with their source names.
-
#parent ⇒ Hash<Symbol, Dry::Initializer::Definition>
readonly
Hash of attribute definitions with their source names.
Instance Method Summary collapse
-
#attributes(instance) ⇒ Hash<Symbol, Object>
The hash of assigned attributes for an instance of the [#extended_class].
-
#children ⇒ Array<Dry::Initializer::Config>
List of configs of all subclasses of the [#extended_class].
-
#code ⇒ String
Code of the ‘#__initialize__` method.
-
#finalize ⇒ self
Finalizes config.
-
#inch ⇒ String
Human-readable representation of configured params and options.
-
#option(name, type = nil, **opts, &block) ⇒ self
Adds or redefines an option of [#dry_initializer].
-
#options ⇒ Array<Dry::Initializer::Definition>
List of definitions for initializer options.
-
#param(name, type = nil, **opts, &block) ⇒ self
Adds or redefines a parameter.
-
#params ⇒ Array<Dry::Initializer::Definition>
List of definitions for initializer params.
-
#public_attributes(instance) ⇒ Hash<Symbol, Object>
The hash of public attributes for an instance of the [#extended_class].
Instance Attribute Details
#definitions ⇒ Hash<Symbol, Dry::Initializer::Definition> (readonly)
Returns hash of attribute definitions with their source names.
22 |
# File 'lib/dry/initializer/config.rb', line 22 attr_reader :null, :extended_class, :parent, :definitions |
#extended_class ⇒ Hash<Symbol, Dry::Initializer::Definition> (readonly)
Returns hash of attribute definitions with their source names.
|
# File 'lib/dry/initializer/config.rb', line 12
|
#mixin ⇒ Module (readonly)
Returns 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 |
#null ⇒ Hash<Symbol, Dry::Initializer::Definition> (readonly)
Returns hash of attribute definitions with their source names.
|
# File 'lib/dry/initializer/config.rb', line 9
|
#parent ⇒ Hash<Symbol, Dry::Initializer::Definition> (readonly)
Returns hash of attribute definitions with their source names.
|
# 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]
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 |
#children ⇒ Array<Dry::Initializer::Config>
List of configs of all subclasses of the [#extended_class]
39 40 41 |
# File 'lib/dry/initializer/config.rb', line 39 def children @children ||= Set.new end |
#code ⇒ String
Code of the ‘#__initialize__` method
103 104 105 |
# File 'lib/dry/initializer/config.rb', line 103 def code Builders::Initializer[self] end |
#finalize ⇒ self
Finalizes config
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 |
#inch ⇒ String
Human-readable representation of configured params and options
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]
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 |
#options ⇒ Array<Dry::Initializer::Definition>
List of definitions for initializer options
51 52 53 |
# File 'lib/dry/initializer/config.rb', line 51 def definitions.values.select(&:option) end |
#param(name, type = nil, **opts, &block) ⇒ self
Adds or redefines a parameter
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 |
#params ⇒ Array<Dry::Initializer::Definition>
List of definitions for initializer params
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]
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 |