Sha256: c2ebd83b48b1b439e3e017a9aba9b7d2659d8be11eed49fa25b32969d1bfdc37

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

require 'pathname'
require 'json'

module Landrush
  class Store
    def self.hosts
      @hosts ||= new(Server.working_dir.join('hosts.json'))
    end

    def self.config
      @config ||= new(Server.working_dir.join('config.json'))
    end

    def self.reset
      @config = nil
      @hosts = nil
    end

    attr_accessor :backing_file

    def initialize(backing_file)
      @backing_file = Pathname(backing_file)
    end

    def set(key, value)
      write(current_config.merge(key => value))
    end

    def each(*args, &block)
      current_config.each(*args, &block)
    end

    def delete(key)
      write(current_config.reject { |k, v| k == key || v == key })
    end

    def has?(key, value = nil)
      if value.nil?
        current_config.key? key
      else
        current_config[key] == value
      end
    end

    def find(search)
      search = IPAddr.new(search).reverse if (IPAddr.new(search) rescue nil)
      current_config.keys.detect do |key|
        key.casecmp(search) == 0   ||
          search =~ /#{key}$/i     ||
          key    =~ /^#{search}\./i
      end
    end

    def get(key)
      current_config[key]
    end

    def clear!
      write({})
    end

    protected

    def current_config
      if backing_file.exist?
        begin
          JSON.parse(File.read(backing_file))
        rescue JSON::ParserError
          {}
        end
      else
        {}
      end
    end

    def write(config)
      File.open(backing_file, "w") do |f|
        f.write(JSON.pretty_generate(config))
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
landrush-1.1.2 lib/landrush/store.rb
landrush-1.1.1 lib/landrush/store.rb
landrush-1.1.0 lib/landrush/store.rb
landrush-1.1.0.beta.4 lib/landrush/store.rb
landrush-1.1.0.beta.3 lib/landrush/store.rb
landrush-1.1.0.beta.2 lib/landrush/store.rb
landrush-1.1.0.beta.1 lib/landrush/store.rb
landrush-1.0.0 lib/landrush/store.rb