Class: Dry::Initializer::Attribute
- Inherits:
-
Object
- Object
- Dry::Initializer::Attribute
- Defined in:
- lib/dry/initializer/attribute.rb
Overview
Contains definitions for a single attribute, and builds its parts of mixin
Instance Attribute Summary collapse
-
#coercer ⇒ Object
readonly
Returns the value of attribute coercer.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
-
.dispatchers ⇒ Object
Collection of additional dispatchers for method options.
- .new(source, coercer = nil, **options) ⇒ Object
- .option(*args) ⇒ Object
- .param(*args) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#getter ⇒ Object
definition for the getter method.
-
#initialize(options) ⇒ Attribute
constructor
A new instance of Attribute.
Constructor Details
#initialize(options) ⇒ Attribute
42 43 44 45 46 47 48 49 50 |
# File 'lib/dry/initializer/attribute.rb', line 42 def initialize() @source = [:source] @target = [:target] @coercer = [:type] @default = [:default] @optional = !!([:optional] || @default) @reader = .fetch(:reader, :public) validate end |
Instance Attribute Details
#coercer ⇒ Object (readonly)
Returns the value of attribute coercer.
40 41 42 |
# File 'lib/dry/initializer/attribute.rb', line 40 def coercer @coercer end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
40 41 42 |
# File 'lib/dry/initializer/attribute.rb', line 40 def default @default end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
40 41 42 |
# File 'lib/dry/initializer/attribute.rb', line 40 def optional @optional end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
40 41 42 |
# File 'lib/dry/initializer/attribute.rb', line 40 def reader @reader end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
40 41 42 |
# File 'lib/dry/initializer/attribute.rb', line 40 def source @source end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
40 41 42 |
# File 'lib/dry/initializer/attribute.rb', line 40 def target @target end |
Class Method Details
.dispatchers ⇒ Object
Collection of additional dispatchers for method options
18 19 20 |
# File 'lib/dry/initializer/attribute.rb', line 18 def dispatchers @@dispatchers ||= [] end |
.new(source, coercer = nil, **options) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/dry/initializer/attribute.rb', line 22 def new(source, coercer = nil, **) [:source] = source [:target] = .delete(:as) || source [:type] ||= coercer params = dispatchers.inject() { |h, m| m.call(h) } super(params) end |
Instance Method Details
#==(other) ⇒ Object
52 53 54 |
# File 'lib/dry/initializer/attribute.rb', line 52 def ==(other) source == other.source end |
#getter ⇒ Object
definition for the getter method
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dry/initializer/attribute.rb', line 57 def getter return unless reader command = %w(private protected).include?(reader.to_s) ? reader : :public <<-RUBY.gsub(/^ *\|/, "") |def #{target} | @#{target} unless @#{target} == Dry::Initializer::UNDEFINED |end |#{command} :#{target} RUBY end |