Class: GFA::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/gfa/field.rb

Direct Known Subclasses

Char, Float, Hex, Json, NumArray, SigInt, String

Defined Under Namespace

Classes: Char, Float, Hex, Json, NumArray, SigInt, String

Constant Summary collapse

CODES =

Class-level

{
  A: :Char,
  i: :SigInt,
  f: :Float,
  Z: :String,
  J: :Json, # Excluding new-line and tab characters
  H: :Hex,
  B: :NumArray
}
TYPES =
CODES.values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueObject (readonly)

Instance-level



36
37
38
# File 'lib/gfa/field.rb', line 36

def value
  @value
end

Class Method Details

.[](string) ⇒ Object



29
30
31
32
# File 'lib/gfa/field.rb', line 29

def self.[](string)
  code, value = string.split(':', 2)
  code_class(code).new(value)
end

.code_class(code) ⇒ Object



19
20
21
22
23
# File 'lib/gfa/field.rb', line 19

def self.code_class(code)
  name = CODES[code.to_sym]
  raise "Unknown field type: #{code}." if name.nil?
  name_class(name)
end

.name_class(name) ⇒ Object



25
26
27
# File 'lib/gfa/field.rb', line 25

def self.name_class(name)
  const_get(name)
end

Instance Method Details

#!~(field) ⇒ Object

Non-equivalent to field, same as !equivalent?



91
92
93
# File 'lib/gfa/field.rb', line 91

def !~(field)
  !self.~(field)
end

#==(field) ⇒ Object

Same as eql?



116
117
118
# File 'lib/gfa/field.rb', line 116

def ==(field)
  eql?(field)
end

#codeObject



42
43
44
# File 'lib/gfa/field.rb', line 42

def code
  self.class::CODE
end

#eql?(field) ⇒ Boolean

Evaluate equality. Note that fields with equivalent values evaluate as different. For example, the following fields have equivalent information, but they all evaluate as different: Z:123, i:123, f:123.0, B:i,123, H:7b. To test equivalency of contents instead, use equivalent?

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/gfa/field.rb', line 106

def eql?(field)
  if field.is_a?(GFA::Field)
    type == field.type && value == field.value
  else
    field.is_a?(value.class) && value == field
  end
end

#equivalent?(field) ⇒ Boolean

Evaluate equivalency of contents. All the following fields are distinct but contain the same information, and are therefore considered equivalent: Z:123, i:123, f:123.0, B:i,123, H:7b

Note that the information content is determined by the class of the first operator. For example:

  • ‘i:123’ ~ ‘f:123.4’ is true because values are compared as integers

  • ‘f:123.4’ ~ ‘i:123’ if false because values are compared as floats

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gfa/field.rb', line 75

def equivalent?(field)
  return true if eql?(field) # Might be faster, so testing this first

  if field.respond_to?(native_fun)
    if field.is_a?(GFA::Field) && native_fun == :to_s
      field.to_s(false) == to_native
    else
      field.send(native_fun) == to_native
    end
  else
    field == value
  end
end

#hashObject



62
63
64
# File 'lib/gfa/field.rb', line 62

def hash
  value.hash
end

#native_funObject



50
51
52
# File 'lib/gfa/field.rb', line 50

def native_fun
  self.class::NATIVE_FUN
end

#regexObject



46
47
48
# File 'lib/gfa/field.rb', line 46

def regex
  self.class::REGEX
end

#to_nativeObject



54
55
56
# File 'lib/gfa/field.rb', line 54

def to_native
  native_fun == :to_s ? to_s(false) : send(native_fun)
end

#to_s(with_type = true) ⇒ Object



58
59
60
# File 'lib/gfa/field.rb', line 58

def to_s(with_type = true)
  "#{"#{code}:" if with_type}#{value}"
end

#typeObject



38
39
40
# File 'lib/gfa/field.rb', line 38

def type
  CODES[code]
end

#~(field) ⇒ Object

Same as equivalent?



97
98
99
# File 'lib/gfa/field.rb', line 97

def ~(field)
  equivalent?(field)
end