Class: ConfigToolkit::OverrideReader::Visitor

Inherits:
HashArrayVisitor show all
Defined in:
lib/configtoolkit/overridereader.rb

Overview

The Visitor allows the caller to override a configuration (specified by a hash) with a second configuration (specified by a hash). The first hash is passed in through the new method, the second via the visit method. The hash passed in through the new method is directly edited, and after the visit method finishes, contains the resulting overriden hash.

The Visitor algorithm works as follows. The visit method visits all elements in the second “overriding” hash. While doing so, the Visitor keeps track of the matching containing element in the first hash (if such containing element exists), by storing this information on the stack. When an element (in the second hash) is visited, it is added to the containing element specified on the stack (replaces the element in the first hash if the parameter’s key already exists in the first hash).

Defined Under Namespace

Classes: StackFrame

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HashArrayVisitor

#leave_array, #leave_hash, #stack_empty?, #stack_top, #visit

Constructor Details

#initialize(config_hash) ⇒ Visitor

Returns a new instance of Visitor.



83
84
85
86
87
88
# File 'lib/configtoolkit/overridereader.rb', line 83

def initialize(config_hash)
  @config_hash = config_hash
  @next_container = @config_hash

  super()
end

Instance Attribute Details

#config_hashObject (readonly)

The structure currently being built.



75
76
77
# File 'lib/configtoolkit/overridereader.rb', line 75

def config_hash
  @config_hash
end

Instance Method Details

#enter_array(array) ⇒ Object



139
140
141
# File 'lib/configtoolkit/overridereader.rb', line 139

def enter_array(array)
  return StackFrame.new(@next_container)
end

#enter_hash(hash) ⇒ Object



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

def enter_hash(hash)
  return StackFrame.new(@next_container)
end

#visit_array_element(value, index, is_container) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/configtoolkit/overridereader.rb', line 143

def visit_array_element(value, index, is_container)
  converted_value = convert_value(value)
  

  if(is_container)
    existing_value = stack_top().container[index]
    if ((existing_value.class == value.class) &&
        (existing_value.class == Hash ||
         (existing_value.class == Array && 
          array_contains_container(existing_value))))
      @next_container = existing_value
      return
    end

    @next_container = converted_value
  end
  
  if (index < stack_top().container.length())
    stack_top().container[index] = converted_value
  else
    stack_top().container.push(converted_value)
  end
end

#visit_hash_element(key, value, index, is_container) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/configtoolkit/overridereader.rb', line 110

def visit_hash_element(key, value, index, is_container)
  converted_value = convert_value(value)
  
  if(is_container)
    existing_value = stack_top().container[key]
    
    if ((existing_value.class == value.class) &&
        (existing_value.class == Hash ||
         (existing_value.class == Array && 
          array_contains_container(existing_value))))
      @next_container = existing_value
      return
    end
    
    @next_container = converted_value
  end
  
  stack_top().container[key] = converted_value
end