49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/stupidedi/guides/004010/guide_builder.rb', line 49
def Segment(position, segment_def, name, requirement, repeat_count, *elements)
unless elements.length == segment_def.element_uses.length
raise Exceptions::InvalidSchemaError,
"segment #{segment_def.id} has #{segment_def.element_uses.length}" <<
" elements but #{elements.length} arguments were specified"
end
element_index = "00"
element_uses = elements.zip(segment_def.element_uses).map do |e, u|
e_tag, e_requirement, e_name, e_arguments = e
element_index.succ!
unless e_tag == :Element
raise Exceptions::InvalidSchemaError,
"given argument for #{segment_def.id}#{element_index} must be Element(...)"
end
if u.composite?
e_repeat_count, e_arguments = e_arguments.partition{|x| x.is_a?(Schema::RepeatCount) }
changes = Hash.new
changes[:requirement] = e_requirement
if e_repeat_count.length == 1
changes[:repeat_count] = e_repeat_count.head
elsif e_repeat_count.length > 1
raise Exceptions::InvalidSchemaError,
"more than one RepeatCount was specified"
end
unless e_requirement.forbidden?
unless e_arguments.length == u.definition.component_uses.length
raise Exceptions::InvalidSchemaError,
"composite element #{u.definition.id} at #{segment_def.id}" <<
"#{element_index} has #{u.definition.component_uses.length}" <<
" component elements but #{e_arguments.length} were given"
end
component_index = "00"
component_uses = e_arguments.zip(u.definition.component_uses).map do |e, c|
c_tag, c_requirement, c_name, c_arguments = e
component_index.succ!
unless c_tag == :Element
raise Exceptions::InvalidSchemaError,
"given argument for #{segment_def.id}#{element_index}" <<
"-#{component_index} must be Element(...)"
end
mod_element(c, c_requirement, c_name, c_arguments)
end
changes[:definition] = u.definition.copy(:name => e_name,
:component_uses => component_uses)
else
changes[:definition] = u.definition.copy(:name => e_name)
end
u.copy(changes)
else
mod_element(u, e_requirement, e_name, e_arguments)
end
end
segment_def.copy(:name => name).
copy(:element_uses => element_uses).
use(position, requirement, repeat_count)
end
|