Module: Dry::Transformer::Registry
- Included in:
- ArrayTransformations, ClassTransformations, Coercions, Conditional, HashTransformations, ProcTransformations, Recursion
- Defined in:
- lib/dry/transformer/registry.rb
Overview
Container to define transproc functions in, and access them via ‘[]` method from the outside of the module
Instance Method Summary collapse
-
#[](fn, *args) ⇒ Dry::Transformer::Function
(also: #t)
Builds the transformation.
-
#contain?(key) ⇒ Boolean
Returns wether the registry contains such transformation by its key.
-
#fetch(fn) ⇒ #call
Gets the procedure for creating a transproc.
-
#import(*args) ⇒ itself
(also: #uses)
Imports either a method (converted to a proc) from another module, or all methods from that module.
-
#register(name, fn = nil, &block) ⇒ Object
store.register(:to_json) { |v| v.to_json }.
-
#store ⇒ Dry::Transformer::Store
The store of procedures imported from external modules.
Instance Method Details
#[](fn, *args) ⇒ Dry::Transformer::Function Also known as: t
Builds the transformation
48 49 50 51 52 53 54 |
# File 'lib/dry/transformer/registry.rb', line 48 def [](fn, *args) fetched = fetch(fn) return Function.new(fetched, args: args, name: fn) unless already_wrapped?(fetched) args.empty? ? fetched : fetched.with(*args) end |
#contain?(key) ⇒ Boolean
Returns wether the registry contains such transformation by its key
63 64 65 |
# File 'lib/dry/transformer/registry.rb', line 63 def contain?(key) respond_to?(key) || store.contain?(key) end |
#fetch(fn) ⇒ #call
Gets the procedure for creating a transproc
134 135 136 137 138 139 140 |
# File 'lib/dry/transformer/registry.rb', line 134 def fetch(fn) return fn unless fn.instance_of? Symbol respond_to?(fn) ? method(fn) : store.fetch(fn) rescue StandardError raise FunctionNotFoundError.new(fn, self) end |
#import(source) ⇒ itself #import(*names, **options) ⇒ itself #import(name, **options) ⇒ itself Also known as: uses
Imports either a method (converted to a proc) from another module, or all methods from that module.
If the external module is a registry, looks for its imports too.
112 113 114 115 |
# File 'lib/dry/transformer/registry.rb', line 112 def import(*args) @store = store.import(*args) self end |
#register(name, fn = nil, &block) ⇒ Object
store.register(:to_json) { |v| v.to_json }
74 75 76 77 78 79 80 81 |
# File 'lib/dry/transformer/registry.rb', line 74 def register(name, fn = nil, &block) if contain?(name) raise FunctionAlreadyRegisteredError, "Function #{name} is already defined" end @store = store.register(name, fn, &block) self end |
#store ⇒ Dry::Transformer::Store
The store of procedures imported from external modules
122 123 124 |
# File 'lib/dry/transformer/registry.rb', line 122 def store @store ||= Store.new end |