Class: Stupidedi::Schema::LoopDef

Inherits:
AbstractDef show all
Includes:
Inspect
Defined in:
lib/stupidedi/schema/loop_def.rb

Overview

See Also:

  • 2.2.2 Loops
  • B.1.3.12.4 Loops of Data Segments

Instance Attribute Summary collapse

Constructors collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect

Methods inherited from AbstractDef

#component?, #composite?, #definition?, #element?, #functional_group?, #interchange?, #repeated?, #segment?, #simple?, #table?, #transaction_set?, #usage?

Constructor Details

#initialize(id, repeat_count, children, parent) ⇒ LoopDef

Returns a new instance of LoopDef.



29
30
31
32
33
34
35
36
37
38
# File 'lib/stupidedi/schema/loop_def.rb', line 29

def initialize(id, repeat_count, children, parent)
  @id, @repeat_count, @children, @parent =
    id, repeat_count, children, parent

  # Delay re-parenting until the entire definition tree has a root
  # to prevent unnecessarily copying objects
  unless parent.nil?
    @children = @children.map{|x| x.copy(:parent => self) }
  end
end

Instance Attribute Details

#childrenArray<SegmentUse, LoopDef> (readonly)

Returns:



20
21
22
# File 'lib/stupidedi/schema/loop_def.rb', line 20

def children
  @children
end

#idString (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/stupidedi/schema/loop_def.rb', line 14

def id
  @id
end

#parentLoopDef, TableDef (readonly)

Returns:



23
24
25
# File 'lib/stupidedi/schema/loop_def.rb', line 23

def parent
  @parent
end

#repeat_countRepeatCount (readonly)

Returns:



17
18
19
# File 'lib/stupidedi/schema/loop_def.rb', line 17

def repeat_count
  @repeat_count
end

Class Method Details

.build(id, repeat_count, *children) ⇒ LoopDef

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/stupidedi/schema/loop_def.rb', line 139

def build(id, repeat_count, *children)
  # Validate: first child must be a SegmentUse
  if children.empty? || !children.head.is_a?(SegmentUse)
    raise Exceptions::InvalidSchemaError,
      "first child must be a SegmentUse"
  elsif children.head.repeat_count.include?(2)
    raise Exceptions::InvalidSchemaError,
      "first child must have RepeatCount.bounded(1)"
  end

  new(id, repeat_count, children, nil)
end

Instance Method Details

#bounded?Boolean

Returns:

  • (Boolean)

See Also:

  • B.1.1.3.11.1 Loop Control Segments
  • B.1.1.3.12.4 Loops of Data Segments Bounded Loops


85
86
87
88
89
90
91
# File 'lib/stupidedi/schema/loop_def.rb', line 85

def bounded?
  first_seg = @children.find{|x| x.is_a?(SegmentUse) }
  last_seg = @children.reverse.find{|x| x.is_a?(SegmentUse) }
  first_seg && last_seg &&
    first_seg.definition.id == :LS &&
    last_seg.definition.id == :LE
end

#code_listsAbstractSet<CodeList>

Returns:



114
115
116
# File 'lib/stupidedi/schema/loop_def.rb', line 114

def code_lists
  @children.map(&:code_lists).inject(&:|)
end

#copy(changes = {}) ⇒ LoopDef

Returns:



41
42
43
44
45
46
47
# File 'lib/stupidedi/schema/loop_def.rb', line 41

def copy(changes = {})
  LoopDef.new \
    changes.fetch(:id, @id),
    changes.fetch(:repeat_count, @repeat_count),
    changes.fetch(:children, @children),
    changes.fetch(:parent, @parent)
end

#descriptorString

Returns:

  • (String)


75
76
77
# File 'lib/stupidedi/schema/loop_def.rb', line 75

def descriptor
  "loop #{id}"
end

#emptyLoopVal

Returns:

  • (LoopVal)


105
106
107
# File 'lib/stupidedi/schema/loop_def.rb', line 105

def empty
  Values::LoopVal.new(self, [])
end

#entry_segment_useSegmentUse

Returns:



100
101
102
# File 'lib/stupidedi/schema/loop_def.rb', line 100

def entry_segment_use
  @children.find{|x| x.is_a?(SegmentUse) }
end

#header_segment_usesArray<SegmentUse>

Deprecated.

Use #children instead. This method does not include segments that appear between child loops in interleaved structures.

Returns segments before the first LoopDef.

Returns:



53
54
55
# File 'lib/stupidedi/schema/loop_def.rb', line 53

def header_segment_uses
  @children.take_while{|x| x.is_a?(SegmentUse) }
end

#hierarchical?Boolean

Returns:

  • (Boolean)

See Also:

  • 5.6 HL-initiated Loop


94
95
96
97
# File 'lib/stupidedi/schema/loop_def.rb', line 94

def hierarchical?
  first_seg = @children.find{|x| x.is_a?(SegmentUse) }
  first_seg && first_seg.definition.id == :HL
end

#loop?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/stupidedi/schema/loop_def.rb', line 109

def loop?
  true
end

#loop_defsArray<LoopDef>

Deprecated.

Use #children instead and filter with ‘select{|x| x.is_a?(LoopDef)}`.

Returns all LoopDef children.

Returns:



60
61
62
# File 'lib/stupidedi/schema/loop_def.rb', line 60

def loop_defs
  @children.select{|x| x.is_a?(LoopDef) }
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/stupidedi/schema/loop_def.rb', line 119

def pretty_print(q)
  q.text("LoopDef[#{@id}]")
  q.group(2, "(", ")") do
    q.breakable ""
    @children.each do |e|
      unless q.current_group.first?
        q.text ","
        q.breakable
      end
      q.pp e
    end
  end
end

#repeatable?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/stupidedi/schema/loop_def.rb', line 79

def repeatable?
  @repeat_count.try{|r| r.include?(2) }
end

#trailer_segment_usesArray<SegmentUse>

Deprecated.

Use #children instead. This method does not include segments that appear between child loops in interleaved structures.

Returns segments after the last LoopDef.

Returns:



68
69
70
71
72
# File 'lib/stupidedi/schema/loop_def.rb', line 68

def trailer_segment_uses
  last_loop_idx = @children.rindex{|x| x.is_a?(LoopDef) }
  return [] if last_loop_idx.nil?
  @children.drop(last_loop_idx + 1).select{|x| x.is_a?(SegmentUse) }
end