Class: Synthra::Export::YamlData

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/export/yaml_data.rb

Overview

Exports generated data as YAML

Examples:

Export schema data as YAML

exporter = YamlData.new(schema, registry: registry, count: 10)
yaml = exporter.export

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Synthra::Export::Base

Instance Method Details

#exportObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/synthra/export/yaml_data.rb', line 15

def export
  count = options[:count] || 10
  seed = options[:seed]
  mode = options[:mode] || :random

  records = if count == 1
              schema.generate(seed: seed, mode: mode, registry: registry) # single record
            else
              schema.generate_many(count, seed: seed, mode: mode, registry: registry)
            end

  output = wrap_output(records)

  # Add header comment
  lines = []
  lines << "# Generated from Synthra schema: #{schema.name}"
  lines << "# Version: #{schema.version}" if schema.version
  lines << "# Records: #{count}"
  lines << "# Generated at: #{Time.now.iso8601}"
  lines << ""
  lines << YAML.dump(output)

  lines.join("\n")
end

#wrap_output(records) ⇒ Object (private)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/synthra/export/yaml_data.rb', line 42

def wrap_output(records)
  # :nocov:
  if options[:wrap] == false
    records
  # :nocov:
  elsif options[:root_key]
    { options[:root_key] => records }
  else
    records
  end
end