Module: Stupidedi::TransactionSets::Builder

Defined in:
lib/stupidedi/transaction_sets/builder.rb,
lib/stupidedi/transaction_sets/builder/dsl.rb

Overview

Builder is a simple DSL for construction a transaction set

Defined Under Namespace

Classes: Dsl

Element Constraints collapse

Definition Constructors collapse

Class Method Summary collapse

Class Method Details

.build(functional_group, id, name, *table_defs) ⇒ Schema::TransactionSetDef


18
19
20
# File 'lib/stupidedi/transaction_sets/builder.rb', line 18

def build(functional_group, id, name, *table_defs)
  Schema::TransactionSetDef.build(functional_group, id, name, *table_defs)
end

.Element(requirement, name, *constraints)

Parameters:


48
49
50
# File 'lib/stupidedi/transaction_sets/builder.rb', line 48

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

.MaxLength(n)

Parameters:

  • n (Integer)

31
32
33
# File 'lib/stupidedi/transaction_sets/builder.rb', line 31

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

.MaxPrecision(n)

Parameters:

  • n (Integer)

36
37
38
# File 'lib/stupidedi/transaction_sets/builder.rb', line 36

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

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

Returns:

  • (SegmentUse)

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
118
119
120
121
122
# File 'lib/stupidedi/transaction_sets/builder.rb', line 53

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 given"
  end

  element_idx   = "00"
  element_uses  = elements.zip(segment_def.element_uses).map do |e, u|
    e_tag, e_requirement, e_name, e_arguments = e
    element_idx = element_idx.succ
    descriptor  = "#{segment_def.id}#{element_idx}"

    unless e_tag == :Element
      raise Exceptions::InvalidSchemaError,
        "given argument for #{descriptor} 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 for #{descriptor}"
      end

      unless e_requirement.forbidden?
        unless e_arguments.length == u.definition.component_uses.length
          raise Exceptions::InvalidSchemaError,
            "composite element #{u.definition.id} at #{descriptor} has
            #{u.definition.component_uses.length} component elements but
            #{e_arguments.length} arguments were given".join
        end

        # ComponentElementUses
        component_idx   = "00"
        component_uses  = e_arguments.zip(u.definition.component_uses).map do |e1, c|
          c_tag, c_requirement, c_name, c_arguments = e1
          component_idx = component_idx.succ
          descriptor    = "#{descriptor}-#{component_idx}"

          unless c_tag == :Element
            raise Exceptions::InvalidSchemaError,
              "given argument for #{descriptor} must be Element(...)"
          end

          mod_element(c, descriptor, 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, descriptor, e_requirement, e_name, e_arguments)
    end
  end

  segment_def.
    copy(:name => name, :element_uses => element_uses).
    use(position, requirement, repeat_count)
end

.Values(*values)

Parameters:

  • values (String, ...)

26
27
28
# File 'lib/stupidedi/transaction_sets/builder.rb', line 26

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