Sha256: 0d3215bf8fdfc2422a7b4e7bb5427606adcb53e23005735d60d0e3508094c36d

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require "filetree"
require "json"

module Rsense
  module Server

  end
end

class Rsense::Server::Config
  attr_accessor :searched, :options, :errors, :port, :ignores

  def initialize
    @searched = []
    @ignores = []
    @errors = []
  end

  def search(path_str="~", level=0)
    level = level + 1
    path = FileTree.new(path_str)
    return if @searched.include?(path)
    @searched << path
    conf = path.join(".rsense").expand_path
    unless conf.exist?
      if path.parent == path || level == 3
        contender = Pathname.new("~").join(".rsense").expand_path
        conf = contender if contender.exist?
      else
        conf = search(path.parent, level)
      end
    end
    conf
  end

  def options(config_path)
    begin
      json_str = JSON.parse(config_path.read)
    rescue JSON::ParserError => e
      @errors << e
    end
    if json_str
      @options ||= Rsense::Server::Options.new(json_str)
    else
      @options ||= Rsense::Server::Options.new({})
    end
    @options
  end

  def port
    @port ||= check_options("port")
  end

  def ignores
    if @ignores.empty?
      @ignores << check_options("ignores")
      @ignores.flatten!
    end
    @ignores
  end

  def check_options(name)
    @options.rest[name] if @options.rest.key?(name)
  end

  def set_up(path)
    conf = search(path)
    if conf
      options(conf)
    end
  end


end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rsense-server-0.5.9 lib/rsense/server/config.rb