Class: MetabaseQuerySync::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/metabase_query_sync/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials:, paths:) ⇒ Config

Returns a new instance of Config.

Parameters:



11
12
13
14
# File 'lib/metabase_query_sync/config.rb', line 11

def initialize(credentials:, paths:)
  @credentials = credentials
  @paths = paths
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



7
8
9
# File 'lib/metabase_query_sync/config.rb', line 7

def credentials
  @credentials
end

#pathsObject (readonly)

Returns the value of attribute paths.



7
8
9
# File 'lib/metabase_query_sync/config.rb', line 7

def paths
  @paths
end

Class Method Details

.from_file(path, paths: [], host: nil, user: nil, pass: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/metabase_query_sync/config.rb', line 16

def self.from_file(path, paths: [], host: nil, user: nil, pass: nil)
  if File.exists? path
    data = YAML.load(ERB.new(File.read(path)).result)
    result = Dry::Schema.JSON do
      required(:paths).value(array[:string], min_size?: 1)
      required(:credentials).hash do
        required(:host).filled(:string)
        required(:user).filled(:string)
        required(:pass).filled(:string)
      end
    end.(data)
    raise "Invalid data provided in config file: #{result.errors.to_h}" if result.failure?
  end
  new(
    credentials: MetabaseCredentials.new(
      host: host || data["credentials"]["host"],
      user: user || data["credentials"]["user"],
      pass: pass || data["credentials"]["pass"]
    ),
    paths: paths.empty? ? data["paths"] : paths
  )
end