Class: JsonDataExtractor::Extractor

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

Overview

Main extractor class - delegates to OptimizedExtractor when possible

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_data, modifiers = {}) ⇒ Extractor

Returns a new instance of Extractor.

Parameters:

  • json_data (Hash, String)
  • modifiers (Hash) (defaults to: {})


10
11
12
13
14
15
# File 'lib/json_data_extractor/extractor.rb', line 10

def initialize(json_data, modifiers = {})
  @data = json_data.is_a?(Hash) ? Oj.dump(json_data, mode: :compat) : json_data
  @modifiers = modifiers.transform_keys(&:to_sym)
  @results = {}
  @path_cache = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/json_data_extractor/extractor.rb', line 6

def data
  @data
end

#modifiersObject (readonly)

Returns the value of attribute modifiers.



6
7
8
# File 'lib/json_data_extractor/extractor.rb', line 6

def modifiers
  @modifiers
end

#schema_cacheObject (readonly)

Returns the value of attribute schema_cache.



6
7
8
# File 'lib/json_data_extractor/extractor.rb', line 6

def schema_cache
  @schema_cache
end

Class Method Details

.with_schema(schema, modifiers = {}) ⇒ Extractor

Creates a new extractor with a pre-processed schema

Parameters:

  • schema (Hash)

    schema of the expected data mapping

  • modifiers (Hash) (defaults to: {})

    modifiers to apply to the extracted data

Returns:

  • (Extractor)

    an extractor initialized with the schema



21
22
23
24
25
26
# File 'lib/json_data_extractor/extractor.rb', line 21

def self.with_schema(schema, modifiers = {})
  extractor = new({}, modifiers)
  extractor.instance_variable_set(:@schema_cache, SchemaCache.new(schema))
  extractor.instance_variable_set(:@optimized_extractor, OptimizedExtractor.new(schema, modifiers: modifiers))
  extractor
end

Instance Method Details

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

Parameters:

  • modifier_name (String, Symbol)
  • callable (#call, nil) (defaults to: nil)

    Optional callable object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/json_data_extractor/extractor.rb', line 48

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

  # Also add to optimized extractor if present
  @optimized_extractor&.add_modifier(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(schema) ⇒ Object

Parameters:

  • schema (Hash)

    schema of the expected data mapping



61
62
63
64
65
# File 'lib/json_data_extractor/extractor.rb', line 61

def extract(schema)
  # Use optimized path for direct extraction
  optimized = OptimizedExtractor.new(schema, modifiers: @modifiers)
  return optimized.extract_from(@data)
end

#extract_from(json_data) ⇒ Hash

Extracts data from the provided json_data using the cached schema

Parameters:

  • json_data (Hash, String)

    the data to extract from

Returns:

  • (Hash)

    the extracted data

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/json_data_extractor/extractor.rb', line 31

def extract_from(json_data)
  # Use optimised extractor if available
  if @optimized_extractor
    return @optimized_extractor.extract_from(json_data)
  end

  # Fallback to original implementation
  raise ArgumentError, 'No schema cache available. Use Extractor.with_schema first.' unless @schema_cache

  @results = {}
  @data = json_data.is_a?(Hash) ? Oj.dump(json_data, mode: :compat) : json_data
  extract_using_cache
  @results
end