Class: PutText::POFile

Inherits:
Object
  • Object
show all
Defined in:
lib/puttext/po_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries) ⇒ POFile

Create a new POFile

Parameters:

  • entries (Array<POEntry>)

    an array of POEntry objects, that should be placed in this file.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puttext/po_file.rb', line 13

def initialize(entries)
  @entries = entries
  @header_entry = POEntry.new(
    flags: ['fuzzy'],
    msgid: '',
    msgstr: <<-STRING.unindent
      POT-Creation-Date: #{Time.now.strftime('%Y-%m-%d %H:%M%z')}
      MIME-Version: 1.0
      Content-Type: text/plain; charset=UTF-8
    STRING
  )
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



8
9
10
# File 'lib/puttext/po_file.rb', line 8

def entries
  @entries
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
# File 'lib/puttext/po_file.rb', line 55

def ==(other)
  @entries.sort == other.entries.sort
end

#merge(other_file) ⇒ Object

Merge the contents of another POFile to this POFile.

Parameters:

  • other_file (POFile)

    the file to merge the contents of to this file.



47
48
49
50
51
52
53
# File 'lib/puttext/po_file.rb', line 47

def merge(other_file)
  unless other_file.is_a?(POFile)
    raise ArgumentError, 'argument must be a PutText::POFile'
  end

  @entries += other_file.entries
end

#to_sObject



26
27
28
29
30
# File 'lib/puttext/po_file.rb', line 26

def to_s
  str_io = StringIO.new
  write_to(str_io)
  str_io.string
end

#write_to(io) ⇒ Object

Write the contents of this file to the specified IO object.

Parameters:

  • io (IO)

    the IO object to write the contents of the file to.



34
35
36
37
38
39
40
41
42
43
# File 'lib/puttext/po_file.rb', line 34

def write_to(io)
  deduplicate

  io.write(@header_entry.to_s)

  @entries.each do |entry|
    io.write("\n")
    io.write(entry.to_s)
  end
end