Class: Exchange::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/exchange/configuration.rb

Overview

A configuration class that stores the configuration of the gem. It allows to set the api from which the data gets retrieved, the cache in which the data gets cached, the regularity of updates for the currency rates, how many times the api calls should be retried on failure, and wether operations mixing currencies should raise errors or not

Author:

  • Beat Richartz

Since:

  • 0.1

Version:

  • 0.6

Constant Summary collapse

DEFAULTS =

The configuration defaults

Since:

  • 0.6

Version:

  • 1.0

{ 
  :api => {
    :subclass => ExternalAPI::XavierMedia, 
    :retries => 7,
    :protocol => :http,
    :app_id => nil,
    :fallback => ExternalAPI::Ecb
  },
  :cache => {
    :subclass => Cache::Memory,
    :expire => :daily,
    :path => nil,
    :host => nil,
    :port => nil
  },
  :implicit_conversions => true
}

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}, &block) ⇒ Configuration

Initialize a new configuration. Takes a hash and/or a block. Lets you easily set the configuration the way you want it to be

Examples:

Define the configuration with a hash

Exchange::Configuration.new(:implicit_conversions => false, :api => {:subclass => :open_exchange_rates, :retries => 2})

Define the configuration with a block

Exchange::Configuration.new do |c|
  c.implicit_conversions = false
  c.cache = {
    :subclass => Exhange::Cache::Redis,
    :expire => :hourly
  }

Parameters:

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

    The configuration as a hash

  • block (Proc)

    A block to yield the configuration with

Since:

  • 0.6

Version:

  • 0.6



107
108
109
110
111
# File 'lib/exchange/configuration.rb', line 107

def initialize configuration={}, &block
  @config = DEFAULTS.merge(configuration)
  self.instance_eval(&block) if block_given?
  super()
end

Instance Method Details

#apiExchange::ExternalAPI::Configuration

Getter for the api configuration. Instantiates the configuration as an open struct, if called for the first time. Also camelizes and constantizes the api subclass, if used for the first time.

Returns:

Since:

  • 0.1



173
# File 'lib/exchange/configuration.rb', line 173

install_getter :api

#cacheExchange::Cache::Configuration

Getter for the cache configuration. Instantiates the configuration as an open struct, if called for the first time. Also camelizes and constantizes the cache subclass, if used for the first time.

Returns:

Since:

  • 0.1



179
# File 'lib/exchange/configuration.rb', line 179

install_getter :cache

#implicit_conversionsBoolean

Getter for the implicit Conversions configuration. If set to true, implicit conversions will not raise errors If set to false, implicit conversions will raise errors

Returns:

  • (Boolean)

    True if implicit conversions are allowed, false if not

Since:

  • 0.6

Version:

  • 0.6



129
130
131
# File 'lib/exchange/configuration.rb', line 129

def implicit_conversions
  @config[:implicit_conversions]
end

#implicit_conversions=(data) ⇒ Boolean

Setter for the implicit conversions configuration. If set to true, implicit conversions will not raise errors If set to false, implicit conversions will raise errors

Parameters:

  • data (Boolean)

    The configuration to set

Returns:

  • (Boolean)

    The configuration set

Since:

  • 0.6

Version:

  • 0.6



140
141
142
# File 'lib/exchange/configuration.rb', line 140

def implicit_conversions= data
  @config[:implicit_conversions] = data
end

#resetObject

Allows to reset the configuration to the defaults

Since:

  • 0.9

Version:

  • 0.9



117
118
119
120
121
# File 'lib/exchange/configuration.rb', line 117

def reset
  api.reset
  cache.reset
  self.implicit_conversions = DEFAULTS[:implicit_conversions]
end