Class: RedashExporter::Query

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/redash_exporter/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Query

Returns a new instance of Query.



13
14
15
16
# File 'lib/redash_exporter/query.rb', line 13

def initialize(raw)
  @data = raw.deep_symbolize_keys
  @sort_key = :id
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/redash_exporter/query.rb', line 10

def data
  @data
end

#sort_keyObject

Returns the value of attribute sort_key.



10
11
12
# File 'lib/redash_exporter/query.rb', line 10

def sort_key
  @sort_key
end

Instance Method Details

#<=>(other) ⇒ Object



39
40
41
# File 'lib/redash_exporter/query.rb', line 39

def <=>(other)
  self[@sort_key] <=> other[@sort_key]
end

#[](key) ⇒ Object



18
19
20
# File 'lib/redash_exporter/query.rb', line 18

def [](key)
  @data[key.to_sym]
end

#export(dir: '.', force: false) ⇒ Object



43
44
45
46
# File 'lib/redash_exporter/query.rb', line 43

def export(dir: '.', force: false, **)
  path = File.expand_path("#{dir}/#{file_name}")
  Exporter.export(path, to_s, force: force)
end

#to_json(*_args) ⇒ Object



35
36
37
# File 'lib/redash_exporter/query.rb', line 35

def to_json(*_args)
  JSON.generate(@data)
end

#to_sObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redash_exporter/query.rb', line 22

def to_s
  <<~SQL
    /**
     * #{@data[:name]}
     *
     * created at #{@data[:created_at]}
     * last updated at #{@data[:updated_at]}
     * created by #{@data.dig(:user, :name)} (#{@data.dig(:user, :email)})
     **/
     #{@data[:query]}
  SQL
end