Module: AcroForge::Schema

Defined in:
lib/acroforge/schema.rb

Class Method Summary collapse

Class Method Details

.aggregate_proposals(proposals) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/acroforge/schema.rb', line 112

def aggregate_proposals(proposals)
  proposals.each_with_object({}) do |p, schema|
    next if p[:canonical_key].nil?

    key = p[:canonical_key].to_sym
    schema[key] ||= {type: infer_type(p), variations: []}
    if p[:raw_label]
      cleaned = humanize_label(p[:raw_label])
      schema[key][:variations] << cleaned unless schema[key][:variations].include?(cleaned)
    end

    if p[:pdf_field_type] == :button && p[:options]
      schema[key][:options] = p[:options].keys.map(&:to_sym).uniq
    end
  end
end

.dump(schema, path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/acroforge/schema.rb', line 25

def dump(schema, path)
  stringified = stringify_schema(schema)
  case File.extname(path).downcase
  when ".yml", ".yaml"
    File.write(path, YAML.dump(stringified))
  when ".json"
    File.write(path, JSON.pretty_generate(stringified))
  else
    raise ArgumentError, "unknown schema file extension: #{path.inspect}"
  end
end

.humanize_label(label) ⇒ Object

Thin delegator. The real implementation lives in AcroForge::Labels so the Engine can apply it at the source (right after the spatial heuristic picks a label) without creating a circular require between engine.rb and schema.rb.



133
134
135
# File 'lib/acroforge/schema.rb', line 133

def humanize_label(label)
  AcroForge::Labels.humanize(label)
end

.infer(pdf_path, sections: [], engine: nil) ⇒ Object

Infer a schema from a PDF.

If ‘engine:` is given, the caller has already compiled an engine and we use its proposals directly. This lets callers (notably the CLI’s ‘bootstrap` subcommand) avoid a redundant second compile when they also want to call Relabeler.propose on the same PDF.



101
102
103
104
105
106
107
108
109
110
# File 'lib/acroforge/schema.rb', line 101

def infer(pdf_path, sections: [], engine: nil)
  return aggregate_proposals(engine.field_proposals) if engine

  require "tmpdir"
  Dir.mktmpdir do |tmp|
    e = AcroForge::Engine.new(pdf_path, sections: sections, normalized_dir: tmp)
    e.compile!
    aggregate_proposals(e.field_proposals)
  end
end

.infer_type(proposal) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/acroforge/schema.rb', line 137

def infer_type(proposal)
  case proposal[:pdf_field_type]
  when :button
    ((proposal[:options]&.size || 0) > 1) ? :select : :boolean
  when :choice
    :select
  else
    label = proposal[:raw_label].to_s.downcase
    case label
    when /amount|salary|income|balance|fee|tier3/ then :money
    when /\bdate\b|birth|expiry|employed/ then :date
    when /email/ then :email
    when /years|tenor|number of|\bno\.?\b/ then :number
    else :string
    end
  end
end

.load(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/acroforge/schema.rb', line 12

def load(path)
  raw = case File.extname(path).downcase
  when ".yml", ".yaml"
    YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: true)
  when ".json"
    JSON.parse(File.read(path), symbolize_names: false)
  else
    raise ArgumentError, "unknown schema file extension: #{path.inspect}"
  end

  normalize(symbolize_schema(raw))
end

.merge(schema, mapping_entries) ⇒ Object

Merge a mapping file’s hand-reviewed decisions back into a schema. Each non-null mapping entry contributes a canonical key (stripped of any _N collision suffix), its type, and its raw_label as a variation. Existing schema entries keep their type but gain new variations; missing entries are created. Returns the merged schema hash.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/acroforge/schema.rb', line 160

def merge(schema, mapping_entries)
  result = schema.each_with_object({}) do |(k, v), out|
    out[k] = v.is_a?(Hash) ? v.dup.tap { |d| d[:variations] = (d[:variations] || []).dup } : v
  end

  mapping_entries.each do |pdf_field_name, entry|
    next if pdf_field_name.to_s.start_with?("_")
    next unless entry.is_a?(Hash)

    key_str = entry["key"]
    next if key_str.nil? || key_str.to_s.empty?

    # Strip the _N collision suffix the engine appends when multiple
    # fields map to the same canonical key (full_name_1, full_name_2).
    canonical = key_str.to_s.sub(/_\d+\z/, "").to_sym

    type_str = entry["type"]
    type_sym = type_str.is_a?(String) ? type_str.to_sym : type_str

    result[canonical] ||= {type: type_sym || :string, variations: []}
    # Don't overwrite an existing type unless one is actually given
    result[canonical][:type] = type_sym if type_sym

    raw_label = entry.dig("meta", "raw_label")
    if raw_label && !raw_label.to_s.empty?
      variations = result[canonical][:variations] ||= []
      variations << raw_label.to_s unless variations.include?(raw_label.to_s)
    end
  end

  result
end

.normalize(input) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/acroforge/schema.rb', line 193

def normalize(input)
  return {} if input.nil? || input.empty?

  input.each_with_object({}) do |(key, value), out|
    out[key] = case value
    when Array
      {type: :string, variations: value}
    when Hash
      value
    else
      raise ArgumentError, "Schema entry for #{key.inspect} must be an Array or Hash, got #{value.class}"
    end
  end
end

.stringify_entry(entry) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/acroforge/schema.rb', line 73

def stringify_entry(entry)
  return entry unless entry.is_a?(Hash)

  result = {}
  entry.each do |k, v|
    str_k = k.to_s
    result[str_k] = case k.to_sym
    when :type
      v.is_a?(Symbol) ? v.to_s : v
    when :options
      if v.is_a?(Array)
        v.map { |item| item.is_a?(Symbol) ? item.to_s : item }
      else
        v
      end
    else
      v
    end
  end
  result
end

.stringify_schema(schema) ⇒ Object



67
68
69
70
71
# File 'lib/acroforge/schema.rb', line 67

def stringify_schema(schema)
  schema.each_with_object({}) do |(key, value), out|
    out[key.to_s] = stringify_entry(value)
  end
end

.symbolize_entry(entry) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/acroforge/schema.rb', line 45

def symbolize_entry(entry)
  return entry unless entry.is_a?(Hash)

  result = {}
  entry.each do |k, v|
    sym_k = k.to_sym
    result[sym_k] = case sym_k
    when :type
      v.is_a?(String) ? v.to_sym : v
    when :options
      if v.is_a?(Array)
        v.map { |item| item.is_a?(String) ? item.to_sym : item }
      else
        v
      end
    else
      v
    end
  end
  result
end

.symbolize_schema(raw_hash) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/acroforge/schema.rb', line 37

def symbolize_schema(raw_hash)
  return {} if raw_hash.nil? || raw_hash.empty?

  raw_hash.each_with_object({}) do |(key, value), out|
    out[key.to_sym] = symbolize_entry(value)
  end
end