Module: Stupidedi::Guides::FiftyTen::GuideBuilder

Defined in:
lib/stupidedi/guides/005010/guide_builder.rb

Overview

GuideBuilder is a simple DSL for construction an implementation guide.

Element Constraints collapse

Definition Constructors collapse

Class Method Summary collapse

Class Method Details

.build(transaction_set_def, *table_defs) ⇒ Schema::TransactionSetDef


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stupidedi/guides/005010/guide_builder.rb', line 17

def build(transaction_set_def, *table_defs)
  table_defs.each do |t|
    next unless t.repeatable?

    search = [t.header_segment_uses, t.loop_defs, t.trailer_segment_uses]
    search.each do |s|
      opt, req = s.split_until(&:optional?)
      prefix   = opt.concat(req.take(1))

      if x = prefix.find(&:repeatable?)
        raise Exceptions::InvalidSchemaError,
          "#{x.id} is an entry point into #{t.id}. Both are marked " +
          "repeatable, but only one should be repeatable to avoid "  +
          "generating an ambiguous grammar. You probably want to "   +
          "change the repeat count of #{x.id}"
      end

      break if req.present?
    end
  end

  transaction_set_def.copy(:table_defs => table_defs)
end

.Element(requirement, name, *constraints)


67
68
69
# File 'lib/stupidedi/guides/005010/guide_builder.rb', line 67

def Element(requirement, name, *constraints)
  [:Element, requirement, name, constraints]
end

.MaxLength(n)


50
51
52
# File 'lib/stupidedi/guides/005010/guide_builder.rb', line 50

def MaxLength(n)
  [:MaxLength, n]
end

.MaxPrecision(n)


55
56
57
# File 'lib/stupidedi/guides/005010/guide_builder.rb', line 55

def MaxPrecision(n)
  [:MaxPrecision, n]
end

.Segment(position, segment_def, name, requirement, repeat_count, *elements) ⇒ SegmentUse


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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/stupidedi/guides/005010/guide_builder.rb', line 72

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!

    element_index = 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

        # ComponentElementUses
        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!

          component_index = 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

.Values(*values)


45
46
47
# File 'lib/stupidedi/guides/005010/guide_builder.rb', line 45

def Values(*values)
  [:Values, values]
end