Class: Currency::Config

Inherits:
Object show all
Defined in:
lib/currency/config.rb

Overview

The Currency::Config class is responsible for maintaining global configuration for the Currency package.

TO DO:

Migrate all class variable configurations to this object.

Constant Summary collapse

@@default =
nil
@@identity =

:nodoc:

Proc.new { |x| x }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#historical_table_nameObject

Defines the table name for Historical::Rate records. Defaults to ‘currency_historical_rates’.



85
86
87
# File 'lib/currency/config.rb', line 85

def historical_table_name
  @historical_table_name
end

Class Method Details

.configure(&blk) ⇒ Object

Clones the current configuration and makes it current during the execution of a block. After block completes, the previous configuration is restored.

Currency::Config.configure do | c |
  c.float_ref_filter = Proc.new { | x | x.round }
  "123.448".money.rep == 12345
end


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/currency/config.rb', line 52

def self.configure(&blk)
  c_prev = current
  c_new = self.current = current.clone
  result = nil
  begin
    result = yield c_new
  ensure
    self.current = c_prev
  end
  result
end

.currentObject

Returns the current Currency::Config object used during in the current thread.

If #current= has not been called and #default= has not been called, then UndefinedExchange is raised.



32
33
34
35
36
# File 'lib/currency/config.rb', line 32

def self.current
  Thread.current[:Currency__Config] ||= 
    self.default || 
    (raise ::Currency::Exception::UndefinedConfig, "Currency::Config.default not defined")
end

.current=(x) ⇒ Object

Sets the current Currency::Config object used in the current thread.



40
41
42
# File 'lib/currency/config.rb', line 40

def self.current=(x)
  Thread.current[:Currency__Config] = x
end

.defaultObject

Returns the default Currency::Config object.

If one is not specfied an instance is created. This is a global, not thread-local.



17
18
19
20
# File 'lib/currency/config.rb', line 17

def self.default 
  @@default ||= 
    self.new
end

.default=(x) ⇒ Object

Sets the default Currency::Config object.



23
24
25
# File 'lib/currency/config.rb', line 23

def self.default=(x)
  @@default = x
end

Instance Method Details

#float_ref_filterObject

Returns the current Float conversion filter. Can be used to set rounding or truncation policies when converting Float values to Money values. Defaults to an identity function. See Float#Money_rep.



72
73
74
75
# File 'lib/currency/config.rb', line 72

def float_ref_filter
  @float_ref_filter ||= 
    @@identity
end

#float_ref_filter=(x) ⇒ Object

Sets the current Float conversion filter.



78
79
80
# File 'lib/currency/config.rb', line 78

def float_ref_filter=(x)
  @float_ref_filter = x
end