Class: Vial::YamlEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/vial/yaml_emitter.rb

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ YamlEmitter

Returns a new instance of YamlEmitter.



7
8
9
# File 'lib/vial/yaml_emitter.rb', line 7

def initialize(records)
  @records = records
end

Instance Method Details

#to_yamlObject



11
12
13
14
15
# File 'lib/vial/yaml_emitter.rb', line 11

def to_yaml
  buffer = StringIO.new
  write_to(buffer)
  buffer.string
end

#write_to(io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vial/yaml_emitter.rb', line 17

def write_to(io)
  @records.each do |record|
    io << "#{record.fixture_label}:\n"
    if record.attributes.empty?
      io << "  {}\n"
      next
    end

    record.attributes.each do |key, value|
      formatted = format_value(value)
      if formatted.is_a?(Array)
        io << "  #{key}:\n"
        formatted.each do |line|
          io << "    #{line}\n"
        end
      else
        io << "  #{key}: #{formatted}\n"
      end
    end
  end
end