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, #dump_yn_bool, #parse_integer, #parse_timestamp, #parse_yn_bool

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

#can_parse?(str) ⇒ Boolean

Checks whether the start of the given string can be parsed as this particular field

Parameters:

  • str (String)

    The string for which we want to parse the beginning

Returns:

  • (Boolean)

    Whether the beginning of the string can be parsed for this field



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

def can_parse?(str)
  str.match(/^#{tag}\=[^\x01]+\x01/)
end

#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



98
99
100
101
102
# File 'lib/fix/protocol/field.rb', line 98

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



111
112
113
114
115
116
117
118
119
# File 'lib/fix/protocol/field.rb', line 111

def from_type(obj)
  if !obj.nil? && type && !mapping
    send("dump_#{type}", obj)
  elsif !obj.nil? && 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



47
48
49
50
51
52
53
54
# File 'lib/fix/protocol/field.rb', line 47

def parse(str)
  if str.match(/^#{tag}\=([^\x01]+)\x01/)
    @value = $1
    str.gsub(/^[^\x01]+\x01/, '')
  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



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

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



89
90
91
# File 'lib/fix/protocol/field.rb', line 89

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



127
128
129
130
131
132
133
134
135
# File 'lib/fix/protocol/field.rb', line 127

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



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

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



70
71
72
# File 'lib/fix/protocol/field.rb', line 70

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