Class: Currency::Formatter::Template

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

Overview

The underlying object for Currency::Formatter#format. This object is cloned and initialized with strings created from Formatter#format. It handles the Formatter#format string interpolation.

Constant Summary collapse

@@empty_hash =
{ }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = @@empty_hash) ⇒ Template

Returns a new instance of Template.



40
41
42
43
44
45
46
# File 'lib/currency/formatter.rb', line 40

def initialize(opts = @@empty_hash)
  @template =
    @template_proc =
    nil

  opts.each_pair{ | k, v | self.send("#{k}=", v) }
end

Instance Attribute Details

#codeObject

The currency code or nil.



35
36
37
# File 'lib/currency/formatter.rb', line 35

def code
  @code
end

#currencyObject

The Currency::Currency object being formatted.



24
25
26
# File 'lib/currency/formatter.rb', line 24

def currency
  @currency
end

#fractionObject

The fraction part of the value, with decimal_separator or nil.



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

def fraction
  @fraction
end

#moneyObject

The Currency::Money object being formatted.



22
23
24
# File 'lib/currency/formatter.rb', line 22

def money
  @money
end

#signObject

The sign: ‘-’ or nil.



27
28
29
# File 'lib/currency/formatter.rb', line 27

def sign
  @sign
end

#symbolObject

The currency symbol or nil.



33
34
35
# File 'lib/currency/formatter.rb', line 33

def symbol
  @symbol
end

#templateObject

The template string.



19
20
21
# File 'lib/currency/formatter.rb', line 19

def template
  @template
end

#timeObject

The time or nil.



38
39
40
# File 'lib/currency/formatter.rb', line 38

def time
  @time
end

#wholeObject

The whole part of the value, with thousands_separator or nil.



29
30
31
# File 'lib/currency/formatter.rb', line 29

def whole
  @whole
end

Instance Method Details

#formatObject

Formats the current state using the template.



71
72
73
74
# File 'lib/currency/formatter.rb', line 71

def format
  template_proc
  _format
end

#template_proc(template = @template) ⇒ Object

Defines a the self._format template procedure using the template as a string to be interpolated.



60
61
62
63
64
65
66
67
# File 'lib/currency/formatter.rb', line 60

def template_proc(template = @template)
  return @template_proc if @template_proc
  @template_proc = template || ''
  # @template_proc = @template_proc.gsub(/[\\"']/) { | x | "\\" + x }
  @template_proc = "def self._format; \"#{@template_proc}\"; end"
  self.instance_eval @template_proc
  @template_proc
end