Class: Stupidedi::Reader::SegmentTok

Inherits:
Object
  • Object
show all
Includes:
Inspect
Defined in:
lib/stupidedi/reader/tokens/segment_tok.rb

Instance Attribute Summary collapse

Constructors collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect

Constructor Details

#initialize(id, element_toks, position, remainder) ⇒ SegmentTok

Returns a new instance of SegmentTok.



21
22
23
24
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 21

def initialize(id, element_toks, position, remainder)
  @id, @element_toks, @position, @remainder =
    id, element_toks, position, remainder
end

Instance Attribute Details

#element_toksArray<CompositeElementTok, SimpleElementTok> (readonly)



13
14
15
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 13

def element_toks
  @element_toks
end

#idSymbol (readonly)

Returns:

  • (Symbol)


10
11
12
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 10

def id
  @id
end

#positionPosition (readonly)

Returns:



16
17
18
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 16

def position
  @position
end

#remainderPosition (readonly)

Returns:



19
20
21
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 19

def remainder
  @remainder
end

Class Method Details

.build(id, element_toks, position, remainder) ⇒ SegmentTok

Returns:



66
67
68
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 66

def build(id, element_toks, position, remainder)
  new(id, element_toks, position, remainder)
end

Instance Method Details

#blank?Boolean

:nocov:

Returns:

  • (Boolean)


41
42
43
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 41

def blank?
  @element_toks.all?(&:blank?)
end

#copy(changes = {}) ⇒ SegmentTok

Returns:



27
28
29
30
31
32
33
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 27

def copy(changes = {})
  SegmentTok.new \
    changes.fetch(:id, @id),
    changes.fetch(:element_toks, @element_toks),
    changes.fetch(:position, @position),
    changes.fetch(:remainder, @remainder)
end

#present?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 45

def present?
  not blank?
end

#pretty_print(q)

:nocov:



36
37
38
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 36

def pretty_print(q)
  q.pp(:segment.cons(@id.cons(@element_toks)))
end

#to_x12(separators)



49
50
51
52
53
54
55
56
57
58
# File 'lib/stupidedi/reader/tokens/segment_tok.rb', line 49

def to_x12(separators)
  if blank?
    "#{id}#{separators.segment}"
  else
    es  = @element_toks.map{|x| x.to_x12(separators) }
    sep = separators.element || "*"
    eos = separators.segment || "~"
    id.cons(es).join(sep).gsub(/#{Regexp.escape(sep)}+$/, "") + eos.strip
  end
end