Class: Fix::Protocol::Field
- Inherits:
-
Object
- Object
- Fix::Protocol::Field
- 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
-
#dump ⇒ String
Returns the field as a message fragment.
-
#errors ⇒ Array
Returns the errors for this field, if any.
-
#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.
-
#initialize(node) ⇒ Field
constructor
A new instance of Field.
-
#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.
-
#raw_value ⇒ String
Returns the string representing this value as it would appear in a FIX message without any kind of type conversion or mapping.
-
#raw_value=(v) ⇒ Object
Assigns a string directly to the field value without type casting or mapping it.
-
#to_type(str) ⇒ Object
Maps a string to an object or a mapped symbol.
-
#value ⇒ Object
Returns the type-casted value of the field, according to its type or mapping definition.
-
#value=(v) ⇒ Object
Assigns a typed value to the field, it is cast according to its type or mapping definition.
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
#dump ⇒ String
Returns the field as a message fragment
26 27 28 |
# File 'lib/fix/protocol/field.rb', line 26 def dump "#{tag}=#{@value}\x01" end |
#errors ⇒ Array
Returns the errors for this field, if any
88 89 90 91 92 |
# File 'lib/fix/protocol/field.rb', line 88 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
101 102 103 104 105 106 107 108 109 |
# File 'lib/fix/protocol/field.rb', line 101 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
37 38 39 40 41 42 43 44 |
# 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" end end |
#raw_value ⇒ String
Returns the string representing this value as it would appear in a FIX message without any kind of type conversion or mapping
70 71 72 |
# File 'lib/fix/protocol/field.rb', line 70 def raw_value @value end |
#raw_value=(v) ⇒ Object
Assigns a string directly to the field value without type casting or mapping it
79 80 81 |
# File 'lib/fix/protocol/field.rb', line 79 def raw_value=(v) @value = v end |
#to_type(str) ⇒ Object
Maps a string to an object or a mapped symbol
117 118 119 120 121 122 123 124 125 |
# File 'lib/fix/protocol/field.rb', line 117 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 ⇒ Object
Returns the type-casted value of the field, according to its type or mapping definition
51 52 53 |
# File 'lib/fix/protocol/field.rb', line 51 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
60 61 62 |
# File 'lib/fix/protocol/field.rb', line 60 def value=(v) @value = from_type(v) end |