Class: Stupidedi::Schema::SegmentDef

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

Overview

See Also:

  • B.1.1.3.4 Data Segment

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?, #loop?, #repeated?, #simple?, #table?, #transaction_set?, #usage?

Constructor Details

#initialize(id, name, purpose, element_uses, syntax_notes, parent) ⇒ SegmentDef


32
33
34
35
36
37
38
39
40
41
# File 'lib/stupidedi/schema/segment_def.rb', line 32

def initialize(id, name, purpose, element_uses, syntax_notes, parent)
  @id, @name, @purpose, @element_uses, @syntax_notes, @parent =
    id, name, purpose, element_uses, syntax_notes, parent

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

Instance Attribute Details

#element_usesArray<SimpleElementUse, CompositeElementUse> (readonly)


24
25
26
# File 'lib/stupidedi/schema/segment_def.rb', line 24

def element_uses
  @element_uses
end

#idSymbol (readonly)


15
16
17
# File 'lib/stupidedi/schema/segment_def.rb', line 15

def id
  @id
end

#nameString (readonly)


18
19
20
# File 'lib/stupidedi/schema/segment_def.rb', line 18

def name
  @name
end

#parentSegmentUse (readonly)


30
31
32
# File 'lib/stupidedi/schema/segment_def.rb', line 30

def parent
  @parent
end

#purposeString (readonly)


21
22
23
# File 'lib/stupidedi/schema/segment_def.rb', line 21

def purpose
  @purpose
end

#syntax_notesArray<SyntaxNote> (readonly)


27
28
29
# File 'lib/stupidedi/schema/segment_def.rb', line 27

def syntax_notes
  @syntax_notes
end

Class Method Details

.build(id, name, purpose, *args) ⇒ SegmentDef


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/stupidedi/schema/segment_def.rb', line 99

def build(id, name, purpose, *args)
  element_uses = args.take_while{|x| x.is_a?(AbstractElementUse) }
  syntax_notes = args.drop(element_uses.length)

  # @todo: Validate SyntaxNotes (beyond this rudimentary validation)
  syntax_notes.each do |sn|
    unless sn.indexes.max - 1 <= element_uses.length
      raise Exceptions::InvalidSchemaError,
        "Syntax note for #{id} (#{element_uses.length} elements) " +
        "refers to non-existent element #{sn.indexes.max}"
    end
  end

  new(id, name, purpose, element_uses, syntax_notes, nil)
end

Instance Method Details

#code_listsAbstractSet<CodeList>


73
74
75
# File 'lib/stupidedi/schema/segment_def.rb', line 73

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

#copy(changes = {}) ⇒ SegmentDef


44
45
46
47
48
49
50
51
52
# File 'lib/stupidedi/schema/segment_def.rb', line 44

def copy(changes = {})
  SegmentDef.new \
    changes.fetch(:id, @id),
    changes.fetch(:name, @name),
    changes.fetch(:purpose, @purpose),
    changes.fetch(:element_uses, @element_uses),
    changes.fetch(:syntax_notes, @syntax_notes),
    changes.fetch(:parent, @parent)
end

#empty(usage) ⇒ Values::SegmentVal


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

def empty(usage)
  Values::SegmentVal.new([], usage)
end

#pretty_print(q)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/stupidedi/schema/segment_def.rb', line 78

def pretty_print(q)
  q.text "SegmentDef[#{@id}]"

  q.group(2, "(", ")") do
    q.breakable ""
    @element_uses.each do |e|
      unless q.current_group.first?
        q.text ","
        q.breakable
      end
      q.pp e
    end
  end
end

#segment?Boolean


68
69
70
# File 'lib/stupidedi/schema/segment_def.rb', line 68

def segment?
  true
end

#use(position, requirement, repeat_count) ⇒ SegmentUse


55
56
57
# File 'lib/stupidedi/schema/segment_def.rb', line 55

def use(position, requirement, repeat_count)
  SegmentUse.new(self, position, requirement, repeat_count, nil)
end

#value(element_vals, usage, position)


64
65
66
# File 'lib/stupidedi/schema/segment_def.rb', line 64

def value(element_vals, usage, position)
  Values::SegmentVal.new(element_vals, usage, position)
end