Module: Mongoid::CachedJson::Config

Extended by:
Config
Includes:
ActiveSupport::Callbacks
Included in:
Config
Defined in:
lib/mongoid-cached-json/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Default configuration settings.



12
13
14
# File 'lib/mongoid-cached-json/config.rb', line 12

def defaults
  @defaults
end

#settingsObject

Current configuration settings.



9
10
11
# File 'lib/mongoid-cached-json/config.rb', line 9

def settings
  @settings
end

Instance Method Details

#cacheCache

Returns the cache, or defaults to Rails cache when running under Rails or ActiveSupport::Cache::MemoryStore.

Examples:

Get the cache.

config.cache

Returns:

  • (Cache)

    The configured cache or a default cache instance.



84
85
86
87
# File 'lib/mongoid-cached-json/config.rb', line 84

def cache
  settings[:cache] = default_cache unless settings.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.



95
96
97
# File 'lib/mongoid-cached-json/config.rb', line 95

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

#default_cacheCache

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

Examples:

Get the default cache store

config.default_cache

Returns:

  • (Cache)

    The default Cache instance.



74
75
76
# File 'lib/mongoid-cached-json/config.rb', line 74

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

#default_versionVersion

Returns the default JSON version

Examples:

Get the default JSON version

config.default_version

Returns:

  • (Version)

    The default JSON version.



53
54
55
56
# File 'lib/mongoid-cached-json/config.rb', line 53

def default_version
  settings[:default_version] = :unspecified unless settings.key?(:default_version)
  settings[:default_version]
end

#default_version=(default_version) ⇒ Version

Sets the default JSON version.

Examples:

Set the default version.

config.default_version = :v2

Returns:

  • (Version)

    The newly set default version.



64
65
66
# File 'lib/mongoid-cached-json/config.rb', line 64

def default_version=(default_version)
  settings[:default_version] = default_version
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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid-cached-json/config.rb', line 26

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!


103
104
105
# File 'lib/mongoid-cached-json/config.rb', line 103

def reset!
  settings.replace(defaults)
end

#transform(&block) ⇒ Object

Define a transformation on JSON data.

Examples:

Convert every string in materialized JSON to upper-case.

config.transform do |field, value|
   value.upcase
end


113
114
115
116
117
# File 'lib/mongoid-cached-json/config.rb', line 113

def transform(& block)
  settings[:transform] = [] unless settings.key?(:transform)
  settings[:transform] << block if block_given?
  settings[:transform]
end