Class: Qravan::Resource

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

Overview

Resource processing class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data, cargo = {}) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
# File 'lib/qravan/resource/resource.rb', line 7

def initialize(name, data, cargo = {})
  @resource = name
  @resource_data = data
  @cargo ||= cargo
  @connections = cargo.connections
end

Instance Attribute Details

#cargoObject

Returns the value of attribute cargo.



5
6
7
# File 'lib/qravan/resource/resource.rb', line 5

def cargo
  @cargo
end

#connectionsObject

Returns the value of attribute connections.



5
6
7
# File 'lib/qravan/resource/resource.rb', line 5

def connections
  @connections
end

Instance Method Details

#extractObject



18
19
20
21
# File 'lib/qravan/resource/resource.rb', line 18

def extract
  source_name = "#{cargo.models[@resource]["extract"]["source"]["name"]}_source"
  @connections[source_name.to_s].extract(sql)
end

#sqlObject



23
24
25
26
27
28
29
30
31
# File 'lib/qravan/resource/resource.rb', line 23

def sql
  [
    :select,
    @resource_data["attributes"].join(","),
    :from,
    cargo.models[@resource]["extract"]["source"]["table"],
    where
  ].flatten.join(" ")
end

#validateObject



14
15
16
# File 'lib/qravan/resource/resource.rb', line 14

def validate
  @resource_data
end

#whereObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/qravan/resource/resource.rb', line 33

def where
  if @resource_data["conditions"]
    [
      :where,
      @resource_data["conditions"].map { |k, v| "#{k}='#{v}'" }
                                  .join(" and ")
    ]
  else
    [""]
  end
end