Sha256: b34ee4b1a05f1dde3cf84227a479ec105b099051196cfe8d0e167c91cddae952

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require 'active_support'
require 'active_support/core_ext'
require 'zaru'
require 'redash_exporter/exporter'

module RedashExporter
  class Query
    attr_accessor :sort_key, :data
    include Comparable

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

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

    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

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

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

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

    private

    def file_name
      Zaru.sanitize! "#{@data[:id]}-#{@data[:name]}.sql"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redash_exporter-0.3.1 lib/redash_exporter/query.rb
redash_exporter-0.3.0 lib/redash_exporter/query.rb