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

#accepting_null_listObject



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

def accepting_null_list
  "#{as_list(null_value_allowed)} #{were_was(null_value_allowed)} accepting null values" if @not_allowing_null_values && null_value_allowed.any?
end

#allowing_null_values_matches?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 80

def allowing_null_values_matches?
  @allowing_null_values ? @fields.all? { |field| null_value_allowed?(field) } : true
end

#definedObject



60
61
62
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 60

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

#not_accepting_null_listObject



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

def not_accepting_null_list
  "#{as_list(null_value_disallowed)} #{were_was(null_value_disallowed)} not accepting null values" if @allowing_null_values && null_value_disallowed.any?
end

#not_allowing_null_values_matches?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 84

def not_allowing_null_values_matches?
  @not_allowing_null_values ? @fields.all? { |field| null_value_disallowed?(field) } : true
end

#not_definedObject



56
57
58
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 56

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

#not_defined_listObject



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

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

#not_optionalObject



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

def not_optional
  @not_optional ||= defined - optional
end

#not_requiredObject



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

def not_required
  @not_required ||= defined - required
end

#null_value_allowedObject



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

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

#null_value_allowed?(field) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 88

def null_value_allowed?(field)
  rule = rules[field]&.right&.rule
  predicate = rule&.left
  predicate.present? && predicate.type == :not && predicate.rules&.first&.name == :nil?
end

#null_value_disallowedObject



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

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

#null_value_disallowed?(field) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 94

def null_value_disallowed?(field)
  rule = rules[field]&.right&.rule
  predicate = rule&.left
  predicate.present? && predicate.type == :predicate && predicate.name == :filled?
end

#optionalObject



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

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

#optional?(field) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 64

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



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

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

#required?(field) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/pathway/rspec/matchers/form_schema_helpers.rb', line 72

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



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

def rules
  @form.schema.rules
end