Class: Firebolt::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/firebolt/config.rb

Constant Summary collapse

CACHE_FILENAME =
"firebolt.cache.json".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Constructor!



8
9
10
11
12
13
# File 'lib/firebolt/config.rb', line 8

def initialize(options = {})
  merge!(options)

  self[:cache_file_path] ||= '/tmp'
  self[:namespace] ||= ::SecureRandom.hex
end

Class Method Details

.hash_accessor(*names) ⇒ Object

Creates an accessor that simply sets and reads a key in the hash:

class Config < Hash
  hash_accessor :app
end

config = Config.new
config.app = Foo
config[:app] #=> Foo

config[:app] = Bar
config.app #=> Bar


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/firebolt/config.rb', line 32

def self.hash_accessor(*names) #:nodoc:
  names.each do |name|
    class_eval <<-METHOD, __FILE__, __LINE__ + 1
      def #{name}
        self[:#{name}]
      end

      def #{name}=(value)
        self[:#{name}] = value
      end
    METHOD
  end
end

Instance Method Details

#cache_fileObject

Public instance methods



52
53
54
# File 'lib/firebolt/config.rb', line 52

def cache_file
  ::File.join(self[:cache_file_path], CACHE_FILENAME)
end

#cache_file_enabled?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/firebolt/config.rb', line 56

def cache_file_enabled?
  !! ::Firebolt.config.cache_file_enabled
end

#cache_file_path=(path) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
# File 'lib/firebolt/config.rb', line 60

def cache_file_path=(path)
  raise ArgumentError, "Directory '#{path}' does not exist or is not writable." unless ::File.writable?(path)

  self[:cache_file_path] = path
end

#cache_file_readable?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/firebolt/config.rb', line 66

def cache_file_readable?
  ::File.readable?(cache_file)
end

#namespaceObject



70
71
72
# File 'lib/firebolt/config.rb', line 70

def namespace
  @namespace ||= "firebolt.#{self[:namespace]}"
end

#use_file_warmer?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/firebolt/config.rb', line 74

def use_file_warmer?
  cache_file_enabled? && cache_file_readable?
end

#warmer=(value) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
# File 'lib/firebolt/config.rb', line 78

def warmer=(value)
  raise ArgumentError, "Warmer must include the ::Firebolt::Warmer module." unless value.ancestors.include?(::Firebolt::Warmer)
  raise ArgumentError, "Warmer must respond to #perform." unless value.instance_methods.include?(:perform)

  self[:warmer] = value
end