Class: Sunrise::YmlLocale

Inherits:
Object
  • Object
show all
Defined in:
lib/sunrise/locales/yml_locale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#localeObject

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_pathObject



12
13
14
# File 'lib/sunrise/locales/yml_locale.rb', line 12

def file_path
  Rails.root.join("config/locales/#{@locale}.yml")
end

#get_dataObject



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

Returns:

  • (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