Module: Pathway::Rspec::FormSchemaHelpers

Includes:
FieldListHelpers
Defined in:
lib/pathway/rspec/matchers/form_schema_helpers.rb

Instance Method Summary collapse

Methods included from FieldListHelpers

#field_list, #pluralize_fields, #were_was

Methods included from ListHelpers

#as_list, #as_sentence

Instance Method Details

#definedObject



36
37
38
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 36

def defined
  @defined ||= @fields & rules.keys
end

#not_definedObject



32
33
34
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 32

def not_defined
  @not_defined ||= @fields - defined
end

#not_defined_listObject



12
13
14
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 12

def not_defined_list
  "#{as_list(not_defined)} #{were_was(not_defined)} not defined" if not_defined.any?
end

#not_optionalObject



28
29
30
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 28

def not_optional
  @not_required ||= defined - optional
end

#not_requiredObject



24
25
26
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 24

def not_required
  @not_required ||= defined - required
end

#optionalObject



20
21
22
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 20

def optional
  @optional ||= @fields.select { |field| optional?(field) }
end

#optional?(field) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 40

def optional?(field)
  if rules[field]&.type == :implication
    left = rules[field].left

    left.type == :predicate && left.name == :key? && left.args.first == field
  end
end

#requiredObject



16
17
18
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 16

def required
  @required ||= @fields.select { |field| required?(field) }
end

#required?(field) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 48

def required?(field)
  if rules[field]&.type == :and
    left = rules[field].left

    left.type == :predicate && left.name == :key? && left.args.first == field
  end
end

#rulesObject



8
9
10
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 8

def rules
  @form.rules
end