Class: EDI::Diagram::Diagram

Inherits:
Object
  • Object
show all
Defined in:
lib/edi4r/diagrams.rb

Overview

Diagram: A structure class to represent a message diagram (branching diagram)

A Diagram is essentially

  • a Branch object (at top-level)

  • a description text

  • a node dictionary (a hash that allows index-like access)

In contrast to a simple Branch, all nodes of a Diagram object are indexed (counting from 1) according to their natural sequence in the EDIFACT branching diagram. Thus, access by index is available for Diagram objects, but not for Branch objects.

Constant Summary collapse

@@cache =
{}
@@caching =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(std, par) ⇒ Diagram

:nodoc:


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/edi4r/diagrams.rb', line 133

def initialize( std, par ) # :nodoc:
  case std
  when 'E' # UN/EDIFACT
    @base_key = [par[:d0065], # msg type
      par[:d0052], # version
      par[:d0054], # release, 
      par[:d0051], # resp. agency
      '',
      #                   par[:d0057],  # assoc. assigned code (subset)
      ''].join(':')
    @msg_type = par[:d0065]

    @dir = EDI::Dir::Directory.create(std,
                                      :d0065 => @msg_type,
                                      :d0052 => par[:d0052], 
                                      :d0054 => par[:d0054], 
                                      :d0051 => par[:d0051], 
                                      :d0057 => par[:d0057], 
                                      :is_iedi => par[:is_iedi])
  when 'I' # SAP IDocs
    @base_key = [par[:IDOCTYPE],
      par[:EXTENSION],
      par[:SAPTYPE],
      '',
      '',
      #                   par[:d0057],  # assoc. assigned code (subset)
      ''].join(':')
    @msg_type = par[:IDOCTYPE]

    @dir = EDI::Dir::Directory.create(std,
                                      :DOCTYPE => @msg_type,
                                      :EXTENSION => par[:EXTENSION], 
                                      :SAPTYPE => par[:SAPTYPE])
    #      raise "Not implemented yet!"
  else
    raise "Unsupported syntax standard: #{std}"
  end

  top_branch = Branch.new( @base_key, nil, self )
  raise "No branch found for key '#{@base_key}'" unless top_branch

  @diag = top_branch.expand
  @desc = @diag.desc

  i = 0; @node_dict = {}
  @diag.each { |node| i+=1; node.index=i; @node_dict[i]=node }
end

Class Method Details

.caching?Boolean

Tells if caching is currently activated (returns a boolean)

Returns:

  • (Boolean)

84
85
86
# File 'lib/edi4r/diagrams.rb', line 84

def Diagram.caching?
  @@caching
end

.caching_offObject

A Diagram can become quite complex and memory-consuming. Therefore diagrams are cached after creation, so that they need to be created and maintained only once when there are several messages of the same type in an interchange.

Turns off this caching mechanism, saving memory but costing time.


72
73
74
# File 'lib/edi4r/diagrams.rb', line 72

def Diagram.caching_off
  @@caching = false
end

.caching_onObject

Turns on caching (default setting), saving time but costing memory.


78
79
80
# File 'lib/edi4r/diagrams.rb', line 78

def Diagram.caching_on
  @@caching = true
end

.create(std, params) ⇒ Object

Creates (and caches) a new diagram. Returns reference to existing diagram when already in cache.

std

The syntax standard key. Currently supported:

  • 'E' (EDIFACT),

  • 'I' (SAP IDOC)

params

A hash of parameters that uniquely identify the selected diagram. Internal use only - see source code for details.


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
# File 'lib/edi4r/diagrams.rb', line 103

def Diagram.create( std, params )
  case std
  when 'E' # UN/EDIFACT
    par = {:d0051 => 'UN', 
      #           :d0057 => '',
      :is_iedi => false }.update( params )
  when 'I' # SAP IDocs
    par = params
    #      raise "Not implemented yet!"
  else
    raise "Unsupported syntax standard: #{std}"
  end

  if Diagram.caching?
    #
    # Use param set as key for caching
    #
    key = par.sort {|a,b| a.to_s <=> b.to_s}.hash
    obj = @@cache[key]
    return obj unless obj.nil?
    
    obj = new( std, par )
    @@cache[key] = obj # cache & return it

  else
    new( std, par )
  end
end

.flush_cacheObject

Releases memory by flushing the cache. Needed primarily for unit tests, where many if not all available diagrams are created.


91
92
93
# File 'lib/edi4r/diagrams.rb', line 91

def Diagram.flush_cache
  @@cache = {}
end

Instance Method Details

#[](i) ⇒ Object

Index access through ordinal number of node, starting with 1 (!).


191
192
193
# File 'lib/edi4r/diagrams.rb', line 191

def [](i)
  @node_dict[i] # efficient access via hash
end

#branchObject

Returns the top branch of the diagram.


203
204
205
# File 'lib/edi4r/diagrams.rb', line 203

def branch
  @diag
end

#dirObject

Getter for the directory object associated with this diagram.


197
198
199
# File 'lib/edi4r/diagrams.rb', line 197

def dir
  @dir
end

#each(&b) ⇒ Object

Iterates recursively through all nodes of the diagram.


184
185
186
# File 'lib/edi4r/diagrams.rb', line 184

def each(&b)
  @diag.each(&b)
end