Class: Babelyoda::Strings

Inherits:
Keyset
  • Object
show all
Defined in:
lib/babelyoda/strings.rb

Instance Attribute Summary collapse

Attributes inherited from Keyset

#keys, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Keyset

#debug_counts, #drop_empty!, #empty?, #ensure_languages!, keyset_name, #merge!, #merge_key!, parse_xml, #to_s, #to_strings, #to_xml

Constructor Details

#initialize(filename, language) ⇒ Strings

Returns a new instance of Strings.



41
42
43
44
# File 'lib/babelyoda/strings.rb', line 41

def initialize(filename, language)
  super(Babelyoda::Keyset.keyset_name(filename))
  @filename, @language = filename, language
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



38
39
40
# File 'lib/babelyoda/strings.rb', line 38

def filename
  @filename
end

#languageObject (readonly)

Returns the value of attribute language.



39
40
41
# File 'lib/babelyoda/strings.rb', line 39

def language
  @language
end

Class Method Details

.save_keyset(keyset, filename, language) ⇒ Object



71
72
73
74
75
# File 'lib/babelyoda/strings.rb', line 71

def self.save_keyset(keyset, filename, language)
  strings = self.new(filename, language)
  strings.merge!(keyset)
  strings.save!
end

Instance Method Details

#readObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/babelyoda/strings.rb', line 51

def read
  if File.exist?(@filename)
    File.open(@filename, read_mode) do |f|
      lexer = StringsLexer.new
      parser = StringsParser.new(lexer, @language)
      parser.parse(f.read) do |localization_key|
        merge_key!(localization_key)
      end        
    end
  end
  self
end

#read!Object

Raises:

  • (ArgumentError)


46
47
48
49
# File 'lib/babelyoda/strings.rb', line 46

def read!
  raise ArgumentError.new("File not found: #{filename}") unless File.exist?(@filename)
  read
end

#save!Object



64
65
66
67
68
69
# File 'lib/babelyoda/strings.rb', line 64

def save!
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename, "wb") do |f|
    to_strings(f, language)
  end
end