Class: PoParser::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/poparser/entry.rb

Overview

A single translation entity in a PO file

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Entry

rubocop:disable Metrics/ClassLength



6
7
8
9
10
11
12
13
14
15
# File 'lib/poparser/entry.rb', line 6

def initialize(args = {})
  # Defining all instance variables to prevent warnings
  define_labels_instance_variables
  define_args_instance_variables(args)
  define_writer_methods(COMMENTS_LABELS, 'Comment')
  define_writer_methods(ENTRIES_LABELS, 'Message')
  define_reader_methods
  define_aliases
  define_obsolete_instance_variables
end

Instance Method Details

#flag_as(flag) ⇒ Object

Set flag to a custom string

Raises:

  • (ArgumentError)


73
74
75
76
77
# File 'lib/poparser/entry.rb', line 73

def flag_as(flag)
  raise ArgumentError if flag.class != String

  @flag = flag
end

#flag_as_fuzzyEntry

Flag the entry as Fuzzy

Returns:



67
68
69
70
# File 'lib/poparser/entry.rb', line 67

def flag_as_fuzzy
  @flag = 'fuzzy'
  self
end

#fuzzy?Boolean

Checks if the entry is fuzzy

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/poparser/entry.rb', line 58

def fuzzy?
  return false if obsolete?

  @flag.to_s.match?('fuzzy') ? true : false
end

#inspectObject



111
112
113
# File 'lib/poparser/entry.rb', line 111

def inspect
  to_s
end

#obsolete?Boolean Also known as: cached?

If entry doesn't have any msgid, it's probably a obsolete entry that is kept by the program for later use. These entries will usually start with: #~

Returns:

  • (Boolean)


22
23
24
# File 'lib/poparser/entry.rb', line 22

def obsolete?
  !@obsolete.nil?
end

#plural?Boolean

Checks if the entry is plural

Returns:

  • (Boolean)


51
52
53
# File 'lib/poparser/entry.rb', line 51

def plural?
  @msgid_plural != nil
end

#to_hHash

Convert entry to a hash key value

Returns:

  • (Hash)


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/poparser/entry.rb', line 82

def to_h
  instance_variables.each_with_object({}) do |label, hash|
    object = instance_variable_get(label)
    # If it's a plural msgstr
    if object.is_a?(Array)
      object.each do |entry|
        hash[entry.type] = entry.to_s unless entry.nil?
      end
    else
      hash[object.type] = object.to_s unless object.nil?
    end
  end
end

#to_sString

Convert entry to a string

Returns:

  • (String)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/poparser/entry.rb', line 99

def to_s
  LABELS.each_with_object([]) do |label, arr|
    object = instance_variable_get("@#{label}".to_sym)
    # If it's a plural msgstr
    if object.is_a?(Array)
      arr.push(*object.map { |entry| entry.to_s(true) }.compact)
    else
      arr << object.to_s(true) unless object.nil?
    end
  end.join
end

#translated?Boolean Also known as: complete?

Checks if the entry is translated

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/poparser/entry.rb', line 41

def translated?
  return false if obsolete? || fuzzy?

  !untranslated?
end

#untranslated?Boolean Also known as: incomplete?

Checks if the entry is untraslated

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/poparser/entry.rb', line 30

def untranslated?
  return false if obsolete? || fuzzy?
  return @msgstr.map(&:str).join.empty? if @msgstr.is_a? Array

  @msgstr.nil? || @msgstr.str.empty?
end