Class: Eancom::Edifact::Location

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocation

Returns a new instance of Location.



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

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

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



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

def hash
  @hash
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

Instance Method Details

#<<(segment) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/eancom/edifact/location.rb', line 17

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

  if @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



13
14
15
# File 'lib/eancom/edifact/location.rb', line 13

def add_item(item)
  @items << item
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/eancom/edifact/location.rb', line 38

def empty?
  @hash.empty?
end

#start_itemObject



42
43
44
45
46
47
# File 'lib/eancom/edifact/location.rb', line 42

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

#to_json_hashObject



30
31
32
33
34
35
36
# File 'lib/eancom/edifact/location.rb', line 30

def to_json_hash
  if @item && !@item.empty?
    add_item(@item.to_json_hash)
  end

  @hash.merge({ items: @items })
end