Sha256: 7b260c990df196d54e904a328c50202a83e0d223077a3382c8f702fcd476b561

Contents?: true

Size: 749 Bytes

Versions: 2

Compression:

Stored size: 749 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[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

2 entries across 2 versions & 1 rubygems

Version Path
cyberarm_engine-0.14.0 lib/cyberarm_engine/config_file.rb
cyberarm_engine-0.13.1 lib/cyberarm_engine/config_file.rb