Class: PoParser::Comment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value) ⇒ Comment

Returns a new instance of Comment.



5
6
7
8
9
10
11
12
# File 'lib/poparser/comment.rb', line 5

def initialize(type, value)
  @type = type
  @value  = value

  if @type.to_s =~ /^previous_/ # these behave more like messages
    remove_empty_line
  end
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/poparser/comment.rb', line 3

def type
  @type
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/poparser/comment.rb', line 3

def value
  @value
end

Instance Method Details

#inspectObject



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

def inspect
  @value
end

#to_s(with_label = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/poparser/comment.rb', line 14

def to_s(with_label = false)
  return to_str unless with_label
  if @value.is_a? Array
    if @type.to_s =~ /^previous_/ # these behave more like messages
      string = ["#{COMMENTS_LABELS[@type]} \"\"\n"]
      @value.each do |str|
        string << "#| \"#{str}\"\n".gsub(/[\p{Blank}]+$/, '')
      end
    else
      string = []
      @value.each do |str|
        string << "#{COMMENTS_LABELS[@type]} #{str}\n".gsub(/[\p{Blank}]+$/, '')
      end
    end
    return string.join
  else
    if @type.to_s =~ /^previous_/ # these behave more like messages
      "#{COMMENTS_LABELS[@type]} \"#{@value}\"\n".gsub(/[\p{Blank}]+$/, '')
    else
      # removes the space but not newline at the end
      "#{COMMENTS_LABELS[@type]} #{@value}\n".gsub(/[\p{Blank}]+$/, '')
    end
  end
end

#to_strObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poparser/comment.rb', line 39

def to_str
  if @value.is_a?(Array)
    if @type.to_s =~ /^previous_/ # these behave more like messages
      @value.join
    else
      @value.join("\n")
    end
  else
    @value
  end
end