Module: Pocolog::Upgrade::DSL

Defined in:
lib/pocolog/upgrade/dsl.rb

Defined Under Namespace

Classes: Context

Class Method Summary collapse

Class Method Details

.create(output_path, reference_time, source_type, target_type, description: "Converter created at #{Time.now}") ⇒ Object

Create a template converter for given time, source and target types



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
# File 'lib/pocolog/upgrade/dsl.rb', line 53

def self.create(output_path, reference_time, source_type, target_type, description: "Converter created at #{Time.now}")
    file_basename = reference_time.iso8601 + source_type.name.gsub('/', ':')
    existing_paths = Pathname.enum_for(:glob, output_path + (file_basename + ".*"))
    max_id = existing_paths.map do |p|
        e = p.extname
        if e =~ /^\.(\d+)$/
            Integer($1)
        end
    end.compact.max
    id = (max_id || 0) + 1

    converter_file = file_basename + "." + id.to_s
    source_tlb     = converter_file + ".source.tlb"
    target_tlb     = converter_file + ".target.tlb"

    (output_path + converter_file).open('w') do |io|
        io.puts "# #{description}"
        io.puts "define \"#{reference_time.to_time}\", \"#{source_type.name}\", \"#{target_type.name}\" do |target_value, source_value|"
        io.puts "    # This would copy everything that can be copied from the source to the target"
        io.puts "    # deep_cast(target_value, source_value)"
        io.puts "    target_value"
        io.puts "end"
    end
    (output_path + source_tlb).open('w') do |io|
        io.write source_type.to_xml
    end
    (output_path + target_tlb).open('w') do |io|
        io.write target_type.to_xml
    end
    return (output_path + converter_file), (output_path + source_tlb), (output_path + target_tlb)
end

.load_dir(load_path, converter_registry) ⇒ Object

Load a directory containing custom converters generated by 'rock-log create-converter'



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pocolog/upgrade/dsl.rb', line 87

def self.load_dir(load_path, converter_registry)
    converters = Array.new
    Pathname.glob(Pathname.new(load_path) + "*") do |path|
        next if !File.file?(path)
        source_tlb = path.sub(/$/, '.source.tlb')
        target_tlb = path.sub(/$/, '.target.tlb')
        if source_tlb.exist? && target_tlb.exist?
            # Assume this *is* a converter
            source_registry = Typelib::Registry.from_xml(source_tlb.read)
            target_registry = Typelib::Registry.from_xml(target_tlb.read)
            context = Context.new(path.to_s, converter_registry, source_registry, target_registry)
            context.instance_eval(path.read, path.to_s, 1)
            if c = context.converter
                converters << c
            end
        end
    end
    converters
end