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.



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

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

  # these behave more like messages
  remove_empty_line if /^previous_/.match?(@type.to_s)
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/poparser/comment.rb', line 5

def type
  @type
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/poparser/comment.rb', line 5

def value
  @value
end

Instance Method Details

#inspectObject



53
54
55
# File 'lib/poparser/comment.rb', line 53

def inspect
  @value
end

#to_s(with_label = false) ⇒ Object



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

def to_s(with_label = false)
  return to_str unless with_label

  if @value.is_a? Array
    if /^previous_/.match?(@type.to_s) # 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 /^previous_/.match?(@type.to_s) # 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



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

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