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, 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

.==(other) ⇒ Object



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

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

.extract_text(datum) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/ubi/memoria.rb', line 37

def extract_text(datum)
  case datum
  when String then datum
  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



31
32
33
34
35
# File 'lib/ubi/memoria.rb', line 31

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



67
68
69
70
# File 'lib/ubi/memoria.rb', line 67

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

.nameObject

Human-readable name of this class



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

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

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

Scan for memoria regex and map to new memoria if found



50
51
52
53
54
# File 'lib/ubi/memoria.rb', line 50

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



58
59
60
61
62
# File 'lib/ubi/memoria.rb', line 58

def parse!(datum, hint = :br)
  res = parse(datum, hint)
  res.each { |m| datum.tap { |d| d.slice!(m.text) }.strip! }
  res
end

.pluralObject



79
80
81
# File 'lib/ubi/memoria.rb', line 79

def plural
  "#{key}s"
end

Instance Method Details

#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