Module: Mongoid::CachedJson::Config

Extended by:
Config
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.


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

def defaults
  @defaults
end

#settingsObject

Current configuration settings.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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!

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

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

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

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