Class: Financial::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/financial/locale.rb

Constant Summary collapse

LOCALES_FOLDER =
File.join(File.dirname(__FILE__), 'locales')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale_name) ⇒ Locale

Returns a new instance of Locale.



9
10
11
12
13
14
15
16
17
18
# File 'lib/financial/locale.rb', line 9

def initialize(locale_name)
  raise LocaleDontAvailable unless available_locales.include?(locale_name)
  @name = locale_name
  @file = YAML.load_file(File.join(LOCALES_FOLDER, "#{@name}.yml"))
  @locale_methods = @file['methods']
  @names = @file['names']
  @date_format = @file['date']
  @table = file['table']
  send("create_alias_methods_for_#{@name}")
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/financial/locale.rb', line 5

def file
  @file
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/financial/locale.rb', line 4

def name
  @name
end

Instance Method Details

#==(other_locale) ⇒ Object



119
120
121
# File 'lib/financial/locale.rb', line 119

def ==(other_locale)
  name.equal?(other_locale.name)
end

#add_per_cent_aliasesObject



89
90
91
92
93
# File 'lib/financial/locale.rb', line 89

def add_per_cent_aliases
  Numeric.class_eval do
    alias_method :por_cento, :per_cent
  end
end

#available_localesObject



75
76
77
# File 'lib/financial/locale.rb', line 75

def available_locales
  [:pt, :en]
end

#balance_nameObject



99
100
101
# File 'lib/financial/locale.rb', line 99

def balance_name
  @names['balance']
end

#coinObject



111
112
113
# File 'lib/financial/locale.rb', line 111

def coin
  @file['coin']
end

#create_alias_methods_for_enObject



115
116
117
# File 'lib/financial/locale.rb', line 115

def create_alias_methods_for_en
  # skip
end

#create_alias_methods_for_ptObject



79
80
81
82
83
84
85
86
87
# File 'lib/financial/locale.rb', line 79

def create_alias_methods_for_pt
  @locale_methods.each do |klass, alias_pt_methods|
    eval("Financial::#{klass}").class_eval do
      alias_pt_methods.each do |pt_method, original_method_name|
        alias_method pt_method, original_method_name
      end
    end
  end
end

#date_formatObject



54
55
56
# File 'lib/financial/locale.rb', line 54

def date_format
  @date_format['format']
end

#date_to_s(date) ⇒ Object



28
29
30
# File 'lib/financial/locale.rb', line 28

def date_to_s(date)
  right_date_format(date.day.to_s.rjust(2, '0'), date.month.to_s.rjust(2, '0'), date.year)
end

#deposit_nameObject



103
104
105
# File 'lib/financial/locale.rb', line 103

def deposit_name
  @names['deposit']
end

#final_balanceObject



36
37
38
# File 'lib/financial/locale.rb', line 36

def final_balance
  @table['final_balance']
end

#find_month_for(month, month_names) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/financial/locale.rb', line 66

def find_month_for(month, month_names)
  month_name = month_names.find { |month_name| month_name.equal?(month) }
  unless month_name
    raise MonthDontExist, "Month #{month} dont exist in locale: #{@name}. Availables months: #{month_names}"
  else
    month_names.index(month_name) + 1
  end
end

#format_coin(value) ⇒ Object



127
128
129
# File 'lib/financial/locale.rb', line 127

def format_coin(value)
  %{#{sprintf("%.2f", value.to_s).gsub(period_separator, separator)}}
end

#header_for(account, initial_date, final_date) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/financial/locale.rb', line 40

def header_for(, initial_date, final_date)
  if portuguese_language?
    initial_date = date_to_s(initial_date)
    final_date = date_to_s(final_date)
    "Nome da Conta: #{.banner} (de: #{initial_date}, até: #{final_date})"
  else
    "Account name: #{.banner} (from: #{initial_date}, to: #{final_date})"
  end
end

#initial_balanceObject



32
33
34
# File 'lib/financial/locale.rb', line 32

def initial_balance
  @table['initial_balance']
end

#month_for(month) ⇒ Object



62
63
64
# File 'lib/financial/locale.rb', line 62

def month_for(month)
  find_month_for month, month_names
end

#month_namesObject



58
59
60
# File 'lib/financial/locale.rb', line 58

def month_names
  @date_format['month_names']
end

#name_of_the_parcelObject



95
96
97
# File 'lib/financial/locale.rb', line 95

def name_of_the_parcel
  @names['parcel']
end

#period_separatorObject

I prefer to not use constants



137
138
139
# File 'lib/financial/locale.rb', line 137

def period_separator
  '.'
end

#portuguese_language?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/financial/locale.rb', line 131

def portuguese_language?
  @name.equal? :pt
end

#received_deposit_nameObject



107
108
109
# File 'lib/financial/locale.rb', line 107

def received_deposit_name
  @names['received_deposit']
end

#right_date_format(day, month, year) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/financial/locale.rb', line 20

def right_date_format(day, month, year)
  if portuguese_language?
    "#{day}/#{month}/#{year}"
  else
    "#{month}/#{day}/#{year}"
  end
end

#separatorObject

I prefer to not use constants



143
144
145
# File 'lib/financial/locale.rb', line 143

def separator
  ','
end

#table_headingsObject



50
51
52
# File 'lib/financial/locale.rb', line 50

def table_headings
  @table['headings']
end

#to_coin(value) ⇒ Object



123
124
125
# File 'lib/financial/locale.rb', line 123

def to_coin(value)
  "#{coin} #{format_coin(value)}"
end