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.
19 |
# File 'lib/dry/initializer/config.rb', line 19 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 9
|
#mixin ⇒ Module (readonly)
Returns reference to the module to be included into class.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dry/initializer/config.rb', line 23 def mixin @mixin ||= Module.new.tap do |mod| __dry_initializer__ = self mod.extend(Mixin::Local) mod.send :define_method, :__dry_initializer_config__ do __dry_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 6
|
#parent ⇒ Hash<Symbol, Dry::Initializer::Definition> (readonly)
Returns hash of attribute definitions with their source names.
|
|
# File 'lib/dry/initializer/config.rb', line 12
|
Instance Method Details
#attributes(instance) ⇒ Hash<Symbol, Object>
The hash of assigned attributes for an instance of the [#extended_class]
89 90 91 92 93 94 95 |
# File 'lib/dry/initializer/config.rb', line 89 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]
36 37 38 |
# File 'lib/dry/initializer/config.rb', line 36 def children @children ||= Set.new end |
#code ⇒ String
Code of the ‘#__initialize__` method
99 100 101 |
# File 'lib/dry/initializer/config.rb', line 99 def code Builders::Initializer[self] end |
#finalize ⇒ self
Finalizes config
105 106 107 108 109 110 111 |
# File 'lib/dry/initializer/config.rb', line 105 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
115 116 117 118 119 120 121 122 123 |
# File 'lib/dry/initializer/config.rb', line 115 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]
70 71 72 |
# File 'lib/dry/initializer/config.rb', line 70 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
48 49 50 |
# File 'lib/dry/initializer/config.rb', line 48 def definitions.values.select(&:option) end |
#param(name, type = nil, **opts, &block) ⇒ self
Adds or redefines a parameter
60 61 62 |
# File 'lib/dry/initializer/config.rb', line 60 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
42 43 44 |
# File 'lib/dry/initializer/config.rb', line 42 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]
77 78 79 80 81 82 83 84 |
# File 'lib/dry/initializer/config.rb', line 77 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 |