Class: ConfigToolkit::KeyValueReader::HashEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/configtoolkit/keyvaluereader.rb

Overview

This class is not meant to be accessed by KeyValueReader users; it solely is to be used internally.

This class represents a hash entry while the entry is being parsed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_value_delimiter) ⇒ HashEntry

Description:

This initializes an empty instance (empty key and empty value). key_value_delimiter is the String that the instance will search for in order to know when to start appending characters to the value rather than to the key.

Parameters:

key_value_delimiter

The String that will separate the key from the value



119
120
121
122
123
124
125
# File 'lib/configtoolkit/keyvaluereader.rb', line 119

def initialize(key_value_delimiter)
  @key = ""
  @value = ""
  @key_value_delimiter = key_value_delimiter
  @curr_delimiter = ""
  @finished_parsing_key = false
end

Instance Attribute Details

#keyObject (readonly)

:nodoc:



105
106
107
# File 'lib/configtoolkit/keyvaluereader.rb', line 105

def key
  @key
end

#valueObject

Returns the value of attribute value.



106
107
108
# File 'lib/configtoolkit/keyvaluereader.rb', line 106

def value
  @value
end

Instance Method Details

#<<(char) ⇒ Object

Description:

This method appends char to the hash entry. This method will figure out whether char should be appended to the key or the to value, depending on whether the key-value delimiter has been appended yet.

Parameters:

char

The character (String) to append



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/configtoolkit/keyvaluereader.rb', line 137

def <<(char)
  #
  # If still parsing the key, check whether the new character matches
  # the next unmatched character in the delimiter.  If it does, then
  # do not append the character to the key (yet), because the character
  # may be part of the delimiter.  If the all characters in the delimiter
  # have been matched, then switch to parsing the value; otherwise,
  # return in order to get the next character.
  #
  # If the new character does not match the next character in
  # the delimiter, then append any prior characters that did match to the 
  # key since they now have been proven not to be part of the delimiter.
  #
  if(!@finished_parsing_key)
    if(char == @key_value_delimiter[@curr_delimiter.size(), 1])
      @curr_delimiter << char

      if(@curr_delimiter.size() == @key_value_delimiter.size())
        @finished_parsing_key = true
      end
    else
      if(!@curr_delimiter.empty?())
        @key << @curr_delimiter
        @curr_delimiter = ""
      end

      @key << char
    end
  else
    @value << char
  end
end