Class: DocCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/tochka_cyclops_api/methods.rb

Instance Method Summary collapse

Instance Method Details

#predicate_description(name, args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tochka_cyclops_api/methods.rb', line 53

def predicate_description(name, args)
  case name
  when :str? then "string"
  when :bool? then "true/false"
  when :filled? then "filled"
  when :int? then "integer"
  when :gt? then "greater than #{args[0]}"
  else
    raise NotImplementedError, "#{name} not supported yet"
  end
end

#visit(node) ⇒ Object



12
13
14
15
# File 'lib/tochka_cyclops_api/methods.rb', line 12

def visit(node)
  meth, rest = node
  public_send(:"visit_#{meth}", rest)
end

#visit_and(node) ⇒ Object



21
22
23
24
# File 'lib/tochka_cyclops_api/methods.rb', line 21

def visit_and(node)
  left, right = node
  [visit(left), visit(right)].compact
end

#visit_implication(node) ⇒ Object



40
41
42
43
# File 'lib/tochka_cyclops_api/methods.rb', line 40

def visit_implication(node)
  _, right = node.map(&method(:visit))
  right.merge(optional: true)
end

#visit_key(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tochka_cyclops_api/methods.rb', line 26

def visit_key(node)
  name, rest = node

  predicates = visit(rest).flatten

  if predicates[0].is_a? Symbol
    validations = predicate_description(predicates[0], predicates[1])

    { key: name, validations: validations }
  else
    { key: name, validations: predicates }
  end
end

#visit_predicate(node) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/tochka_cyclops_api/methods.rb', line 45

def visit_predicate(node)
  name, args = node

  return if name.equal?(:key?)

  { name => args.map(&:last).reject { |v| v.equal?(Dry::Schema::Undefined) } }
end

#visit_set(nodes) ⇒ Object



17
18
19
# File 'lib/tochka_cyclops_api/methods.rb', line 17

def visit_set(nodes)
  nodes.map { |node| visit(node) }.flatten(1)
end