Class: Dry::Types::Constructor

Inherits:
Definition show all
Defined in:
lib/dry/types/constructor.rb

Instance Attribute Summary collapse

Attributes inherited from Definition

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Definition

[], #name, #try, #valid?, #with

Methods included from Builder

#constrained, #default, #enum, #optional, #safe, #|

Constructor Details

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

Returns a new instance of Constructor.



19
20
21
22
23
# File 'lib/dry/types/constructor.rb', line 19

def initialize(type, options = {})
  super
  @type = type
  @fn = options.fetch(:fn)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dry/types/constructor.rb', line 44

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

    if response.is_a?(Constructor)
      constructor(response.fn, options.merge(response.options))
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#fnObject (readonly)

Returns the value of attribute fn.



10
11
12
# File 'lib/dry/types/constructor.rb', line 10

def fn
  @fn
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/dry/types/constructor.rb', line 12

def type
  @type
end

Class Method Details

.new(input, options = {}) ⇒ Object



14
15
16
17
# File 'lib/dry/types/constructor.rb', line 14

def self.new(input, options = {})
  type = input.is_a?(Definition) ? input : Definition.new(input)
  super(type, options)
end

Instance Method Details

#call(input) ⇒ Object Also known as: []



29
30
31
# File 'lib/dry/types/constructor.rb', line 29

def call(input)
  fn[input]
end

#constructor(new_fn, options = {}) ⇒ Object



34
35
36
# File 'lib/dry/types/constructor.rb', line 34

def constructor(new_fn, options = {})
  with(options.merge(fn: -> input { new_fn[fn[input]] }))
end

#primitiveObject



25
26
27
# File 'lib/dry/types/constructor.rb', line 25

def primitive
  type.primitive
end

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

Returns:

  • (Boolean)


38
39
40
# File 'lib/dry/types/constructor.rb', line 38

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