Class: Dry::Types::Constructor
- Inherits:
-
Definition
- Object
- Definition
- Dry::Types::Constructor
- Defined in:
- lib/dry/types/constructor.rb
Instance Attribute Summary collapse
-
#fn ⇒ Object
readonly
Returns the value of attribute fn.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from Definition
Class Method Summary collapse
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
- #constructor(new_fn, options = {}) ⇒ Object
-
#initialize(type, options = {}) ⇒ Constructor
constructor
A new instance of Constructor.
- #primitive ⇒ Object
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean
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, = {}) super @type = type @fn = .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, .merge(response.)) else response end else super end end |
Instance Attribute Details
#fn ⇒ Object (readonly)
Returns the value of attribute fn.
10 11 12 |
# File 'lib/dry/types/constructor.rb', line 10 def fn @fn end |
#type ⇒ Object (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, = {}) type = input.is_a?(Definition) ? input : Definition.new(input) super(type, ) 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, = {}) with(.merge(fn: -> input { new_fn[fn[input]] })) end |
#primitive ⇒ Object
25 26 27 |
# File 'lib/dry/types/constructor.rb', line 25 def primitive type.primitive end |
#respond_to_missing?(meth, include_private = false) ⇒ 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 |