Class: Riddl::Wrapper::Description::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/wrapper/description/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, recursive = false) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 6

def initialize(path=nil,recursive=false)
  #{{{
  @path = path
  @role = nil
  @resources = {}
  @access_methods = {}
  @composition = {}
  @recursive = recursive
  @custom = []
  #}}}
end

Instance Attribute Details

#access_methodsObject (readonly)

}}}



393
394
395
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 393

def access_methods
  @access_methods
end

#compositionObject (readonly)

}}}



393
394
395
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 393

def composition
  @composition
end

#customObject

Returns the value of attribute custom.



394
395
396
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 394

def custom
  @custom
end

#pathObject (readonly)

}}}



393
394
395
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 393

def path
  @path
end

#recursiveObject (readonly)

}}}



393
394
395
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 393

def recursive
  @recursive
end

#resourcesObject (readonly)

}}}



393
394
395
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 393

def resources
  @resources
end

#roleObject (readonly)

}}}



393
394
395
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 393

def role
  @role
end

Instance Method Details

#add_access_methods(des, desres, index, interface) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 18

def add_access_methods(des,desres,index,interface)
  #{{{
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @in and not(@in='*')]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_in_out(index,interface,des,method,m.attributes['in'],m.attributes['out'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @pass and not(@pass='*')]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_in_out(index,interface,des,method,m.attributes['pass'],m.attributes['pass'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @transformation]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_transform(index,interface,des,method,m.attributes['transformation'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @in and @in='*']").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_star_out(index,interface,des,method,m.attributes['out'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and not(@in) and not(@pass)]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_star_out(index,interface,des,method,m.attributes['out'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @pass and @pass='*']").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_pass(index,interface,method,m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and name()='websocket']").each do |m|
    add_websocket(index,interface,m.find('*|text()'))
  end
  @role = desres.find("string(@role)")
  @role = nil if @role.strip == ''
  #}}}
end

#add_custom(desres) ⇒ Object



52
53
54
55
56
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 52

def add_custom(desres)
  #{{{
  @custom += desres.find("*[not(self::des:*)]").to_a
  #}}}
end

#add_websocket(index, interface, custom) ⇒ Object



385
386
387
388
389
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 385

def add_websocket(index,interface,custom)
  @access_methods['websocket'] ||= []
  @access_methods['websocket'][index] ||= []
  @access_methods['websocket'][index] << WebSocket.new(interface,custom)
end

#compose!Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 101

def compose!
  #{{{
  @access_methods.each do |k,v|
    ### remove all emtpy layers
    v.map!{|e| (e.nil? || e.empty?) ? nil: e }
    v.compact!
    case v.size
      when 0
      when 1
        @composition[k] = compose_plain(v[0])
      else
        @composition[k] = compose_layers(k,v)
    end
  end
  #}}}
end

#description_xml(namespaces) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 328

def description_xml(namespaces)
  #{{{
  namespaces = namespaces.delete_if do |k,n|
    k =~ /^xmlns\d+$/ || [Riddl::Wrapper::DESCRIPTION, Riddl::Wrapper::DECLARATION, Riddl::Wrapper::XINCLUDE].include?(n)
  end.map do |k,n|
    "xmlns:#{k}=\"#{n}\""
  end.join(' ')

  messages = {}
  messages_result = ''
  collect = description_xml_string(messages," " * 4) + description_xml_string_sub(messages," " * 4)
  collect = XML::Smart.string("<resource>\n" + collect + "  </resource>")

  names = []
  messages.each do |hash,mess|
    t = mess.content.dup
    name = mess.name
    name += '_' while names.include?(name)
    collect.find("//@*[.=#{hash}]").each { |e| e.value = name }
    names << name
    t.root.attributes['name'] = name
    messages_result << t.root.dump + "\n"
  end
  XML::Smart.string("<description #{Riddl::Wrapper::COMMON} #{namespaces}>\n\n" + messages_result.gsub(/^/,'  ') + "\n  " + collect.root.dump + "\n</description>").to_s
  #}}}
end

#description_xml_string(messages, t) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 292

def description_xml_string(messages,t)
 #{{{
  result = ''
  @custom.each do |c|
    result << c.dump
  end

  if @composition.any?
    @composition.each do |k,v|
      v.each do |m|
        result << description_xml_string_analyse(messages,t,k,m.result)
      end
    end
  else
    @access_methods.each do |k,v|
      v.first.each do |m|
        result << description_xml_string_analyse(messages,t,k,m)
      end
    end
  end
  result
 #}}}
end

#description_xml_string_sub(messages, t) ⇒ Object

}}}



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 315

def description_xml_string_sub(messages,t)
 #{{{
  result = ''
  @resources.each do |k,r|
    tmp = r.custom.map{ |c| c.dump }.join
    result << t + "<resource"
    result << " relative=\"#{k}\"" unless k == "{}"
    result << (tmp == '' ? "/>\n" : ">\n" + tmp + t + "</resource>\n")
  end
  result
 #}}}
end

#remove_access_methods(des, filter, index) ⇒ Object

TODO add websockets

Raises:



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
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 60

def remove_access_methods(des,filter,index)
  #{{{
  freq = if filter['in'] && filter['in'] != '*'
    t = [RequestInOut,Riddl::Wrapper::Description::Message.new(des,filter['in'])]
    t << (filter['out'] ? Riddl::Wrapper::Description::Message.new(des,filter['out']) : nil)
  elsif filter['pass'] && filter['pass'] != '*'
    [RequestInOut,Riddl::Wrapper::Description::Message.new(des,filter['pass']),Riddl::Wrapper::Description::Message.new(des,filter['pass'])]
  elsif filter['in'] && filter['in'] == '*'
    t = [RequestStarOut]
    t << (filter['out'] ? Riddl::Wrapper::Description::Message.new(des,filter['out']) : nil)
  elsif filter['transformation']
    [RequestTransformation,Riddl::Wrapper::Description::Transformation.new(des,filter['transformation'])]
  elsif filter['pass'] && filter['pass'] == '*'
    [RequestPass]
  end
  raise BlockError, "blocking #{filter.inspect} not possible" if freq.nil?

  if reqs = @access_methods[filter['method']]
    reqs = reqs[index]
    reqs.delete_if do |req|
      if req.class == freq[0]
        if req.class == RequestInOut
          # TODO These hash comparisons are maybe too trivial, as we want to catch name="*" parameters
          if freq[1] && freq[1].hash == req.in.hash && freq[2] && req.out && freq[2].hash == req.out.hash
            true
          elsif freq[1] && freq[1].hash == req.in.hash && !freq[2]
            true
          end
        elsif req.class == RequestStarOut
          true if freq[1] && req.out && freq[1].hash == req.out.hash
        elsif req.class == RequestTransformation
          true if freq[1] && freq[1].hash == req.trans.hash
        elsif req.class == RequestPass
          true
        end
      end
    end
  end
  #}}}
end