class Fix::Protocol::Field

A FIX message field

Public Class Methods

new(node) click to toggle source
# 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

Public Instance Methods

can_parse?(str) click to toggle source

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

@param str [String] The string for which we want to parse the beginning @return [Boolean] Whether the beginning of the string can be parsed for this field

# File lib/fix/protocol/field.rb, line 36
def can_parse?(str)
  str.match(/^#{tag}\=[^\x01]+\x01/)
end
dump() click to toggle source

Returns the field as a message fragment

@return [String] A message fragment terminated by the separator byte

# File lib/fix/protocol/field.rb, line 26
def dump
  @value && "#{tag}=#{@value}\x01"
end
errors() click to toggle source

Returns the errors for this field, if any

@return [Array] The errors for this field

# File lib/fix/protocol/field.rb, line 98
def errors
  if required && !@value
    "Missing value for <#{name}> field"
  end
end
from_type(obj) click to toggle source

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

@param obj [Object] The mapping key or object to convert @return [String] The FIX field value

# 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) click to toggle source

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

@param str [String] A string starting with the field to parse @return [String] The same string with the field stripped off

# 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_value() click to toggle source

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

@return [String] The raw field value

# File lib/fix/protocol/field.rb, line 80
def raw_value
  @value
end
raw_value=(v) click to toggle source

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

@param v [String] The string value to assign

# File lib/fix/protocol/field.rb, line 89
def raw_value=(v)
  @value = v
end
to_type(str) click to toggle source

Maps a string to an object or a mapped symbol

@param str [String] The string to cast or map @return [Object] An object of the defined type or a mapped symbol

# 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
value() click to toggle source

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

@return [Object] The type-casted value

# File lib/fix/protocol/field.rb, line 61
def value
  to_type(@value)
end
value=(v) click to toggle source

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

@param v [Object] An object of the defined type for this field

# File lib/fix/protocol/field.rb, line 70
def value=(v)
  @value = from_type(v)
end