Class: Eancom::Edifact::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/eancom/edifact/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



8
9
10
11
12
# File 'lib/eancom/edifact/message.rb', line 8

def initialize
  @locations = []
  @items = []
  @hash = {}
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



6
7
8
# File 'lib/eancom/edifact/message.rb', line 6

def hash
  @hash
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/eancom/edifact/message.rb', line 6

def items
  @items
end

#locationsObject

Returns the value of attribute locations.



6
7
8
# File 'lib/eancom/edifact/message.rb', line 6

def locations
  @locations
end

Instance Method Details

#<<(segment) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eancom/edifact/message.rb', line 22

def <<(segment)
  start_location if segment.starts_location?
  start_item if segment.starts_item?

  if @location
    @location << segment
  elsif @item
    @item << segment
  elsif name = group_name(segment)
    @hash[name] = [] if @hash[name].nil?
    @hash[name] << segment.to_json_hash
  else
    @hash.merge!(segment.to_json_hash)
  end
end

#add_item(item) ⇒ Object



18
19
20
# File 'lib/eancom/edifact/message.rb', line 18

def add_item(item)
  @items << item
end

#add_location(location) ⇒ Object



14
15
16
# File 'lib/eancom/edifact/message.rb', line 14

def add_location(location)
  @locations << location
end

#to_json_hashObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eancom/edifact/message.rb', line 38

def to_json_hash
  if @location && !@location.empty?
    add_location(@location.to_json_hash)
  end
  if @item && !@item.empty?
    add_item(@item.to_json_hash)
  end
  message_hash = {}
  message_hash.merge!(@hash)
  message_hash.merge!({ locations: @locations }) if !@locations.empty?
  message_hash.merge!({ items: @items }) if !@items.empty?
  message_hash
end