Class: Slither::Section

Inherits:
Object
  • 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
15
# File 'lib/slither/section.rb', line 8

def initialize(name, options = {})
  @name = name
  @options = options
  @columns = []
  @trap = options[:trap]
  @optional = options[:optional] || false
  @length = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



84
85
86
# File 'lib/slither/section.rb', line 84

def method_missing(method, *args)
  column(method, *args)
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/slither/section.rb', line 4

def columns
  @columns
end

#definitionObject

Returns the value of attribute definition.



3
4
5
# File 'lib/slither/section.rb', line 3

def definition
  @definition
end

#lengthObject (readonly)

Returns the value of attribute length.



4
5
6
# File 'lib/slither/section.rb', line 4

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/slither/section.rb', line 4

def name
  @name
end

#optionalObject

Returns the value of attribute optional.



3
4
5
# File 'lib/slither/section.rb', line 3

def optional
  @optional
end

#optionsObject (readonly)

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



17
18
19
20
21
22
23
24
25
# File 'lib/slither/section.rb', line 17

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
  @length += length
  col
end

#format(data) ⇒ Object

Format a data Hash using columns width.

  • Data - hash, based on columns definitions content.

Ex: Having the next 2 columns .column(:id, 5) && .column(:name, 10)

we pass the data hash data = { id: 3, name: "Ryan" }
the result is the content of the hash based on the columns width:

format(data)

=> "    3      Ryan"


51
52
53
54
55
56
57
58
59
60
# File 'lib/slither/section.rb', line 51

def format(data)
  # raise( ColumnMismatchError,
  #   "The '#{@name}' section has #{@columns.size} column(s) defined, but there are #{data.size} column(s) provided in the data."
  # ) unless @columns.size == data.size
  row = ''
  @columns.each do |column|
    row += column.format(data[column.name])
  end
  row
end

#match(raw_line) ⇒ Object



80
81
82
# File 'lib/slither/section.rb', line 80

def match(raw_line)
  raw_line.nil? ? false : @trap.call(raw_line)
end

#parse(line) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/slither/section.rb', line 62

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

#parse_when_problem(line) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/slither/section.rb', line 71

def parse_when_problem(line)
  line_data = line.unpack(@columns.map { |c| "a#{c.length}" }.join(''))
  row = ''
  @columns.each_with_index do |c, i|
    row << "\n'#{c.name}':'#{line_data[i]}'" unless RESERVED_NAMES.include?(c.name)
  end
  row
end

#spacer(length) ⇒ Object



27
28
29
# File 'lib/slither/section.rb', line 27

def spacer(length)
  column(:spacer, length)
end

#template(name) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
# File 'lib/slither/section.rb', line 35

def template(name)
  template = @definition.templates[name]
  raise ArgumentError, "Template #{name} not found as a known template." unless template
  @columns += template.columns
  @length += template.length
  # Section options should trump template options
  @options = template.options.merge(@options)
end

#trap(&block) ⇒ Object



31
32
33
# File 'lib/slither/section.rb', line 31

def trap(&block)
  @trap = block
end