Class: Slither::Section
- Inherits:
-
Object
show all
- Defined in:
- lib/slither/section.rb
Constant Summary
collapse
- RESERVED_NAMES =
[:spacer]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, options = {}) ⇒ Section
Returns a new instance of Section.
8
9
10
11
12
13
14
|
# File 'lib/slither/section.rb', line 8
def initialize(name, options = {})
@name = name
@options = options
@columns = []
@trap = options[:trap]
@optional = options[:optional] || false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
65
66
67
|
# File 'lib/slither/section.rb', line 65
def method_missing(method, *args)
column(method, *args)
end
|
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
4
5
6
|
# File 'lib/slither/section.rb', line 4
def columns
@columns
end
|
#definition ⇒ Object
Returns the value of attribute definition.
3
4
5
|
# File 'lib/slither/section.rb', line 3
def definition
@definition
end
|
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/slither/section.rb', line 4
def name
@name
end
|
#optional ⇒ Object
Returns the value of attribute optional.
3
4
5
|
# File 'lib/slither/section.rb', line 3
def optional
@optional
end
|
#options ⇒ Object
Returns the value of attribute options.
4
5
6
|
# File 'lib/slither/section.rb', line 4
def options
@options
end
|
Instance Method Details
#column(name, length, options = {}) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/slither/section.rb', line 16
def column(name, length, options = {})
raise(Slither::DuplicateColumnNameError, "You have already defined a column named '#{name}'.") if @columns.map do |c|
RESERVED_NAMES.include?(c.name) ? nil : c.name
end.flatten.include?(name)
col = Column.new(name, length, @options.merge(options))
@columns << col
col
end
|
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/slither/section.rb', line 41
def format(data)
row = ''
@columns.each do |column|
row += column.format(data[column.name])
end
row
end
|
#match(raw_line) ⇒ Object
61
62
63
|
# File 'lib/slither/section.rb', line 61
def match(raw_line)
raw_line.nil? ? false : @trap.call(raw_line)
end
|
#parse(line) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/slither/section.rb', line 52
def parse(line)
line_data = line.unpack(unpacker)
row = {}
@columns.each_with_index do |c, i|
row[c.name] = c.parse(line_data[i]) unless RESERVED_NAMES.include?(c.name)
end
row
end
|
#spacer(length) ⇒ Object
25
26
27
|
# File 'lib/slither/section.rb', line 25
def spacer(length)
column(:spacer, length)
end
|
#template(name) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/slither/section.rb', line 33
def template(name)
template = @definition.templates[name]
raise ArgumentError, "Template #{name} not found as a known template." unless template
@columns = @columns + template.columns
@options = template.options.merge(@options)
end
|
#trap(&block) ⇒ Object
29
30
31
|
# File 'lib/slither/section.rb', line 29
def trap(&block)
@trap = block
end
|