Class: PROIEL::PositionalTag Abstract
- Inherits:
-
Object
- Object
- PROIEL::PositionalTag
- Includes:
- Comparable
- Defined in:
- lib/proiel/positional_tag.rb
Overview
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
-
#<=>(o) ⇒ Integer
Returns an integer, -1, 0 or 1, suitable for sorting the tag.
-
#[](field) ⇒ String
Returns the value of a field.
-
#[]=(field, value) ⇒ String
Assigns a value to a field.
-
#empty? ⇒ true, false
Checks if the tag is unitialized.
-
#fields ⇒ Array<Symbol>
Returns the field names.
-
#initialize(value = nil) ⇒ PositionalTag
constructor
Creates a new positional tag.
-
#to_h ⇒ Hash<Symbol, String>
Returns a hash representation of the tag.
-
#to_s ⇒ String
Returns the positional tag as a string.
Constructor Details
#initialize(value = nil) ⇒ PositionalTag
Creates a new positional tag.
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.
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 ‘-`.
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 `-`.
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.
59 60 61 |
# File 'lib/proiel/positional_tag.rb', line 59 def empty? @fields.empty? end |
#fields ⇒ Array<Symbol>
Returns the field names. This method should be overridden by implementations. The names should be returned as an array of symbols.
114 115 116 |
# File 'lib/proiel/positional_tag.rb', line 114 def fields [] end |
#to_h ⇒ Hash<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.
68 69 70 |
# File 'lib/proiel/positional_tag.rb', line 68 def to_h @fields end |
#to_s ⇒ String
Returns the positional tag as a 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 |