Class: Ruote::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/reader.rb

Overview

A process definition reader.

Can reader XML, JSON, Ruby (and more) process definition representations.

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Reader

Returns a new instance of Reader.



77
78
79
80
# File 'lib/ruote/reader.rb', line 77

def initialize(context)

  @context = context
end

Class Method Details

.read(d) ⇒ Object

Class method for parsing process definition (XML, Ruby, from file or from a string, …) to syntax trees. Used by ruote-fluo for example.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruote/reader.rb', line 126

def self.read(d)

  unless @reader

    require 'ostruct'
    require 'ruote/svc/treechecker'

    @reader = Ruote::Reader.new(
      OpenStruct.new('treechecker' => Ruote::TreeChecker.new({})))
  end

  @reader.read(d)
end

.remote?(definition) ⇒ Boolean

Returns true if the defintion is a remote URI

Returns:

  • (Boolean)


226
227
228
229
230
231
# File 'lib/ruote/reader.rb', line 226

def self.remote?(definition)

  u = URI.parse(definition)

  (u.scheme != nil) && ( ! ('A'..'Z').include?(u.scheme))
end

.to_json(tree) ⇒ Object

Turns the process definition tree (ruote syntax tree) to a JSON String.



219
220
221
222
# File 'lib/ruote/reader.rb', line 219

def self.to_json(tree)

  tree.to_json
end

.to_radial(tree, level = 0) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ruote/reader.rb', line 200

def self.to_radial(tree, level=0)

  s =
    '  ' * level +
    tree[0] +
    atts_to_x(tree[1]) { |k, v|
      "#{k}: #{v.inspect}"
    }

  return "#{s}\n" if tree[2].empty?

  s << "\n"
  tree[2].each { |child| s << to_radial(child, level + 1) }

  s
end

.to_ruby(tree, level = 0) ⇒ Object

Turns the given process definition tree (ruote syntax tree) to a Ruby process definition (a String containing that ruby process definition).

Mainly used by ruote-fluo.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ruote/reader.rb', line 179

def self.to_ruby(tree, level=0)

  expname = tree[0]
  expname = 'Ruote.process_definition' if level == 0 && expname == 'define'

  s =
    '  ' * level +
    expname +
    atts_to_x(tree[1]) { |k, v|
      ":#{k} => #{v.inspect}"
    }

  return "#{s}\n" if tree[2].empty?

  s << " do\n"
  tree[2].each { |child| s << to_ruby(child, level + 1) }
  s << "#{'  ' * level}end\n"

  s
end

.to_xml(tree, options = {}) ⇒ Object

Turns the given process definition tree (ruote syntax tree) to an XML String.

Mainly used by ruote-fluo.



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
# File 'lib/ruote/reader.rb', line 145

def self.to_xml(tree, options={})

  require 'builder'

  # TODO : deal with "participant 'toto'"

  builder(options) do |xml|

    atts = tree[1].dup

    t = atts.find { |k, v| v == nil }
    if t
      atts.delete(t.first)
      key = tree[0] == 'if' ? 'test' : 'ref'
      atts[key] = t.first
    end

    atts = atts.inject({}) { |h, (k, v)| h[k.to_s.gsub(/\_/, '-')] = v; h }

    if tree[2].empty?
      xml.tag!(tree[0], atts)
    else
      xml.tag!(tree[0], atts) do
        tree[2].each { |child| to_xml(child, options) }
      end
    end
  end
end

Instance Method Details

#read(definition) ⇒ Object

Turns the input into a ruote syntax tree (raw process definition). This method is used by engine.launch(x) for example.

Raises:

  • (ArgumentError)


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
# File 'lib/ruote/reader.rb', line 85

def read(definition)

  return definition if Ruote.is_tree?(definition)

  raise ArgumentError.new(
    "cannot read process definitions of class #{definition.class}"
  ) unless definition.is_a?(String)

  if is_uri?(definition)

    raise ArgumentError.new(
      "remote process definitions are not allowed"
    ) if Ruote::Reader.remote?(definition) && @context['remote_definition_allowed'] != true

    return read(open(definition).read)
  end

  tree = nil
  error = Error.new(definition)

  [
    Ruote::RubyReader, Ruote::RadialReader,
    Ruote::XmlReader, Ruote::JsonReader
  ].each do |reader|

    next if tree
    next unless reader.understands?(definition)

    begin
      tree = reader.read(definition, @context.treechecker)
    rescue => e
      error << [ reader, e ]
    end
  end

  tree || raise(error)
end