Class: ZAML

Inherits:
Object show all
Defined in:
lib/vendor/puppet/util/zaml.rb

Defined Under Namespace

Classes: Label

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZAML

Instance Methods



62
63
64
65
66
67
68
69
# File 'lib/vendor/puppet/util/zaml.rb', line 62

def initialize
  @result = []
  @indent = nil
  @structured_key_prefix = nil
  @previously_emitted_object = {}
  @next_free_label_number = 0
  emit('--- ')
end

Class Method Details

.dump(stuff, where = '') ⇒ Object

Class Methods



53
54
55
56
57
# File 'lib/vendor/puppet/util/zaml.rb', line 53

def self.dump(stuff, where='')
  z = new
  stuff.to_zaml(z)
  where << z.to_s
end

Instance Method Details

#emit(s) ⇒ Object



144
145
146
147
# File 'lib/vendor/puppet/util/zaml.rb', line 144

def emit(s)
  @result << s
  @recent_nl = false unless s.kind_of?(Label)
end

#first_time_only(obj) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vendor/puppet/util/zaml.rb', line 122

def first_time_only(obj)
  if label = label_for(obj)
    label.this_label_number ||= (@next_free_label_number += 1)
    emit(label.reference)
  else
    with_structured_prefix(obj) do
      emit(new_label_for(obj))
      yield
    end
  end
end

#label_for(obj) ⇒ Object



112
113
114
# File 'lib/vendor/puppet/util/zaml.rb', line 112

def label_for(obj)
  @previously_emitted_object[obj.object_id]
end

#nested(tail = ' ') ⇒ Object



71
72
73
74
75
76
# File 'lib/vendor/puppet/util/zaml.rb', line 71

def nested(tail='  ')
  old_indent = @indent
  @indent = "#{@indent || "\n"}#{tail}"
  yield
  @indent = old_indent
end

#new_label_for(obj) ⇒ Object



116
117
118
119
120
# File 'lib/vendor/puppet/util/zaml.rb', line 116

def new_label_for(obj)
  label = Label.new(obj,(Hash === obj || Array === obj) ? "#{@indent || "\n"}  " : ' ')
  @previously_emitted_object[obj.object_id] = label
  label
end

#nl(s = nil) ⇒ Object



149
150
151
152
153
# File 'lib/vendor/puppet/util/zaml.rb', line 149

def nl(s = nil)
  emit(@indent || "\n") unless @recent_nl
  emit(s) if s
  @recent_nl = true
end

#prefix_structured_keys(x) ⇒ Object



159
160
161
162
163
164
# File 'lib/vendor/puppet/util/zaml.rb', line 159

def prefix_structured_keys(x)
  @structured_key_prefix = x
  yield
  nl unless @structured_key_prefix
  @structured_key_prefix = nil
end

#to_sObject



155
156
157
# File 'lib/vendor/puppet/util/zaml.rb', line 155

def to_s
  @result.join
end

#with_structured_prefix(obj) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/vendor/puppet/util/zaml.rb', line 134

def with_structured_prefix(obj)
  if @structured_key_prefix
    unless obj.is_a?(String) and obj !~ /\n/
      emit(@structured_key_prefix)
      @structured_key_prefix = nil
    end
  end
  yield
end