Class: Fix::Protocol::Field

Inherits:
Object
  • Object
show all
Includes:
TypeConversions
Defined in:
lib/fix/protocol/field.rb

Overview

A FIX message field

Constant Summary collapse

@@attrs =
[:tag, :name, :default, :type, :required, :parse_failure, :mapping]

Instance Method Summary collapse

Methods included from TypeConversions

#dump_integer, #dump_timestamp, #parse_integer, #parse_timestamp

Constructor Details

#initialize(node) ⇒ Field

Returns a new instance of Field.



16
17
18
19
# File 'lib/fix/protocol/field.rb', line 16

def initialize(node)
  @@attrs.each { |attr| node[attr] && send("#{attr}=", node[attr]) }
  self.value ||= (default.is_a?(Proc) ? default.call(self) : default)
end

Instance Method Details

#dumpString

Returns the field as a message fragment

Returns:

  • (String)

    A message fragment terminated by the separator byte



26
27
28
# File 'lib/fix/protocol/field.rb', line 26

def dump
  @value && "#{tag}=#{@value}\x01"
end

#errorsArray

Returns the errors for this field, if any

Returns:

  • (Array)

    The errors for this field



90
91
92
93
94
# File 'lib/fix/protocol/field.rb', line 90

def errors
  if required && !@value
    "Missing value for <#{name}> field"
  end
end

#from_type(obj) ⇒ String

Performs the actual mapping or type casting by converting an object to a string or a symbol to a mapped string

Parameters:

  • obj (Object)

    The mapping key or object to convert

Returns:

  • (String)

    The FIX field value



103
104
105
106
107
108
109
110
111
# File 'lib/fix/protocol/field.rb', line 103

def from_type(obj)
  if obj && type && !mapping
    send("dump_#{type}", obj)
  elsif obj && mapping && mapping.has_key?(obj)
    mapping[obj]
  else
    obj
  end
end

#parse(str) ⇒ String

Parses the value for this field from the beginning of the string passed as parameter and return the rest of the string. The field value is assigned to the @value instance variable

Parameters:

  • str (String)

    A string starting with the field to parse

Returns:

  • (String)

    The same string with the field stripped off



37
38
39
40
41
42
43
44
45
46
# File 'lib/fix/protocol/field.rb', line 37

def parse(str)
  if str.match(/^#{tag}\=([^\x01]+)\x01/)
    @value = $1
    str.gsub(/^[^\x01]+\x01/, '')
  elsif required
    self.parse_failure = "Expected <#{str}> to start with a <#{tag}=...|> required field"  
  else
    str
  end
end

#raw_valueString

Returns the string representing this value as it would appear in a FIX message without any kind of type conversion or mapping

Returns:

  • (String)

    The raw field value



72
73
74
# File 'lib/fix/protocol/field.rb', line 72

def raw_value
  @value
end

#raw_value=(v) ⇒ Object

Assigns a string directly to the field value without type casting or mapping it

Parameters:

  • v (String)

    The string value to assign



81
82
83
# File 'lib/fix/protocol/field.rb', line 81

def raw_value=(v)
  @value = v
end

#to_type(str) ⇒ Object

Maps a string to an object or a mapped symbol

Parameters:

  • str (String)

    The string to cast or map

Returns:

  • (Object)

     An object of the defined type or a mapped symbol



119
120
121
122
123
124
125
126
127
# File 'lib/fix/protocol/field.rb', line 119

def to_type(str)
  if str && type && !mapping
    send("parse_#{type}", str)
  elsif str && mapping && mapping.values.map(&:to_s).include?(str)
    mapping.find { |k,v| v.to_s == str.to_s }[0]
  else
    str
  end
end

#valueObject

Returns the type-casted value of the field, according to its type or mapping definition

Returns:

  • (Object)

    The type-casted value



53
54
55
# File 'lib/fix/protocol/field.rb', line 53

def value
  to_type(@value)
end

#value=(v) ⇒ Object

Assigns a typed value to the field, it is cast according to its type or mapping definition

Parameters:

  • v (Object)

    An object of the defined type for this field



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

def value=(v)
  @value = from_type(v)
end