Module: Dry::Data::Decorator

Included in:
Constrained, Default, Enum, Optional, Safe
Defined in:
lib/dry/data/decorator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dry/data/decorator.rb', line 29

def method_missing(meth, *args, &block)
  if type.respond_to?(meth)
    response = type.__send__(meth, *args, &block)

    if response.kind_of?(type.class)
      self.class.new(response, options)
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/dry/data/decorator.rb', line 4

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/dry/data/decorator.rb', line 4

def type
  @type
end

Instance Method Details

#constructorObject



11
12
13
# File 'lib/dry/data/decorator.rb', line 11

def constructor
  type.constructor
end

#initialize(type, options = {}) ⇒ Object



6
7
8
9
# File 'lib/dry/data/decorator.rb', line 6

def initialize(type, options = {})
  @type = type
  @options = options
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/dry/data/decorator.rb', line 19

def respond_to_missing?(meth, include_private = false)
  super || type.respond_to?(meth)
end

#valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/dry/data/decorator.rb', line 15

def valid?(input)
  type.valid?(input)
end

#with(new_options) ⇒ Object



23
24
25
# File 'lib/dry/data/decorator.rb', line 23

def with(new_options)
  self.class.new(type, options.merge(new_options))
end