Class: Financial::Locale
- Inherits:
-
Object
- Object
- Financial::Locale
- Defined in:
- lib/financial/locale.rb
Constant Summary collapse
- LOCALES_FOLDER =
File.join(File.dirname(__FILE__), 'locales')
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #==(other_locale) ⇒ Object
- #add_per_cent_aliases ⇒ Object
- #available_locales ⇒ Object
- #balance_name ⇒ Object
- #coin ⇒ Object
- #create_alias_methods_for_en ⇒ Object
- #create_alias_methods_for_pt ⇒ Object
- #date_format ⇒ Object
- #date_to_s(date) ⇒ Object
- #deposit_name ⇒ Object
- #final_balance ⇒ Object
- #find_month_for(month, month_names) ⇒ Object
- #format_coin(value) ⇒ Object
- #header_for(account, initial_date, final_date) ⇒ Object
- #initial_balance ⇒ Object
-
#initialize(locale_name) ⇒ Locale
constructor
A new instance of Locale.
- #month_for(month) ⇒ Object
- #month_names ⇒ Object
- #name_of_the_parcel ⇒ Object
-
#period_separator ⇒ Object
I prefer to not use constants.
- #portuguese_language? ⇒ Boolean
- #received_deposit_name ⇒ Object
- #right_date_format(day, month, year) ⇒ Object
-
#separator ⇒ Object
I prefer to not use constants.
- #table_headings ⇒ Object
- #to_coin(value) ⇒ Object
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/financial/locale.rb', line 5 def file @file end |
#name ⇒ Object
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_aliases ⇒ Object
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_locales ⇒ Object
75 76 77 |
# File 'lib/financial/locale.rb', line 75 def available_locales [:pt, :en] end |
#balance_name ⇒ Object
99 100 101 |
# File 'lib/financial/locale.rb', line 99 def balance_name @names['balance'] end |
#coin ⇒ Object
111 112 113 |
# File 'lib/financial/locale.rb', line 111 def coin @file['coin'] end |
#create_alias_methods_for_en ⇒ Object
115 116 117 |
# File 'lib/financial/locale.rb', line 115 def create_alias_methods_for_en # skip end |
#create_alias_methods_for_pt ⇒ Object
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_format ⇒ Object
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_name ⇒ Object
103 104 105 |
# File 'lib/financial/locale.rb', line 103 def deposit_name @names['deposit'] end |
#final_balance ⇒ Object
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(account, initial_date, final_date) if portuguese_language? initial_date = date_to_s(initial_date) final_date = date_to_s(final_date) "Nome da Conta: #{account.} (de: #{initial_date}, até: #{final_date})" else "Account name: #{account.} (from: #{initial_date}, to: #{final_date})" end end |
#initial_balance ⇒ Object
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_names ⇒ Object
58 59 60 |
# File 'lib/financial/locale.rb', line 58 def month_names @date_format['month_names'] end |
#name_of_the_parcel ⇒ Object
95 96 97 |
# File 'lib/financial/locale.rb', line 95 def name_of_the_parcel @names['parcel'] end |
#period_separator ⇒ Object
I prefer to not use constants
137 138 139 |
# File 'lib/financial/locale.rb', line 137 def period_separator '.' end |
#portuguese_language? ⇒ Boolean
131 132 133 |
# File 'lib/financial/locale.rb', line 131 def portuguese_language? @name.equal? :pt end |
#received_deposit_name ⇒ Object
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 |
#separator ⇒ Object
I prefer to not use constants
143 144 145 |
# File 'lib/financial/locale.rb', line 143 def separator ',' end |
#table_headings ⇒ Object
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 |