Sha256: 65e29f13c168fc3dd1b793ca25821b1d291ced85e6509f2fe0ccbfa74f8d3990

Contents?: true

Size: 803 Bytes

Versions: 10

Compression:

Stored size: 803 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

10 entries across 10 versions & 1 rubygems

Version Path
cyberarm_engine-0.24.4 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.24.3 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.24.2 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.24.1 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.24.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.23.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.22.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.21.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.20.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.19.1 lib/cyberarm_engine/config_file.rb