class Ubi::Memoria::Base

Memoria Base

Attributes

hint[RW]
opts[RW]
text[RW]

Public Class Methods

extract_text(datum) click to toggle source
# File lib/ubi/memoria.rb, line 42
def extract_text(datum)
  case datum
  when String then datum
  when Ubi::Aranea then datum.text
  when Nokogiri::HTML then datum.data.text
  # when PDF / DOC / IMG (tesseract it =) then datum.data.text
  else fail "Can't parse `#{datum.class}`"
  end
end
inherited(base) click to toggle source

Account for memorias

# File lib/ubi/memoria.rb, line 36
def inherited(base)
  fail "Already defined #{base.key}" if Ubi.memorias.include?(base)
  # puts "With memoria #{base}"
  Ubi.memorias << base
end
key() click to toggle source

Machine-readable name of this class

# File lib/ubi/memoria.rb, line 73
def key
  @key ||= to_s.split('::').last.downcase.to_sym
  # fail "Not implemented by #{self}"
end
name() click to toggle source

Human-readable name of this class

# File lib/ubi/memoria.rb, line 81
def name
  to_s.split('::').last
end
new(text, hint = nil, opts = {}) click to toggle source
# File lib/ubi/memoria.rb, line 8
def initialize(text, hint = nil, opts = {})
  @text = text
  @hint = hint
  @opts = opts
  parser
end
parse(datum, hint = :br) click to toggle source

Scan for memoria regex and map to new memoria if found

@returns Array [Memoria, Memoria…]

# File lib/ubi/memoria.rb, line 56
def parse(datum, hint = :br)
  fail "Not implemented by #{self}" unless defined?(:regex)
  extract_text(datum).scan(regex(hint))
    .map { |r| new(r.first, hint) }
end
parse!(datum, hint = :br) click to toggle source

Scans and removes matches from original

# File lib/ubi/memoria.rb, line 64
def parse!(datum, hint = :br)
  res = parse(datum, hint)
  res.each { |m| datum.tap { |d| d.slice!(m.text) }.strip! }
  res
end
plural() click to toggle source
# File lib/ubi/memoria.rb, line 85
def plural
  "#{key}s"
end

Public Instance Methods

==(other) click to toggle source
# File lib/ubi/memoria.rb, line 27
def ==(other)
  return unless other.respond_to?(:text)
  text == other.text
end
format() click to toggle source
# File lib/ubi/memoria.rb, line 19
def format
  text
end
parser() click to toggle source
# File lib/ubi/memoria.rb, line 15
def parser
  # Implemented on subclasses
end
to_s() click to toggle source
# File lib/ubi/memoria.rb, line 23
def to_s
  format
end