Class: Eancom::Edifact::Body

Inherits:
DocumentElement show all
Defined in:
lib/eancom/edifact/body.rb

Instance Attribute Summary collapse

Attributes inherited from DocumentElement

#segments

Instance Method Summary collapse

Methods inherited from DocumentElement

#segment, #to_s

Constructor Details

#initializeBody

Returns a new instance of Body.



7
8
9
10
11
# File 'lib/eancom/edifact/body.rb', line 7

def initialize
  super
  @hash = {}
  @messages = []
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



5
6
7
# File 'lib/eancom/edifact/body.rb', line 5

def hash
  @hash
end

#itemsObject

Returns the value of attribute items.



5
6
7
# File 'lib/eancom/edifact/body.rb', line 5

def items
  @items
end

#messagesObject

Returns the value of attribute messages.



5
6
7
# File 'lib/eancom/edifact/body.rb', line 5

def messages
  @messages
end

Instance Method Details

#start_item(segment) ⇒ Object



40
41
42
43
44
45
# File 'lib/eancom/edifact/body.rb', line 40

def start_item(segment)
  if @item && !@item.empty?
    @message.add_item(@item.to_json_hash)
  end
  @item = Eancom::Edifact::Item.new()
end

#start_message(segment) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/eancom/edifact/body.rb', line 29

def start_message(segment)
  if @message
    @messages << @message.to_json_hash
    if @item && !@item.empty?
      @message.add_item(@item.to_json_hash)
    end
    @item = nil
  end
  @message = Eancom::Edifact::Message.new()
end

#to_json_hashObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/eancom/edifact/body.rb', line 13

def to_json_hash
  @segments.each do |segment|
    start_message(segment) if segment.starts_message?
    start_item(segment) if segment.starts_item?
    if @item
      @item << segment
    else
      @message << segment
    end
  end
  @message.add_item(@item.to_json_hash)
  @messages << @message.to_json_hash
  hash.merge!({ messages: messages })
  { body: hash }
end