Class: JsonDataExtractor::Extractor
- Defined in:
- lib/json_data_extractor/extractor.rb
Overview
Main extractor class - delegates to OptimizedExtractor when possible
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#modifiers ⇒ Object
readonly
Returns the value of attribute modifiers.
-
#schema_cache ⇒ Object
readonly
Returns the value of attribute schema_cache.
Class Method Summary collapse
-
.with_schema(schema, modifiers = {}) ⇒ Extractor
Creates a new extractor with a pre-processed schema.
Instance Method Summary collapse
- #add_modifier(modifier_name, callable = nil, &block) ⇒ Object
- #extract(schema) ⇒ Object
-
#extract_from(json_data) ⇒ Hash
Extracts data from the provided json_data using the cached schema.
-
#initialize(json_data, modifiers = {}) ⇒ Extractor
constructor
A new instance of Extractor.
Constructor Details
#initialize(json_data, modifiers = {}) ⇒ Extractor
Returns a new instance of Extractor.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/json_data_extractor/extractor.rb', line 6 def data @data end |
#modifiers ⇒ Object (readonly)
Returns the value of attribute modifiers.
6 7 8 |
# File 'lib/json_data_extractor/extractor.rb', line 6 def modifiers @modifiers end |
#schema_cache ⇒ Object (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
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
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
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
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 |