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
- .==(other) ⇒ Object
- .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
- #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
.==(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 |
.key ⇒ Object
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 |
.name ⇒ Object
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 |
.plural ⇒ Object
79 80 81 |
# File 'lib/ubi/memoria.rb', line 79 def plural "#{key}s" end |
Instance Method Details
#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 |