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
-
#can_parse?(str) ⇒ Boolean
Checks whether the start of the given string can be parsed as this particular field.
-
#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, #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
36 37 38 |
# File 'lib/fix/protocol/field.rb', line 36 def can_parse?(str) str.match(/^#{tag}\=[^\x01]+\x01/) end |
#dump ⇒ String
Returns the field as a message fragment
26 27 28 |
# File 'lib/fix/protocol/field.rb', line 26 def dump @value && "#{tag}=#{@value}\x01" end |
#errors ⇒ Array
Returns the errors for this field, if any
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
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
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_value ⇒ String
Returns the string representing this value as it would appear in a FIX message without any kind of type conversion or mapping
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
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
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 |
#value ⇒ Object
Returns the type-casted value of the field, according to its type or mapping definition
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
70 71 72 |
# File 'lib/fix/protocol/field.rb', line 70 def value=(v) @value = from_type(v) end |