Class: Dotini::KeyValuePair

Inherits:
Object
  • Object
show all
Defined in:
lib/dotini/key_value_pair.rb

Overview

Key/value pair, with optional prepended and inline comments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeyValuePair

Creates a new, undefined key/value pair with no comments



9
10
11
12
13
14
# File 'lib/dotini/key_value_pair.rb', line 9

def initialize
  @key = nil
  @value = nil
  @prepended_comments = []
  @inline_comment = nil
end

Instance Attribute Details

#inline_commentObject

Returns the value of attribute inline_comment.



6
7
8
# File 'lib/dotini/key_value_pair.rb', line 6

def inline_comment
  @inline_comment
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/dotini/key_value_pair.rb', line 6

def key
  @key
end

#prepended_commentsObject

Returns the value of attribute prepended_comments.



6
7
8
# File 'lib/dotini/key_value_pair.rb', line 6

def prepended_comments
  @prepended_comments
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/dotini/key_value_pair.rb', line 6

def value
  @value
end

Instance Method Details

#to_sObject

Represents the key/value pair as a string



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dotini/key_value_pair.rb', line 17

def to_s
  buffer = StringIO.new
  prepended_comments.each do |line|
    buffer << line << "\n"
  end

  unless key.nil?
    buffer << "#{key} = #{value}"
    buffer <<
      if inline_comment.nil?
        "\n"
      else
        " #{inline_comment}\n"
      end
  end

  buffer.string
end