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
# 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
  self.options[:cachely_url] = "#{(self.options[:cachely_url] ||= ENV["CACHELY_URL"])}/v1/cache"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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



19
20
21
# File 'lib/rack-cachely/config.rb', line 19

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

#ignore_query_params=(*args) ⇒ Object



15
16
17
# File 'lib/rack-cachely/config.rb', line 15

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

#loggerObject



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

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