Class: Ubi::Memoria::Base
- Inherits:
-
Object
- Object
- Ubi::Memoria::Base
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/ubi/memoria.rb
Overview
Memoria Base
Instance Attribute Summary collapse
-
#hint ⇒ Object
Returns the value of attribute hint.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
- .extract_text(datum) ⇒ Object
-
.inherited(base) ⇒ Object
Account for memorias.
-
.key ⇒ Object
Machine-readable name of this class.
-
.name ⇒ Object
Human-readable name of this class.
-
.parse(datum, hint = :br) ⇒ Object
Scan for memoria regex and map to new memoria if found.
-
.parse!(datum, hint = :br) ⇒ Object
Scans and removes matches from original.
- .plural ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #format ⇒ Object
-
#initialize(text, hint = nil, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #parser ⇒ Object
- #to_s ⇒ Object
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
#hint ⇒ Object
Returns the value of attribute hint.
6 7 8 |
# File 'lib/ubi/memoria.rb', line 6 def hint @hint end |
#opts ⇒ Object
Returns the value of attribute opts.
6 7 8 |
# File 'lib/ubi/memoria.rb', line 6 def opts @opts end |
#text ⇒ Object
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 |
.key ⇒ Object
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 |
.name ⇒ Object
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 |
.plural ⇒ Object
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 |
#format ⇒ Object
19 20 21 |
# File 'lib/ubi/memoria.rb', line 19 def format text end |
#parser ⇒ Object
15 16 17 |
# File 'lib/ubi/memoria.rb', line 15 def parser # Implemented on subclasses end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/ubi/memoria.rb', line 23 def to_s format end |