Module: JsonSchematize::Introspect::ClassMethods

Included in:
Generator
Defined in:
lib/json_schematize/introspect_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#__field_type__(field) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/json_schematize/introspect_class_methods.rb', line 27

def __field_type__(field)
  types = Array(field.type || field.types)
  deep_introspection = false
  type_string = if types.length == 0
    "Anything"
  elsif types.length == 1
    type = types.first
    if JsonSchematize::Generator > type
      deep_introspection = true
      if field.array_of_types
        "Array of #{type}"
      else
        type
      end
    else
      type
    end
  else
    "One of [#{types}]"
  end

  {
    humanize: type_string,
    types:,
    deep_introspection:,
  }
end

#introspect(deep: false, prepend_dig: [], introspection: {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/json_schematize/introspect_class_methods.rb', line 5

def introspect(deep: false, prepend_dig: [], introspection: {})
  fields.each do |field|
    naming = (prepend_dig + field.dig).compact
     = __field_type__(field)
    introspection[naming.join(".")] = {
      required: field.required,
      allowed: [:humanize],
    }

    if deep && [:deep_introspection]
      prepended_naming = if field.array_of_types
        naming.dup.tap { _1[-1] = "#{_1[-1]}[x]"}
      else
        naming.dup
      end
      [:types][0].introspect(deep:, prepend_dig: prepended_naming, introspection: introspection)
    end
  end

  introspection
end