Class: ConfigToolkit::KeyValueWriter::Visitor

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

Overview

This class is used for printing nested elements of the structure passed to write. See the comments for HashArrayVisitor for details on how this visitor works.

Instance Method Summary collapse

Methods inherited from HashArrayVisitor

#stack_empty?, #stack_top, #visit

Constructor Details

#initialize(stream, config) ⇒ Visitor

Description:

This constructs a Visitor to write structures in key-value format to stream stream.

Parameters:

stream

The stream to which to write

config

The KeyValueConfig that defines the format of the key-value output that this instance can write



68
69
70
71
72
73
# File 'lib/configtoolkit/keyvaluewriter.rb', line 68

def initialize(stream, config)
  @stream = stream
  @config = config

  super()
end

Instance Method Details

#enter_array(array) ⇒ Object



97
98
99
# File 'lib/configtoolkit/keyvaluewriter.rb', line 97

def enter_array(array)
  @stream << @config.array_opening
end

#enter_hash(hash) ⇒ Object



75
76
77
78
79
# File 'lib/configtoolkit/keyvaluewriter.rb', line 75

def enter_hash(hash)
  @stream << @config.hash_opening

  return nil
end

#leave_array(popped_stack_frame) ⇒ Object



111
112
113
# File 'lib/configtoolkit/keyvaluewriter.rb', line 111

def leave_array(popped_stack_frame)
  @stream << @config.array_closing
end

#leave_hash(popped_stack_frame) ⇒ Object



81
82
83
# File 'lib/configtoolkit/keyvaluewriter.rb', line 81

def leave_hash(popped_stack_frame)
  @stream << @config.hash_closing
end

#visit_array_element(value, index, is_container) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/configtoolkit/keyvaluewriter.rb', line 101

def visit_array_element(value, index, is_container)
  if(index != 0)
    @stream << "#{@config.container_element_delimiter} "
  end

  if(!is_container)
    @stream << "#{value}"
  end
end

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/configtoolkit/keyvaluewriter.rb', line 85

def visit_hash_element(key, value, index, is_container)
  if(index != 0)
    @stream << "#{@config.container_element_delimiter} "
  end

  @stream << "#{key} #{@config.hash_key_value_delimiter} "

  if(!is_container)
    @stream << "#{value}"
  end
end