class PROIEL::PositionalTag

Represents a positional tag, which consists of one or more fields each with its own value. The default implementation is of a positional tag with no fields. The class should be subclassed and the ‘fields` method overridden to implement a non-empty positional tag.

@abstract Subclass and override {#fields} to implement a custom positional tag class.

Constants

UNSET_FIELD

The string representation of a field without a value.

Public Class Methods

new(value = nil) click to toggle source

Creates a new positional tag.

@param value [String, Hash, PositionalTag] initial value

# File lib/proiel/positional_tag.rb, line 20
def initialize(value = nil)
  @fields = Hash.new

  case value
  when NilClass
  when String
    set_value!(fields.zip(value.split('')).to_h)
  when Hash
    set_value!(value)
  when PositionalTag
    set_value!(value.to_h)
  else
    raise ArgumentError, 'expected nil, Hash, String or PositionalTag'
  end
end

Public Instance Methods

<=>(o) click to toggle source

Returns an integer, -1, 0 or 1, suitable for sorting the tag.

@return [Integer]

# File lib/proiel/positional_tag.rb, line 40
def <=>(o)
  to_s <=> o.to_s
end
[](field) click to toggle source

Returns the value of a field. An field without a value is returned as ‘-`.

@param field [String, Symbol] name of field

@return [String]

# File lib/proiel/positional_tag.rb, line 79
def [](field)
  field = field.to_sym

  raise ArgumentError, "invalid field #{field}" unless fields.include?(field)

  @fields[field] || UNSET_FIELD
end
[]=(field, value) click to toggle source

Assigns a value to a field. Removing any value from a field can be done by assigning ‘nil` or `-`.

@param field [String, Symbol] name of field @param value [String, nil]

@return [String]

# File lib/proiel/positional_tag.rb, line 95
def []=(field, value)
  field = field.to_sym

  raise ArgumentError, "invalid field #{field}" unless fields.include?(field)

  if value == UNSET_FIELD or value.nil?
    @fields.delete(field)
  else
    @fields.store(field, value)
  end

  value
end
empty?() click to toggle source

Checks if the tag is unitialized. The tag is uninitialized if no field has a value.

@return [true, false]

# File lib/proiel/positional_tag.rb, line 59
def empty?
  @fields.empty?
end
fields() click to toggle source

Returns the field names. This method should be overridden by implementations. The names should be returned as an array of symbols.

@return [Array<Symbol>]

# File lib/proiel/positional_tag.rb, line 114
def fields
  []
end
to_h() click to toggle source

Returns a hash representation of the tag. The keys are the names of each field as symbols, the values are the values of each field.

@return [Hash<Symbol, String>]

# File lib/proiel/positional_tag.rb, line 68
def to_h
  @fields
end
to_s() click to toggle source

Returns the positional tag as a string.

@return [String]

# File lib/proiel/positional_tag.rb, line 48
def to_s
  # Iterate fields to ensure conversion of fields without a value to
  # UNSET_FIELD.
  fields.map { |field| self[field] }.join
end

Private Instance Methods

set_value!(o) click to toggle source
# File lib/proiel/positional_tag.rb, line 123
def set_value!(o)
  o.each { |k, v| self[k] = v }
end