Class: BloodContracts::Core::Pipe
- Defined in:
- lib/blood_contracts/core/pipe.rb
Overview
Meta refinement type, represents pipe of several refinement types
Class Attribute Summary collapse
-
.names ⇒ Array<Symbol>
readonly
List of data transformation step names.
-
.steps ⇒ Array<Refined>
readonly
List of data transformation step.
Attributes inherited from Refined
Class Method Summary collapse
-
.and_then(other_type, **kwargs) ⇒ BC::Pipe
(also: >)
Compose types in a Pipe check Pipe passes data from type to type sequentially.
-
.inspect ⇒ String
Returns text representation of Pipe meta-class.
-
.new(*args, **kwargs, &block) ⇒ Object
rubocop:disable Style/SingleLineMethods.
-
.step(name, type) ⇒ Object
Helper which registers step in validation pipe, also defines a reader.
Instance Method Summary collapse
-
#errors ⇒ Array<Hash<Refined, String>>
List of errors per type during the matching.
-
#initialize ⇒ Pipe
constructor
Constructs a Pipe using the given value (for Pipe steps are also stored in the context).
-
#match ⇒ BC::Refined
The type which is the result of data matching process For PIpe it verifies that data is valid through all data transformation steps.
Methods inherited from Refined
===, call, inherited, #invalid?, match, or_a, #unpack, #valid?
Constructor Details
#initialize ⇒ Pipe
Constructs a Pipe using the given value (for Pipe steps are also stored in the context)
80 81 82 83 84 |
# File 'lib/blood_contracts/core/pipe.rb', line 80 def initialize(*) super @context[:steps] = @context[:steps].to_a @context[:steps_values] = {} end |
Class Attribute Details
.names ⇒ Array<Symbol> (readonly)
List of data transformation step names
15 16 17 |
# File 'lib/blood_contracts/core/pipe.rb', line 15 def names @names end |
.steps ⇒ Array<Refined> (readonly)
List of data transformation step
9 10 11 |
# File 'lib/blood_contracts/core/pipe.rb', line 9 def steps @steps end |
Class Method Details
.and_then(other_type, **kwargs) ⇒ BC::Pipe Also known as: >
Compose types in a Pipe check Pipe passes data from type to type sequentially
rubocop:disable Style/CaseEquality
36 37 38 39 40 41 |
# File 'lib/blood_contracts/core/pipe.rb', line 36 def and_then(other_type, **kwargs) raise ArgumentError unless Class === other_type pipe = Class.new(self) { def inspect; super; end } finalize!(pipe, [self, other_type], kwargs[:names].to_a) pipe end |
.inspect ⇒ String
Returns text representation of Pipe meta-class
63 64 65 66 |
# File 'lib/blood_contracts/core/pipe.rb', line 63 def inspect return super if name "Pipe(#{steps.to_a.join(',')})" end |
.new(*args, **kwargs, &block) ⇒ Object
rubocop:disable Style/SingleLineMethods
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/blood_contracts/core/pipe.rb', line 18 def new(*args, **kwargs, &block) return super(*args, **kwargs) if @finalized names = kwargs.delete(:names) unless kwargs.empty? names ||= [] raise ArgumentError unless args.all? { |type| type < Refined } pipe = Class.new(self) { def inspect; super; end } finalize!(pipe, args, names) pipe.class_eval(&block) if block_given? pipe end |
.step(name, type) ⇒ Object
Helper which registers step in validation pipe, also defines a reader
50 51 52 53 54 55 56 57 |
# File 'lib/blood_contracts/core/pipe.rb', line 50 def step(name, type) raise ArgumentError unless type < Refined @steps << type @names << name define_method(name) do match.context.dig(:steps_values, name) end end |
Instance Method Details
#errors ⇒ Array<Hash<Refined, String>>
List of errors per type during the matching
108 109 110 |
# File 'lib/blood_contracts/core/pipe.rb', line 108 def errors @context[:errors] end |
#match ⇒ BC::Refined
The type which is the result of data matching process For PIpe it verifies that data is valid through all data transformation steps
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/blood_contracts/core/pipe.rb', line 92 def match steps_enumerator.reduce(value) do |next_value, (step, index)| match = next_step_value_match!(step, next_value, index) break match if match.invalid? next match unless block_given? next refine_value(yield(match)) if index < self.class.steps.size - 1 match end end |