Class: CloudFile::Convertors
- Includes:
- FromHash
- Defined in:
- lib/cloud_file/convertors.rb
Class Method Summary collapse
Instance Method Summary collapse
- #convert(from, to, str) ⇒ Object
- #convert_inner(from, to, str) ⇒ Object
- #find_path(source, target, prev = []) ⇒ Object
- #graph ⇒ Object
- #register(ops, &b) ⇒ Object
Class Method Details
.method_missing(sym, *args, &b) ⇒ Object
5 6 7 |
# File 'lib/cloud_file/convertors.rb', line 5 def method_missing(sym,*args,&b) instance.send(sym,*args,&b) end |
Instance Method Details
#convert(from, to, str) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cloud_file/convertors.rb', line 22 def convert(from,to,str) path = find_path(from,to) raise "no path from #{from} to #{to}" unless path if path.empty? convert_inner(from,to,str) else str = convert_inner(from,path.first,str) convert(path.first,to,str) end end |
#convert_inner(from, to, str) ⇒ Object
17 18 19 20 21 |
# File 'lib/cloud_file/convertors.rb', line 17 def convert_inner(from,to,str) block = list.find { |x| x[:from].to_s == from.to_s && x[:to].to_s == to.to_s } raise "no convertor" unless block block[:block][str] end |
#find_path(source, target, prev = []) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cloud_file/convertors.rb', line 43 def find_path(source,target,prev=[]) raise "bad source: #{source} target: #{target}" if target.to_s == source.to_s tos = list.select { |x| x[:from] == source.to_s }.map { |x| x[:to] } puts "tos #{tos.inspect}" if tos.any? { |x| x == target.to_s } prev elsif tos.empty? nil else tos.each do |to| p = prev + [to] res = find_path(to,target,p) return res if res end nil end end |
#graph ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/cloud_file/convertors.rb', line 33 def graph require 'rgl/adjacency' a = [] list.each do |c| a << c[:from] a << c[:to] end RGL::DirectedAdjacencyGraph[*a] end |
#register(ops, &b) ⇒ Object
12 13 14 15 16 |
# File 'lib/cloud_file/convertors.rb', line 12 def register(ops,&b) #puts "ops #{ops.inspect}" self.list = list.reject { |x| x[:from].to_s == ops.keys.first.to_s && x[:to].to_s == ops.values.first.to_s } self.list << {:from => ops.keys.first.to_s, :to => ops.values.first.to_s, :block => b} end |