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

Constructor Details

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

Returns a new instance of SegmentDef.



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

def initialize(id, name, purpose, element_uses, syntax_notes, parent)
  @id, @name, @purpose, @element_uses, @syntax_notes, @parent =
    id, name, purpose.join, 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)



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

def element_uses
  @element_uses
end

#idSymbol (readonly)

Returns:

  • (Symbol)


13
14
15
# File 'lib/stupidedi/schema/segment_def.rb', line 13

def id
  @id
end

#nameString (readonly)

Returns:

  • (String)


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

def name
  @name
end

#parentSegmentUse (readonly)

Returns:



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

def parent
  @parent
end

#purposeString (readonly)

Returns:

  • (String)


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

def purpose
  @purpose
end

#syntax_notesArray<SyntaxNote> (readonly)

Returns:



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

def syntax_notes
  @syntax_notes
end

Class Method Details

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

Returns:



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

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>

Returns:



76
77
78
# File 'lib/stupidedi/schema/segment_def.rb', line 76

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

#copy(changes = {}) ⇒ SegmentDef

Returns:



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

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

#descriptorString

Returns:

  • (String)


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

def descriptor
  "segment #{id} #{name}"
end

#empty(usage) ⇒ Values::SegmentVal

Returns:



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

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

#pretty_print(q)

This method returns an undefined value.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stupidedi/schema/segment_def.rb', line 81

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

Returns:

  • (Boolean)


71
72
73
# File 'lib/stupidedi/schema/segment_def.rb', line 71

def segment?
  true
end

#use(position, requirement, repeat_count) ⇒ SegmentUse

Returns:



58
59
60
# File 'lib/stupidedi/schema/segment_def.rb', line 58

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

#value(element_vals, usage, position)



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

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