Class: PROIEL::PositionalTag Abstract

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/proiel/positional_tag.rb

Overview

This class is abstract.

Subclass and override #fields to implement a custom positional tag class.

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.

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ PositionalTag

Creates a new positional tag.

Parameters:

  • value (String, Hash, PositionalTag) (defaults to: nil)

    initial value



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#<=>(o) ⇒ Integer

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

Returns:

  • (Integer)


40
41
42
# File 'lib/proiel/positional_tag.rb', line 40

def <=>(o)
  to_s <=> o.to_s
end

#[](field) ⇒ String

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

Parameters:

  • field (String, Symbol)

    name of field

Returns:

  • (String)

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
# 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) ⇒ String

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

Parameters:

  • field (String, Symbol)

    name of field

  • value (String, nil)

Returns:

  • (String)

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
# 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?true, false

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

Returns:

  • (true, false)


59
60
61
# File 'lib/proiel/positional_tag.rb', line 59

def empty?
  @fields.empty?
end

#fieldsArray<Symbol>

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

Returns:

  • (Array<Symbol>)


114
115
116
# File 'lib/proiel/positional_tag.rb', line 114

def fields
  []
end

#to_hHash<Symbol, String>

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.

Returns:

  • (Hash<Symbol, String>)


68
69
70
# File 'lib/proiel/positional_tag.rb', line 68

def to_h
  @fields
end

#to_sString

Returns the positional tag as a string.

Returns:

  • (String)


48
49
50
51
52
# 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