Class: TextExtractor
- Inherits:
-
Object
show all
- Defined in:
- lib/text_extractor.rb,
lib/text_extractor/value.rb,
lib/text_extractor/record.rb,
lib/text_extractor/version.rb,
lib/text_extractor/filldown.rb,
lib/text_extractor/directives.rb,
lib/text_extractor/extraction.rb,
lib/text_extractor/inline_value.rb,
lib/text_extractor/directives/group.rb,
lib/text_extractor/directives/classes.rb
Overview
represents an extractor definition
Defined Under Namespace
Modules: Patterns
Classes: Directives, Extraction, Filldown, InlineValue, Record, State, Value
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#boolean(id, re = Patterns::BOOLEAN) ⇒ Object
-
#filldown(**kwargs, &block) ⇒ Object
-
#find_record_for(match) ⇒ Object
-
#float(id, re = Patterns::FLOAT) ⇒ Object
-
#initialize(&block) ⇒ TextExtractor
constructor
A new instance of TextExtractor.
-
#inline(id, &block) ⇒ Object
-
#integer(id, re = Patterns::INTEGER) ⇒ Object
-
#ipaddr(id, re = Patterns::IPADDR) ⇒ Object
-
#ipnetaddr(id, re = Patterns::IPNETADDR) ⇒ Object
-
#rational(id, re = Patterns::RATIONAL) ⇒ Object
-
#record(klass = Record, **kwargs, &block) ⇒ Object
-
#regexps ⇒ Object
-
#scan(input) ⇒ Object
-
#to_re ⇒ Object
-
#value(id, re, &block) ⇒ Object
Constructor Details
Returns a new instance of TextExtractor.
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/text_extractor.rb', line 11
def initialize(&block)
raise "#{self.class}.new requires a block" unless block
@values = {}
@fill = {}
@values = {}
@records = []
@filldowns = []
@current_record_values = []
instance_exec(&block)
end
|
Instance Attribute Details
#records ⇒ Object
Returns the value of attribute records.
9
10
11
|
# File 'lib/text_extractor.rb', line 9
def records
@records
end
|
#values ⇒ Object
Returns the value of attribute values.
9
10
11
|
# File 'lib/text_extractor.rb', line 9
def values
@values
end
|
Class Method Details
.expand_directives(re) ⇒ Object
7
8
9
|
# File 'lib/text_extractor/directives.rb', line 7
def self.expand_directives(re)
Directives.new(re).expand
end
|
.version ⇒ Object
2
3
4
|
# File 'lib/text_extractor/version.rb', line 2
def self.version
'0.3.0'
end
|
Instance Method Details
#boolean(id, re = Patterns::BOOLEAN) ⇒ Object
49
50
51
|
# File 'lib/text_extractor.rb', line 49
def boolean(id, re = Patterns::BOOLEAN)
value(id, re) { |val| !val.match(Patterns::FALSE) }
end
|
#filldown(**kwargs, &block) ⇒ Object
80
81
82
83
|
# File 'lib/text_extractor.rb', line 80
def filldown(**kwargs, &block)
raise "#{self.class}.filldown requires a block" unless block
record(Filldown, **kwargs, &block)
end
|
#find_record_for(match) ⇒ Object
85
86
87
|
# File 'lib/text_extractor.rb', line 85
def find_record_for(match)
records[records.length.times.find_index { |i| match["__#{i}"] }]
end
|
#float(id, re = Patterns::FLOAT) ⇒ Object
57
58
59
|
# File 'lib/text_extractor.rb', line 57
def float(id, re = Patterns::FLOAT)
value(id, re) { |val| Float(val) }
end
|
#inline(id, &block) ⇒ Object
45
46
47
|
# File 'lib/text_extractor.rb', line 45
def inline(id, &block)
@values[id] = InlineValue.new(id, &block)
end
|
#integer(id, re = Patterns::INTEGER) ⇒ Object
53
54
55
|
# File 'lib/text_extractor.rb', line 53
def integer(id, re = Patterns::INTEGER)
value(id, re) { |val| Integer(val) }
end
|
#ipaddr(id, re = Patterns::IPADDR) ⇒ Object
65
66
67
|
# File 'lib/text_extractor.rb', line 65
def ipaddr(id, re = Patterns::IPADDR)
value(id, re) { |val| IPAddr.new(val) }
end
|
#ipnetaddr(id, re = Patterns::IPNETADDR) ⇒ Object
69
70
71
|
# File 'lib/text_extractor.rb', line 69
def ipnetaddr(id, re = Patterns::IPNETADDR)
value(id, re) { |val| IPAddr.new(val) }
end
|
#rational(id, re = Patterns::RATIONAL) ⇒ Object
61
62
63
|
# File 'lib/text_extractor.rb', line 61
def rational(id, re = Patterns::RATIONAL)
value(id, re) { |val| Rational(val) }
end
|
#record(klass = Record, **kwargs, &block) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/text_extractor.rb', line 73
def record(klass = Record, **kwargs, &block)
raise "#{self.class}.record requires a block" unless block
kwargs[:extractor_values] = values
kwargs[:values] = @current_record_values = []
@records << klass.new(instance_exec(&block), **kwargs)
end
|
#regexps ⇒ Object
93
94
95
96
97
|
# File 'lib/text_extractor.rb', line 93
def regexps
@records.map.with_index do |record, i|
Regexp.new("(?<__#{i}>#{record.source})", record.options)
end
end
|
#scan(input) ⇒ Object
89
90
91
|
# File 'lib/text_extractor.rb', line 89
def scan(input)
.new(input, self).scan.
end
|
#to_re ⇒ Object
99
100
101
|
# File 'lib/text_extractor.rb', line 99
def to_re
Regexp.union(*regexps)
end
|
#value(id, re, &block) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/text_extractor.rb', line 37
def value(id, re, &block)
val = @values[id] = Value.new(id, re, &block)
define_singleton_method(id) do
@current_record_values << val
"(?<#{id}>#{re.source})"
end
end
|