Class: Ubi::Memoria::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/ubi/memoria.rb

Overview

Memoria Base

Direct Known Subclasses

Address, Document, Email, Image, Phone, Site, Social, Who

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, hint = nil, opts = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'lib/ubi/memoria.rb', line 8

def initialize(text, hint = nil, opts = {})
  @text = text
  @hint = hint
  @opts = opts
  parser
end

Instance Attribute Details

#hintObject

Returns the value of attribute hint.



6
7
8
# File 'lib/ubi/memoria.rb', line 6

def hint
  @hint
end

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/ubi/memoria.rb', line 6

def opts
  @opts
end

#textObject

Returns the value of attribute text.



6
7
8
# File 'lib/ubi/memoria.rb', line 6

def text
  @text
end

Class Method Details

.extract_text(datum) ⇒ Object



42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

Account for memorias



36
37
38
39
40
# 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

.keyObject

Machine-readable name of this class



73
74
75
76
# File 'lib/ubi/memoria.rb', line 73

def key
  @key ||= to_s.split('::').last.downcase.to_sym
  # fail "Not implemented by #{self}"
end

.nameObject

Human-readable name of this class



81
82
83
# File 'lib/ubi/memoria.rb', line 81

def name
  to_s.split('::').last
end

.parse(datum, hint = :br) ⇒ Object

Scan for memoria regex and map to new memoria if found



56
57
58
59
60
# 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) ⇒ Object

Scans and removes matches from original



64
65
66
67
68
# 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

.pluralObject



85
86
87
# File 'lib/ubi/memoria.rb', line 85

def plural
  "#{key}s"
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
# File 'lib/ubi/memoria.rb', line 27

def ==(other)
  return unless other.respond_to?(:text)
  text == other.text
end

#formatObject



19
20
21
# File 'lib/ubi/memoria.rb', line 19

def format
  text
end

#parserObject



15
16
17
# File 'lib/ubi/memoria.rb', line 15

def parser
  # Implemented on subclasses
end

#to_sObject



23
24
25
# File 'lib/ubi/memoria.rb', line 23

def to_s
  format
end