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. Rewrite this whole class. It is not working at all after first config Threads are bad. Use the Singleton library when rewriting this

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute 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.



68
69
70
# File 'lib/currency/config.rb', line 68

def float_ref_filter
  @float_ref_filter
end

#historical_table_nameObject

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



75
76
77
# File 'lib/currency/config.rb', line 75

def historical_table_name
  @historical_table_name
end

#scaleObject (readonly)

Returns the value of attribute scale.



88
89
90
# File 'lib/currency/config.rb', line 88

def scale
  @scale
end

#scale_expObject

Returns the value of attribute scale_exp.



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

def scale_exp
  @scale_exp
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

TODO: rewrite from scratch



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

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

.currentObject

Returns the current Currency::Config object If #current= has not been called and #default= has not been called, then UndefinedExchange is raised.



31
32
33
# File 'lib/currency/config.rb', line 31

def self.current
  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.



37
38
39
# File 'lib/currency/config.rb', line 37

def self.current=(x)
  self.default = 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
# File 'lib/currency/config.rb', line 17

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

.default=(x) ⇒ Object

Sets the default Currency::Config object.



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

def self.default=(x)
  @@default = x
  Currency::Currency::Factory.reset
  @@default
end