Class: JsonDataExtractor::OptimizedExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/json_data_extractor/optimized_extractor.rb

Overview

High-performance single-pass extractor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, modifiers: {}) ⇒ OptimizedExtractor

Returns a new instance of OptimizedExtractor.



10
11
12
13
# File 'lib/json_data_extractor/optimized_extractor.rb', line 10

def initialize(schema, modifiers: {})
  @modifiers = modifiers.transform_keys(&:to_sym)
  @schema_analyzer = SchemaAnalyzer.new(schema, @modifiers)
end

Instance Attribute Details

#modifiersObject (readonly)

Returns the value of attribute modifiers.



8
9
10
# File 'lib/json_data_extractor/optimized_extractor.rb', line 8

def modifiers
  @modifiers
end

Instance Method Details

#add_modifier(modifier_name, callable = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
# File 'lib/json_data_extractor/optimized_extractor.rb', line 30

def add_modifier(modifier_name, callable = nil, &block)
  modifier_name = modifier_name.to_sym unless modifier_name.is_a?(Symbol)
  @modifiers[modifier_name] = callable || block

  return if @modifiers[modifier_name].respond_to?(:call)

  raise ArgumentError, 'Modifier must be a callable object or a block'
end

#extract_from(json_data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/json_data_extractor/optimized_extractor.rb', line 15

def extract_from(json_data)
  # Pre-allocate result from template
  result = deep_dup(@schema_analyzer.result_template)

  # Parse JSON once
  data = parse_data(json_data)

  # Execute extraction plan
  @schema_analyzer.extraction_plan.each do |instruction|
    extract_and_fill(data, instruction, result)
  end

  result
end