Class: Legion::Data::Extract::Handlers::Base

Inherits:
Object
  • Object
show all
Extended by:
Logging::Helper
Defined in:
lib/legion/data/extract/handlers/base.rb

Direct Known Subclasses

Csv, Docx, Html, Json, Jsonl, Markdown, Pdf, Pptx, Text, Vtt, Xlsx

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging::Helper

handle_exception

Class Attribute Details

.registryObject (readonly)

Returns the value of attribute registry.



15
16
17
# File 'lib/legion/data/extract/handlers/base.rb', line 15

def registry
  @registry
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
# File 'lib/legion/data/extract/handlers/base.rb', line 50

def available?
  return true if gem_name.nil?

  require gem_name
  true
rescue LoadError => e
  handle_exception(e, level: :debug, handled: true, operation: :extract_handler_available, handler: name, gem: gem_name)
  false
end

.extensionsObject



43
# File 'lib/legion/data/extract/handlers/base.rb', line 43

def extensions = []

.extract(_source) ⇒ Object

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/legion/data/extract/handlers/base.rb', line 46

def extract(_source)
  raise NotImplementedError, "#{name} must implement .extract"
end

.for_type(type) ⇒ Object



33
34
35
# File 'lib/legion/data/extract/handlers/base.rb', line 33

def for_type(type)
  @registry[type&.to_sym]
end

.gem_nameObject



44
# File 'lib/legion/data/extract/handlers/base.rb', line 44

def gem_name = nil

.inherited(subclass) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/data/extract/handlers/base.rb', line 17

def inherited(subclass)
  super
  # Deferred registration — subclass defines type after class body loads
  TracePoint.new(:end) do |tp|
    if tp.self == subclass
      register(subclass) if subclass.respond_to?(:type) && subclass.type
      tp.disable
    end
  end.enable
end

.register(handler_class) ⇒ Object



28
29
30
31
# File 'lib/legion/data/extract/handlers/base.rb', line 28

def register(handler_class)
  log.debug "Registered extract handler type=#{handler_class.type} class=#{handler_class.name}"
  @registry = @registry.merge(handler_class.type => handler_class).freeze
end

.supported_typesObject



37
38
39
# File 'lib/legion/data/extract/handlers/base.rb', line 37

def supported_types
  @registry.keys
end

.typeObject

Override in subclasses



42
# File 'lib/legion/data/extract/handlers/base.rb', line 42

def type = nil