Class: Qravan::Cargo

Inherits:
Object
  • Object
show all
Defined in:
lib/qravan/cargo.rb

Overview

Load models and sources to cargo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCargo

Returns a new instance of Cargo.



8
9
10
11
12
13
# File 'lib/qravan/cargo.rb', line 8

def initialize
  Console.logger.info "Cargo initialized:"
  Console.logger.info "Sources: #{sources.count}"
  Console.logger.info "Models: #{models.count}"
  Console.logger.info "Connections: #{connections.count}"
end

Instance Attribute Details

#connectionsObject

Returns the value of attribute connections.



6
7
8
# File 'lib/qravan/cargo.rb', line 6

def connections
  @connections
end

#modelsObject

Returns the value of attribute models.



6
7
8
# File 'lib/qravan/cargo.rb', line 6

def models
  @models
end

#sourcesObject

Returns the value of attribute sources.



6
7
8
# File 'lib/qravan/cargo.rb', line 6

def sources
  @sources
end

Instance Method Details

#concat_modelsObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/qravan/cargo.rb', line 37

def concat_models
  model_files = {}
  (list("models") - %w[models/current models/base]).each do |model|
    Console.logger.info "Reading model file #{model}/1.0/model.yaml"
    model_files[model] = File.readlines("#{model}/1.0/model.yaml").map(&:chomp)
  end

  File.open("models/current/model.yaml", "w") do |output_file|
    output_file.puts File.readlines("models/base/1.0/model.yaml").map(&:chomp)
    model_files.each { |elem| output_file.puts elem[1] }
  end
end

#connect_sourcesObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qravan/cargo.rb', line 61

def connect_sources
  @connections = {}
  sources.each do |source|
    case source[1]["type"]
    when "rest"
      @connections[source[0]] = Qravan::Rest.new(source[1])
    when "db"
      @connections[source[0]] = Qravan::Db.new(source[1])
    else
      Console.logger.warn "Not supported resource"
    end
  end
  @connections
end

#list(what = "sources") ⇒ Object



76
77
78
# File 'lib/qravan/cargo.rb', line 76

def list(what = "sources")
  Dir["#{what}/*"]
end

#preload_modelsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/qravan/cargo.rb', line 50

def preload_models
  concat_models
  @models = {}
  Console.logger.info "Preparing models"
  preloaded_models = YAML.load_file("models/current/model.yaml")
  preloaded_models["resources"].each do |model|
    @models[model.keys.first] = model
  end
  @models
end

#preload_sourcesObject



27
28
29
30
31
32
33
34
35
# File 'lib/qravan/cargo.rb', line 27

def preload_sources
  @sources = {}
  list("sources").each do |source|
    Console.logger.info "Reading source #{source}"
    preloaded_source = YAML.load_file("#{source}/1.0/source.yaml")
    @sources[preloaded_source.keys.first] = preloaded_source.first.last
  end
  @sources
end