Sha256: 4b30f87c023e1ba3b1ec2a262e2eb775380d14211f08e1e17211c31821556a16
Contents?: true
Size: 1.9 KB
Versions: 2
Compression:
Stored size: 1.9 KB
Contents
module Dry::Initializer::Builders # @private class Attribute def self.[](definition) new(definition).call end def call lines.compact end private # rubocop: disable Style/MethodLength def initialize(definition) @definition = definition @option = definition.option @type = definition.type @optional = definition.optional @default = definition.default @source = definition.source @ivar = definition.ivar @null = definition.null ? "Dry::Initializer::UNDEFINED" : "nil" @opts = "__dry_initializer_options__" @congif = "__dry_initializer_config__" @item = "__dry_initializer_definition__" @val = @option ? "__dry_initializer_value__" : @source end # rubocop: enable Style/MethodLength def lines [ "", definition_line, reader_line, default_line, coercion_line, assignment_line ] end def reader_line return unless @option @optional ? optional_reader : required_reader end def optional_reader "#{@val} = #{@opts}.fetch(:'#{@source}', #{@null})" end def required_reader "#{@val} = #{@opts}.fetch(:'#{@source}')" \ " { raise KeyError, \"\#{self.class}: #{@definition} is required\" }" end def definition_line return unless @type || @default "#{@item} = __dry_initializer_config__.definitions[:'#{@source}']" end def default_line return unless @default "#{@val} = instance_exec(&#{@item}.default) if #{@val} == #{@null}" end def coercion_line return unless @type "#{@val} = #{@item}.type.call(#{@val}) unless #{@val} == #{@null}" end def assignment_line "#{@ivar} = #{@val}" \ " unless #{@val} == #{@null} && instance_variable_defined?(:#{@ivar})" end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dry-initializer-2.1.0 | lib/dry/initializer/builders/attribute.rb |
dry-initializer-2.0.0 | lib/dry/initializer/builders/attribute.rb |