Module: Schematist::DSL::Conditionals
- Included in:
- Schematist::DSL
- Defined in:
- lib/schematist/dsl/conditionals.rb
Instance Method Summary collapse
- #conditions ⇒ Object
- #dependencies ⇒ Object
- #dependent(property, &block) ⇒ Object
-
#given(condition = nil, **properties, &block) ⇒ Object
Matches on property values, or on any schema when one is passed explicitly: given(status: "shipped") { ... } given(%w[tax_id]) { ... }.
Instance Method Details
#conditions ⇒ Object
6 7 8 |
# File 'lib/schematist/dsl/conditionals.rb', line 6 def conditions @conditions ||= [] end |
#dependencies ⇒ Object
10 11 12 |
# File 'lib/schematist/dsl/conditionals.rb', line 10 def dependencies @dependencies ||= {} end |
#dependent(property, &block) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/schematist/dsl/conditionals.rb', line 14 def dependent(property, &block) builder = ConditionalBuilder.new builder.instance_eval(&block) dependencies[property.to_s] = builder end |
#given(condition = nil, **properties, &block) ⇒ Object
Matches on property values, or on any schema when one is passed explicitly:
given(status: "shipped") { ... }
given({required: %w[tax_id]}) { ... }
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/schematist/dsl/conditionals.rb', line 24 def given(condition = nil, **properties, &block) raise ArgumentError, "given requires a condition" if condition.nil? && properties.empty? if_schema = condition || { properties: properties.transform_keys(&:to_s).transform_values { |v| coerce_condition(v) }, required: properties.keys.map(&:to_s) } then_builder = ConditionalBuilder.new else_builder = ConditionalBuilder.new context = ConditionalContext.new(then_builder, else_builder) context.instance_eval(&block) condition = {if: if_schema, then: then_builder.to_schema} condition[:else] = else_builder.to_schema unless else_builder.empty? conditions << condition end |