Class: Sunrise::YmlLocale
- Inherits:
-
Object
- Object
- Sunrise::YmlLocale
- Defined in:
- lib/sunrise/locales/yml_locale.rb
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
Instance Method Summary collapse
- #file_path ⇒ Object
- #get_data ⇒ Object
-
#initialize(locale) ⇒ YmlLocale
constructor
A new instance of YmlLocale.
- #set_data(text) ⇒ Object
- #valid?(text) ⇒ Boolean
Constructor Details
#initialize(locale) ⇒ YmlLocale
Returns a new instance of YmlLocale.
8 9 10 |
# File 'lib/sunrise/locales/yml_locale.rb', line 8 def initialize(locale) @locale = locale end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
6 7 8 |
# File 'lib/sunrise/locales/yml_locale.rb', line 6 def locale @locale end |
Instance Method Details
#file_path ⇒ Object
12 13 14 |
# File 'lib/sunrise/locales/yml_locale.rb', line 12 def file_path Rails.root.join("config/locales/#{@locale}.yml") end |
#get_data ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/sunrise/locales/yml_locale.rb', line 16 def get_data text = '' f = File.open(file_path, 'r') f.each_line { |line| text += line } f.close text end |
#set_data(text) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/sunrise/locales/yml_locale.rb', line 26 def set_data(text) if valid?(text) File.open(file_path, 'w') {|f| f.write(text) } else false end end |
#valid?(text) ⇒ Boolean
34 35 36 37 38 39 40 41 |
# File 'lib/sunrise/locales/yml_locale.rb', line 34 def valid?(text) begin YAML.load(text) true rescue Exception => e false end end |