Class: Boffin::Config

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

Overview

Stores configuration state to be used in various parts of the app. You will likely not need to instantiate Config directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|self| ... } ⇒ Config

Returns a new instance of Config.

Parameters:

  • opts (Hash) (defaults to: {})

    The parameters to create a new Config instance

Options Hash (opts):

  • :redis (Redis)
  • :namespace (String)
  • :hours_window_secs (Fixnum)
  • :days_window_secs (Fixnum)
  • :months_window_secs (Fixnum)
  • :cache_expire_secs (Fixnum)

Yields:

  • (self)


23
24
25
26
# File 'lib/boffin/config.rb', line 23

def initialize(opts = {}, &block)
  yield(self) if block_given?
  update(opts)
end

Instance Attribute Details

#cache_expire_secsFixnum

Returns Number of seconds to cache the results of Tracker.top.

Returns:

  • (Fixnum)

    Number of seconds to cache the results of Tracker.top



86
87
88
# File 'lib/boffin/config.rb', line 86

def cache_expire_secs
  @cache_expire_secs ||= 15 * 60 # 15 minutes
end

#days_window_secsFixnum

Returns Number of seconds to maintain the daily hit interval window.

Returns:

  • (Fixnum)

    Number of seconds to maintain the daily hit interval window



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

def days_window_secs
  @days_window_secs ||= 3 * 30 * 24 * 3600 # 3 months
end

#hours_window_secsFixnum

Returns Number of seconds to maintain the hourly hit interval window.

Returns:

  • (Fixnum)

    Number of seconds to maintain the hourly hit interval window



68
69
70
# File 'lib/boffin/config.rb', line 68

def hours_window_secs
  @hours_window_secs ||= 3 * 24 * 3600 # 3 days
end

#months_window_secsFixnum

Returns Number of seconds to maintain the monthly hit interval window.

Returns:

  • (Fixnum)

    Number of seconds to maintain the monthly hit interval window



80
81
82
# File 'lib/boffin/config.rb', line 80

def months_window_secs
  @months_window_secs ||= 3 * 12 * 30 * 24 * 3600 # 3 years
end

#namespaceString

The namespace to prefix all Redis keys with. Defaults to "boffin" or "boffin:<env>" if RACK_ENV or RAILS_ENV are present in the environment.

Returns:

  • (String)


56
57
58
59
60
61
62
63
64
# File 'lib/boffin/config.rb', line 56

def namespace
  @namespace ||= begin
    if (env = ENV['RACK_ENV'] || ENV['RAILS_ENV'])
      "boffin:#{env}"
    else
      "boffin"
    end
  end
end

#redisRedis

The Redis instance that will be used to store hit data

Returns:

  • (Redis)

    the active Redis connection



48
49
50
# File 'lib/boffin/config.rb', line 48

def redis
  @redis ||= Redis.connect
end

Instance Method Details

#merge(updates = {}) ⇒ Config

Creates a copy of self and updates the copy with the values provided

Parameters:

  • updates (Hash) (defaults to: {})

    A hash of options to merge with the instance

Returns:

  • (Config)

    the new Config instance with updated values



42
43
44
# File 'lib/boffin/config.rb', line 42

def merge(updates = {})
  dup.update(updates)
end

#update(updates = {}) ⇒ self

Updates self with the values provided

Parameters:

  • updates (Hash) (defaults to: {})

    A hash of options to update the config instance with

Returns:

  • (self)


32
33
34
35
36
# File 'lib/boffin/config.rb', line 32

def update(updates = {})
  tap do |conf|
    updates.each_pair { |k, v| conf.send(:"#{k}=", v) }
  end
end