Class: Stupidedi::Schema::TableDef

Inherits:
AbstractDef show all
Defined in:
lib/stupidedi/schema/table_def.rb

Instance Attribute Summary collapse

Constructors collapse

Instance Method Summary collapse

Methods inherited from AbstractDef

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

Constructor Details

#initialize(id, position, children, parent) ⇒ TableDef

Returns a new instance of TableDef.



19
20
21
22
23
24
25
26
27
28
# File 'lib/stupidedi/schema/table_def.rb', line 19

def initialize(id, position, children, parent)
  @id, @position, @children, @parent =
    id, position, 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:



11
12
13
# File 'lib/stupidedi/schema/table_def.rb', line 11

def children
  @children
end

#idString (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/stupidedi/schema/table_def.rb', line 8

def id
  @id
end

#parentTransactionSetDef (readonly)

Returns:



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

def parent
  @parent
end

#positionInteger (readonly)

Returns:

  • (Integer)


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

def position
  @position
end

Class Method Details

.detail(id, *children) ⇒ TableDef

Returns:



155
156
157
158
159
160
161
162
# File 'lib/stupidedi/schema/table_def.rb', line 155

def detail(id, *children)
  unless id.is_a?(String)
    raise Exceptions::InvalidSchemaError,
      "first argument to TableDef.detail must be a String but got #{id.inspect}"
  end

  new(id, 2, children, nil)
end

.header(id, *children) ⇒ TableDef

Returns:



145
146
147
148
149
150
151
152
# File 'lib/stupidedi/schema/table_def.rb', line 145

def header(id, *children)
  unless id.is_a?(String)
    raise Exceptions::InvalidSchemaError,
      "first argument to TableDef.header must be a String but got #{id.inspect}"
  end

  new(id, 1, children, nil)
end

.summary(id, *children) ⇒ TableDef

Returns:



165
166
167
168
169
170
171
172
# File 'lib/stupidedi/schema/table_def.rb', line 165

def summary(id, *children)
  unless id.is_a?(String)
    raise Exceptions::InvalidSchemaError,
      "first argument to TableDef.summary must be a String but got #{id.inspect}"
  end

  new(id, 3, children, nil)
end

Instance Method Details

#code_listsAbstractSet<CodeList>

Returns:



120
121
122
# File 'lib/stupidedi/schema/table_def.rb', line 120

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

#copy(changes = {}) ⇒ TableDef

Returns:



31
32
33
34
35
36
37
# File 'lib/stupidedi/schema/table_def.rb', line 31

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

#descriptorString

Returns:

  • (String)


65
66
67
# File 'lib/stupidedi/schema/table_def.rb', line 65

def descriptor
  "table #{id}"
end

#emptyValues::TableVal

Returns:



111
112
113
# File 'lib/stupidedi/schema/table_def.rb', line 111

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

#entry_segment_usesArray<SegmentUse>

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/stupidedi/schema/table_def.rb', line 88

def entry_segment_uses
  uses = @children.map do |child|
    if child.is_a?(SegmentUse)
      child
    else
      child.entry_segment_use
    end
  end

  # Up to and including the first required segment
  suffix = uses.drop_while(&:optional?)

  if suffix.present?
    # Some segment(s) is required, so table can't start without it
    position = suffix.map(&:position).min
    uses.take_while{|u| u.position <= position }
  else
    # Nothing is required, table can begin at any of these
    uses
  end
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:



43
44
45
# File 'lib/stupidedi/schema/table_def.rb', line 43

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

#loop_defsArray<LoopDef>

Deprecated.

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

Returns all LoopDef children.

Returns:



50
51
52
# File 'lib/stupidedi/schema/table_def.rb', line 50

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

#pretty_print(q) ⇒ void

This method returns an undefined value.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/stupidedi/schema/table_def.rb', line 125

def pretty_print(q)
  q.text("TableDef[#{@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

#repeat_countObject



75
76
77
78
79
80
81
# File 'lib/stupidedi/schema/table_def.rb', line 75

def repeat_count
  if repeatable?
    RepeatCount.unbounded
  else
    RepeatCount.bounded(1)
  end
end

#repeatable?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/stupidedi/schema/table_def.rb', line 69

def repeatable?
  header_segment_uses.empty? and
    loop_defs.present?       and
    loop_defs.head.repeatable?
end

#required?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/stupidedi/schema/table_def.rb', line 83

def required?
  entry_segment_uses.any?(&:required?)
end

#table?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/stupidedi/schema/table_def.rb', line 115

def table?
  true
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:



58
59
60
61
62
# File 'lib/stupidedi/schema/table_def.rb', line 58

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