Sha256: af313107d1f3de391f99e01cf93075a20452643bc12116829eee92bd2e906a89
Contents?: true
Size: 1.29 KB
Versions: 2
Compression:
Stored size: 1.29 KB
Contents
module Dry::Initializer class Option < Attribute # part of __initializer__ definition def initializer_signature "**__options__" end # parts of __initalizer__ def presetter "@#{target} = #{undefined}" if dispensable? && @undefined end def safe_setter "@#{target} = #{safe_coerced}#{maybe_optional}" end def fast_setter return safe_setter unless dispensable? "@#{target} = __options__.key?(:'#{source}')" \ " ? #{safe_coerced}" \ " : #{undefined}" end # part of __defaults__ def default_hash super :option end # part of __coercers__ def coercer_hash super :option end private def dispensable? optional && !default end def maybe_optional " if __options__.key? :'#{source}'" if dispensable? end def safe_coerced return safe_default unless coercer "__coercers__[:'option_#{source}'].call(#{safe_default})" end def safe_default "__options__.fetch(:'#{source}')#{default_part}" end def default_part if default " { instance_exec(&__defaults__[:'option_#{source}']) }" elsif !optional " { raise ArgumentError, \"option :'#{source}' is required\" }" end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dry-initializer-1.4.1 | lib/dry/initializer/option.rb |
dry-initializer-1.4.0 | lib/dry/initializer/option.rb |