Module: Grape::ShamanCache::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/grape-shaman_cache/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Default configuration settings.



26
27
28
# File 'lib/grape-shaman_cache/config.rb', line 26

def defaults
  @defaults
end

#settingsObject

Current configuration settings.



23
24
25
# File 'lib/grape-shaman_cache/config.rb', line 23

def settings
  @settings
end

Instance Method Details

#cacheCache

Returns the cache, or defaults to Rails cache when running in Rails or an instance of ActiveSupport::Cache::MemoryStore otherwise.

config.cache

Examples:

Get the cache.

Returns:

  • (Cache)

    The configured cache or a default cache instance.



80
81
82
83
# File 'lib/grape-shaman_cache/config.rb', line 80

def cache
  settings[:cache] = default_cache unless settings.has_key?(:cache)
  settings[:cache]
end

#cache=(cache) ⇒ Cache

Sets the cache to use.

config.cache = Rails.cache

Examples:

Set the cache.

Returns:

  • (Cache)

    The newly set cache.



91
92
93
# File 'lib/grape-shaman_cache/config.rb', line 91

def cache=(cache)
  settings[:cache] = cache
end

#default_cacheCache

Returns the default cache store, either Rails.cache or an instance of ActiveSupport::Cache::MemoryStore.

config.default_cache

Examples:

Get the default cache store

Returns:

  • (Cache)

    The default cache store instance.



65
66
67
68
69
70
71
# File 'lib/grape-shaman_cache/config.rb', line 65

def default_cache
  if defined?(Rails) && Rails.respond_to?(:cache)
    Rails.cache
  else
    ::ActiveSupport::Cache::MemoryStore.new
  end
end

#option(name, options = {}) ⇒ Object

Define a configuration option with a default.

Config.option(:cache, :default => nil)

Examples:

Define the option.

Parameters:

  • name (Symbol)

    The name of the configuration option.

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

    Extras for the option.

Options Hash (options):

  • :default (Object)

    The default value.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/grape-shaman_cache/config.rb', line 40

def option(name, options = {})
  defaults[name] = settings[name] = options[:default]

  class_eval <<-RUBY
    def #{name}
      settings[#{name.inspect}]
    end

    def #{name}=(value)
      settings[#{name.inspect}] = value
    end

    def #{name}?
      #{name}
    end
  RUBY
end