Class: Dry::Initializer::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/initializer/attribute.rb

Overview

Contains definitions for a single attribute, and builds its parts of mixin

Direct Known Subclasses

Option, Param

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Attribute



42
43
44
45
46
47
48
49
50
# File 'lib/dry/initializer/attribute.rb', line 42

def initialize(options)
  @source   = options[:source]
  @target   = options[:target]
  @coercer  = options[:type]
  @default  = options[:default]
  @optional = !!(options[:optional] || @default)
  @reader   = options.fetch(:reader, :public)
  validate
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



40
41
42
# File 'lib/dry/initializer/attribute.rb', line 40

def coercer
  @coercer
end

#defaultObject (readonly)

Returns the value of attribute default.



40
41
42
# File 'lib/dry/initializer/attribute.rb', line 40

def default
  @default
end

#optionalObject (readonly)

Returns the value of attribute optional.



40
41
42
# File 'lib/dry/initializer/attribute.rb', line 40

def optional
  @optional
end

#readerObject (readonly)

Returns the value of attribute reader.



40
41
42
# File 'lib/dry/initializer/attribute.rb', line 40

def reader
  @reader
end

#sourceObject (readonly)

Returns the value of attribute source.



40
41
42
# File 'lib/dry/initializer/attribute.rb', line 40

def source
  @source
end

#targetObject (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

.dispatchersObject

Collection of additional dispatchers for method options

Examples:

Enhance the gem by adding :coercer alias for type

Dry::Initializer::Attribute.dispatchers << -> (string: nil, **op) do
  op[:type] = proc(&:to_s) if string
  op
end

class User
  extend Dry::Initializer
  param :name, string: true # same as `type: proc(&:to_s)`
end


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, **options)
  options[:source] = source
  options[:target] = options.delete(:as) || source
  options[:type] ||= coercer
  params = dispatchers.inject(options) { |h, m| m.call(h) }

  super(params)
end

.option(*args) ⇒ Object



35
36
37
# File 'lib/dry/initializer/attribute.rb', line 35

def option(*args)
  Option.new(*args)
end

.param(*args) ⇒ Object



31
32
33
# File 'lib/dry/initializer/attribute.rb', line 31

def param(*args)
  Param.new(*args)
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
# File 'lib/dry/initializer/attribute.rb', line 52

def ==(other)
  source == other.source
end

#getterObject

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