Class: Fix::Protocol::RepeatingMessagePart

Inherits:
MessagePart
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/fix/protocol/repeating_message_part.rb

Overview

Represents a portion of a FIX message consisting of a similar repeating structure preceded by a counter element

Instance Attribute Summary collapse

Attributes inherited from MessagePart

#name, #parse_failure

Instance Method Summary collapse

Methods inherited from MessagePart

collection, #errors, field, inherited, #initialize_node, #node_for_name, #nodes, parse, part, structure

Constructor Details

#initialize(opts = {}) ⇒ RepeatingMessagePart

Returns a new instance of RepeatingMessagePart.



20
21
22
23
24
# File 'lib/fix/protocol/repeating_message_part.rb', line 20

def initialize(opts = {})
  @counter_tag    = opts[:counter_tag]
  @element_klass  = opts[:klass]
  super
end

Instance Attribute Details

#counter_tagObject

Returns the value of attribute counter_tag.



18
19
20
# File 'lib/fix/protocol/repeating_message_part.rb', line 18

def counter_tag
  @counter_tag
end

#element_klassObject

Returns the value of attribute element_klass.



18
19
20
# File 'lib/fix/protocol/repeating_message_part.rb', line 18

def element_klass
  @element_klass
end

Instance Method Details

#buildObject

Appends a new blank node as the last element of the collection



29
30
31
32
33
34
35
36
37
# File 'lib/fix/protocol/repeating_message_part.rb', line 29

def build
  nodes << element_klass.new

  if block_given?
    yield(nodes.last)
  end

  nodes.last
end

#dumpString

Dumps the message fragment as the set of dumped elements preceded by the relevant counter tag

Returns:

  • (String)

    The part of the initial string that didn’t get consumed during the parsing



44
45
46
# File 'lib/fix/protocol/repeating_message_part.rb', line 44

def dump
  "#{counter_tag}=#{length}\x01#{super}"
end

#parse(str) ⇒ Object

Parses an arbitrary number of nodes according to the found counter tag



51
52
53
54
55
56
57
58
59
60
# File 'lib/fix/protocol/repeating_message_part.rb', line 51

def parse(str)
  if str.match(/^#{counter_tag}\=([^\x01]+)\x01/)
    len = $1.to_i 
    @nodes = []
    len.times { @nodes << element_klass.new }
    super(str.gsub(/^#{counter_tag}\=[^\x01]+\x01/, ''))
  else
    self.parse_failure = "Expected <#{str}> to begin with <#{counter_tag}=...|>"
  end
end