Sha256: 845d67c102880eb18ab8cf33a4ef62913eac401a0490d28b58e4c63e3123dadc

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require "yaml"

module Shipit
  module Cli
    class ConfigurationFile
      ATTR_READER = [:endpoint, :private_token, :protected_branches, :ship_skips_ci]
      attr_reader(*ATTR_READER)

      def initialize(path, configuration = nil)
        @path = path
        @file = load_file
        @configuration = configuration || load_configuration
      end

      def exist?
        File.exist?(@path)
      end

      def to_hash
        config_hash = ATTR_READER.inject({}) do |hash, attr|
          hash["#{attr}"] = instance_variable_get("@#{attr}")
          hash
        end
        Shipit::Cli::Sanitizer.symbolize config_hash
      end

      def persist
        File.open(@path, "w") do |f|
          f.write @configuration.to_yaml
        end
        reload!
      end

      def reload!
        @file = load_file
        @configuration = load_configuration
      end

      private

      def load_file
        if exist?
          Shipit::Cli::Sanitizer.symbolize YAML.load_file(@path)
        else
          {}
        end
      end

      def load_configuration
        if @file
          ATTR_READER.each do |attr|
            instance_variable_set "@#{attr}", @file[attr]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
intello-shipit-cli-1.3.1 lib/shipit/cli/configuration_file.rb
intello-shipit-cli-1.3.0 lib/shipit/cli/configuration_file.rb