Class: Rack::Cachely::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-cachely/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack-cachely/config.rb', line 7

def initialize(options = {})
  self.options = {ignore_query_params: [], allow_query_params: [], timeout: 1.0}
  options.each do |key, value|
    self.send("#{key}=", value)
  end

  url = options[:cachely_url] || ENV["CACHELY_URL"]

  if !url || (url.respond_to?(:empty?) && url.empty?)
    self.logger.warn("Cachely: config incomplete (cachely_url is missing), disabling cachley.")
    self.options[:enabled] = false
  else
    url = "#{url}/v1/cache"
    self.options[:enabled] = true
  end
  self.cachely_url = url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rack-cachely/config.rb', line 33

def method_missing(sym, *args, &block)
  if /(.+)=$/.match(sym.to_s)
    self.options[$1.to_sym] = args[0]
  else
    self.options[sym]
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/rack-cachely/config.rb', line 5

def options
  @options
end

Instance Method Details

#allow_query_params=(*args) ⇒ Object



29
30
31
# File 'lib/rack-cachely/config.rb', line 29

def allow_query_params=(*args)
  self.options[:allow_query_params] = [args].flatten.map {|x| x.downcase}
end

#ignore_query_params=(*args) ⇒ Object



25
26
27
# File 'lib/rack-cachely/config.rb', line 25

def ignore_query_params=(*args)
  self.options[:ignore_query_params] = [args].flatten.map {|x| x.downcase}
end

#loggerObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rack-cachely/config.rb', line 41

def logger
  self.options[:logger] ||= begin
    if Object.const_defined?(:Rails)
      logger = Rails.logger
    else
      path = ::File.expand_path("log", ::File.dirname(__FILE__))
      FileUtils.mkdir_p(path)
      logger = ::Logger.new(::File.join(path, "#{ENV['RACK_ENV']}.log"))
    end
    logger
  end
end