Sha256: b98aa2d8dbe5385a8c2ac2dbe08e740b6fe1724c435c19d7cc20a7dcb19022d2

Contents?: true

Size: 757 Bytes

Versions: 6

Compression:

Stored size: 757 Bytes

Contents

module CyberarmEngine
  class ConfigFile
    def initialize(file:)
      @file = file

      if File.exist?(@file)
        deserialize
      else
        @data = {}
      end
    end

    def []=(*keys, value)
      last_key = keys.last

      if keys.size == 1
        hash = @data
      else
        keys.pop
        hash = @data[keys.shift] ||= {}

        keys.each do |key|
          hash = hash[key] ||= {}
        end
      end

      hash[last_key] = value
    end

    def get(*keys)
      @data.dig(*keys)
    end

    def serialize
      JSON.dump(@data)
    end

    def deserialize
      @data = JSON.parse(File.read(@file), symbolize_names: true)
    end

    def save!
      File.open(@file, "w") { |f| f.write(serialize) }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cyberarm_engine-0.19.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.18.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.17.1 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.17.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.16.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.15.0 lib/cyberarm_engine/config_file.rb