Class: Slither::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/slither/definition.rb

Overview

A Definition is the parent object that contains the information about how a fixed-width file is formatted. It contains a collection of sections, each of which contains a collection of fields.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Definition

Initializes a new Definition object.

Parameters:

  • options (Hash) (defaults to: {})

    An optional hash of configuration options.

Options Hash (options):

  • :align (Symbol) — default: :right

    The alignment for fields, can be :left, :right, or :center.

  • :by_bytes (Boolean) — default: true

    Whether to align fields by bytes or characters.



15
16
17
18
19
# File 'lib/slither/definition.rb', line 15

def initialize(options = {})
  @sections = []
  @templates = {}
  @options = { align: :right, by_bytes: true }.merge(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Section

Provides a way to define sections using method calls. For example,

you can call `my_section` instead of `section('my_section')`.

Parameters:

  • method (Symbol)

    The name of the section.

  • args (Array)

    Additional arguments.

  • block (Block)

    A block for defining fields within the section.

Returns:

  • (Section)

    The newly created section.



79
80
81
# File 'lib/slither/definition.rb', line 79

def method_missing(method, *args, &block) # rubocop:disable Style/MissingRespondToMissing
  section(method, *args, &block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/slither/definition.rb', line 7

def options
  @options
end

#sectionsObject (readonly)

Returns the value of attribute sections.



7
8
9
# File 'lib/slither/definition.rb', line 7

def sections
  @sections
end

#templatesObject (readonly)

Returns the value of attribute templates.



7
8
9
# File 'lib/slither/definition.rb', line 7

def templates
  @templates
end

Instance Method Details

#section(name, options = {}) {|Section| ... } ⇒ Section

Defines a new Section within the Definition.

Examples:

Define a section for the “header” part of the fixed-width file.

definition.section(:header, align: :left) do |section|
  # The trap tells Slither which lines should fall into this section
  section.trap { |line| line[0,4] == 'HEAD' }
  # Use the boundary template for the columns
  section.template(:boundary)
end

Parameters:

  • name (String)

    The name of the section.

  • options (Hash) (defaults to: {})

    An optional hash of section-specific configuration options.

Yields:

  • (Section)

    A block for defining fields within the section.

Yield Parameters:

  • section (Section)

    The section object to be configured.

Returns:

  • (Section)

    The newly created section.

Raises:

  • (ArgumentError)

    if the section name is reserved or already defined.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/slither/definition.rb', line 39

def section(name, options = {}, &block)
  if section_using_reserved_name?(name) || section_already_defined?(name)
    raise ArgumentError, "Reserved or duplicate section name: '#{name}'"
  end

  section = Slither::Section.new(name, @options.merge(options))
  section.definition = self

  yield(section) if block

  sections << section
  section
end

#template(name, options = {}) {|section| ... } ⇒ Object

Defines a template, which can be reused to create multiple sections with the same configuration.

Examples:

Define a template for the “boundary” part of the fixed-width file.

definition.template(:boundary) do |section|
  section.column(:record_type, 4)
  section.column(::company_id, 12)

Parameters:

  • name (String)

    The name of the template.

  • options (Hash) (defaults to: {})

    An optional hash of template-specific configuration options.

Yields:

  • (section)

    A block for configuring the template.

Yield Parameters:

  • section (Section)

    The template object to be configured.



65
66
67
68
69
# File 'lib/slither/definition.rb', line 65

def template(name, options = {}, &block)
  section = Slither::Section.new(name, @options.merge(options))
  yield(section) if block
  @templates[name] = section
end