Class: Slither::Section
- Inherits:
-
Object
- Object
- Slither::Section
- Defined in:
- lib/slither/section.rb
Constant Summary collapse
- RESERVED_NAMES =
[:spacer]
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #column(name, length, options = {}) ⇒ Object
-
#format(data) ⇒ Object
Format a data Hash using columns width.
-
#initialize(name, options = {}) ⇒ Section
constructor
A new instance of Section.
- #match(raw_line) ⇒ Object
- #method_missing(method, *args) ⇒ Object
- #parse(line) ⇒ Object
- #parse_when_problem(line) ⇒ Object
- #spacer(length) ⇒ Object
- #template(name) ⇒ Object
- #trap(&block) ⇒ Object
Constructor Details
permalink #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, = {}) @name = name @options = @columns = [] @trap = [:trap] @optional = [:optional] || false @length = 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
permalink #method_missing(method, *args) ⇒ Object
[View source]
84 85 86 |
# File 'lib/slither/section.rb', line 84 def method_missing(method, *args) column(method, *args) end |
Instance Attribute Details
permalink #columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/slither/section.rb', line 4 def columns @columns end |
permalink #definition ⇒ Object
Returns the value of attribute definition.
3 4 5 |
# File 'lib/slither/section.rb', line 3 def definition @definition end |
permalink #length ⇒ Object (readonly)
Returns the value of attribute length.
4 5 6 |
# File 'lib/slither/section.rb', line 4 def length @length end |
permalink #name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/slither/section.rb', line 4 def name @name end |
permalink #optional ⇒ Object
Returns the value of attribute optional.
3 4 5 |
# File 'lib/slither/section.rb', line 3 def optional @optional end |
permalink #options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/slither/section.rb', line 4 def @options end |
Instance Method Details
permalink #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, = {}) 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()) @columns << col @length += length col end |
permalink #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 |
permalink #match(raw_line) ⇒ Object
[View source]
80 81 82 |
# File 'lib/slither/section.rb', line 80 def match(raw_line) raw_line.nil? ? false : @trap.call(raw_line) end |
permalink #parse(line) ⇒ Object
[View source]
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 |
permalink #parse_when_problem(line) ⇒ Object
[View source]
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 |
permalink #spacer(length) ⇒ Object
[View source]
27 28 29 |
# File 'lib/slither/section.rb', line 27 def spacer(length) column(:spacer, length) end |
permalink #template(name) ⇒ Object
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..merge(@options) end |
permalink #trap(&block) ⇒ Object
[View source]
31 32 33 |
# File 'lib/slither/section.rb', line 31 def trap(&block) @trap = block end |