summaryrefslogtreecommitdiff
path: root/lib/killjoy/cassandra/database_configuration.rb
blob: 1361d028024c7e1858cae3ebc91baf2bd9fbf357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'erb'
require 'yaml'

module Killjoy
  module Cassandra
    class DatabaseConfiguration
      attr_reader :environment

      def initialize(environment: ENV.fetch("ENV", "development"))
        @environment = environment
      end

      def hosts
        configuration["hosts"]
      end

      def keyspace
        configuration["keyspace"]
      end

      def port
        configuration["port"].to_i
      end

      def expand(file)
        new_path = file.gsub(/\.erb/, '')
        IO.write(new_path, expand_template(file))
      end

      private

      def configuration(file = "config/database.yml")
        @configuration ||=
          YAML.load(expand_template(file))[environment]
      end

      def expand_template(file)
        ERB.new(File.read(file)).result(binding)
      end
    end
  end
end