Class: PoParser::Header

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

Overview

The very first entry of the PO file is considered the header

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Header

Returns a new instance of Header.



11
12
13
14
15
16
17
18
# File 'lib/poparser/header.rb', line 11

def initialize(entry)
  @entry            = entry
  @comments         = entry.translator_comment.value unless entry.translator_comment.nil?
  @original_configs = convert_msgstr_to_hash(entry.msgstr)
  @flag             = entry.flag

  define_labels_instance_variables
end

Instance Attribute Details

#charsetObject

Returns the value of attribute charset.



7
8
9
# File 'lib/poparser/header.rb', line 7

def charset
  @charset
end

#commentsObject

Returns the value of attribute comments.



7
8
9
# File 'lib/poparser/header.rb', line 7

def comments
  @comments
end

#encodingObject

Returns the value of attribute encoding.



7
8
9
# File 'lib/poparser/header.rb', line 7

def encoding
  @encoding
end

#entryObject (readonly)

Returns the value of attribute entry.



6
7
8
# File 'lib/poparser/header.rb', line 6

def entry
  @entry
end

#flagObject (readonly)

Returns the value of attribute flag.



6
7
8
# File 'lib/poparser/header.rb', line 6

def flag
  @flag
end

#languageObject

Returns the value of attribute language.



7
8
9
# File 'lib/poparser/header.rb', line 7

def language
  @language
end

#last_translatorObject

Returns the value of attribute last_translator.



7
8
9
# File 'lib/poparser/header.rb', line 7

def last_translator
  @last_translator
end

#original_configsObject (readonly)

Returns the value of attribute original_configs.



6
7
8
# File 'lib/poparser/header.rb', line 6

def original_configs
  @original_configs
end

#plural_formsObject

Returns the value of attribute plural_forms.



7
8
9
# File 'lib/poparser/header.rb', line 7

def plural_forms
  @plural_forms
end

#po_revision_dateObject

Returns the value of attribute po_revision_date.



7
8
9
# File 'lib/poparser/header.rb', line 7

def po_revision_date
  @po_revision_date
end

#pot_creation_dateObject

Returns the value of attribute pot_creation_date.



7
8
9
# File 'lib/poparser/header.rb', line 7

def pot_creation_date
  @pot_creation_date
end

#project_idObject

Returns the value of attribute project_id.



7
8
9
# File 'lib/poparser/header.rb', line 7

def project_id
  @project_id
end

#report_toObject

Returns the value of attribute report_to.



7
8
9
# File 'lib/poparser/header.rb', line 7

def report_to
  @report_to
end

#teamObject

Returns the value of attribute team.



7
8
9
# File 'lib/poparser/header.rb', line 7

def team
  @team
end

Instance Method Details

#configsObject



20
21
22
23
24
25
# File 'lib/poparser/header.rb', line 20

def configs
  configs = HEADER_LABELS.each_with_object({}) do |(k, v), hash|
    hash[v] = instance_variable_get "@#{k}".to_sym
  end
  @original_configs.merge(configs)
end

#flag_as(flag) ⇒ Object

Set flag to a custom string

Raises:

  • (ArgumentError)


43
44
45
46
47
# File 'lib/poparser/header.rb', line 43

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

  @flag = flag
end

#flag_as_fuzzyHeader

Flag the entry as Fuzzy

Returns:



37
38
39
40
# File 'lib/poparser/header.rb', line 37

def flag_as_fuzzy
  @flag = 'fuzzy'
  self
end

#fuzzy?Boolean

Checks if the entry is fuzzy

Returns:

  • (Boolean)


30
31
32
# File 'lib/poparser/header.rb', line 30

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

#inspectObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/poparser/header.rb', line 75

def inspect
  string = []
  if @comments.is_a?(Array)
    @comments.each do |comment|
      string << "# #{comment}".strip
    end
  else
    string << "# #{@comments}".strip
  end
  string << "#, #{@flag}" if @flag
  string << "msgid \"\"\nmsgstr \"\""
  configs.each do |k, v|
    next if v.nil? || v.empty?

    string << "#{k}: #{v}\n".dump
  end
  string.join("\n")
end

#to_hObject



49
50
51
# File 'lib/poparser/header.rb', line 49

def to_h
  @entry.to_h
end

#to_sObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/poparser/header.rb', line 53

def to_s
  string = []
  if @comments.is_a?(Array)
    @comments.each do |comment|
      string << "# #{comment}".strip
    end
  else
    string << "# #{@comments}".strip
  end
  string << "#, #{@flag}" if @flag
  string << "msgid \"\"\nmsgstr \"\""
  configs.each do |k, v|
    if v.nil? || v.empty?
      puts "WARNING: \"#{k}\" header field is empty and skipped"
      next
    end

    string << "#{k}: #{v}\n".dump
  end
  string.join("\n")
end