Sha256: d3aebb56af49417998cbe4d47ff22df88a1d5bf90933e7bf11758d7579855e4d

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'fileutils'
require 'json'

module ConfCtl
  class Swpins::Channel
    # @return [String]
    attr_reader :path

    # @return [String]
    attr_reader :name

    # @return [Hash<String, Swpins::Specs::Base>]
    attr_reader :specs

    # @param name [String]
    # @param nix_specs [Hash]
    def initialize(name, nix_specs)
      @name = name
      @path = File.join(ConfCtl::Swpins.channel_dir, "#{name}.json")
      @nix_specs = nix_specs
    end

    def parse
      @json_specs = if File.exist?(path)
                      JSON.parse(File.read(path))
                    else
                      {}
                    end

      @specs = nix_specs.to_h do |name, nix_opts|
        [
          name,
          Swpins::Spec.for(nix_opts['type'].to_sym).new(
            name,
            nix_opts[nix_opts['type']],
            json_specs[name]
          )
        ]
      end
    end

    def valid?
      specs.values.all?(&:valid?)
    end

    def save
      tmp = "#{path}.new"

      FileUtils.mkdir_p(ConfCtl::Swpins.channel_dir)

      File.open(tmp, 'w') do |f|
        f.puts(JSON.pretty_generate(specs))
      end

      File.rename(tmp, path)
    end

    protected

    attr_reader :nix_specs, :json_specs
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confctl-2.0.0 lib/confctl/swpins/channel.rb
confctl-1.0.0 lib/confctl/swpins/channel.rb