Module: Garner::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/garner/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Default configuration settings.



25
26
27
# File 'lib/garner/config.rb', line 25

def defaults
  @defaults
end

#settingsObject

Current configuration settings.



22
23
24
# File 'lib/garner/config.rb', line 22

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.

Examples:

Get the cache.

config.cache

Returns:

  • (Cache)

    The configured cache or a default cache instance.



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

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

#cache=(cache) ⇒ Cache

Sets the cache to use.

Examples:

Set the cache.

config.cache = Rails.cache

Returns:

  • (Cache)

    The newly set cache.



90
91
92
# File 'lib/garner/config.rb', line 90

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

#caller_rootString

Returns the manually configured caller_root, or a default.

Returns:

  • (String)

    The configured caller_root or a default.



105
106
107
108
# File 'lib/garner/config.rb', line 105

def caller_root
  settings[:caller_root] = default_caller_root unless settings.has_key?(:caller_root)
  settings[:caller_root]
end

#caller_root=(caller_root) ⇒ String

Sets the caller_root to use.

Returns:

  • (String)

    The newly set caller_root.



113
114
115
# File 'lib/garner/config.rb', line 113

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

#default_cacheCache

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

Examples:

Get the default cache store

config.default_cache

Returns:

  • (Cache)

    The default cache store instance.



64
65
66
67
68
69
70
# File 'lib/garner/config.rb', line 64

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

#default_caller_rootString

Returns the default caller root, as determined by Garner::Strategies::Context::Key::Caller.

Returns:

  • (String)

    The default caller_root path.



98
99
100
# File 'lib/garner/config.rb', line 98

def default_caller_root
  Garner::Strategies::Context::Key::Caller.default_root
end

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

Define a configuration option with a default.

Examples:

Define the option.

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

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.



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

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

#reset!Object

Reset the configuration options to the defaults.

Examples:

Reset the configuration options.

config.reset!


121
122
123
# File 'lib/garner/config.rb', line 121

def reset!
  settings.replace(defaults)
end