Class: Parametric::TaggedOneOf
- Inherits:
-
Object
- Object
- Parametric::TaggedOneOf
- Defined in:
- lib/parametric/tagged_one_of.rb
Overview
A policy that allows you to select a sub-schema based on a value in the payload. Instances can also be created separately and used as a policy:
Defined Under Namespace
Classes: Runner
Constant Summary collapse
- NOOP_INDEX =
->(payload) { payload }.freeze
Instance Method Summary collapse
-
#build(key, value, payload:, context:) ⇒ Object
The [PolicyFactory] interface.
- #index_by(callable = nil, &block) ⇒ Object
-
#initialize(index: NOOP_INDEX, matchers: {}, &block) ⇒ TaggedOneOf
constructor
A new instance of TaggedOneOf.
- #meta_data ⇒ Object
- #on(key, schema) ⇒ Object
Constructor Details
#initialize(index: NOOP_INDEX, matchers: {}, &block) ⇒ TaggedOneOf
Returns a new instance of TaggedOneOf.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/parametric/tagged_one_of.rb', line 46 def initialize(index: NOOP_INDEX, matchers: {}, &block) @index = index @matchers = matchers @configuring = false if block_given? @configuring = true block.call(self) @configuring = false end freeze end |
Instance Method Details
#build(key, value, payload:, context:) ⇒ Object
The [PolicyFactory] interface
76 77 78 |
# File 'lib/parametric/tagged_one_of.rb', line 76 def build(key, value, payload:, context:) Runner.new(@index, @matchers, key, value, payload, context) end |
#index_by(callable = nil, &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/parametric/tagged_one_of.rb', line 58 def index_by(callable = nil, &block) if callable.is_a?(Symbol) key = callable callable = ->(payload) { payload[key] } end index = callable || block if configuring? @index = index else self.class.new(index:, matchers: @matchers) end end |
#meta_data ⇒ Object
80 81 82 |
# File 'lib/parametric/tagged_one_of.rb', line 80 def { type: :object, one_of: @matchers } end |
#on(key, schema) ⇒ Object
71 72 73 |
# File 'lib/parametric/tagged_one_of.rb', line 71 def on(key, schema) @matchers[key] = schema end |