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

#delegations, #name, #parse_failure

Instance Method Summary collapse

Methods inherited from MessagePart

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

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

#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


54
55
56
# File 'lib/fix/protocol/repeating_message_part.rb', line 54

def can_parse?(str)
  str =~ /^#{counter_tag}\=[^\x01]+\x01/
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


61
62
63
64
65
66
67
68
# File 'lib/fix/protocol/repeating_message_part.rb', line 61

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/, ''))
  end
end