Class: Synthra::Export::JsonData

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

Overview

Exports generated data as JSON

Examples:

Export schema data as JSON

exporter = JsonData.new(schema, registry: registry, count: 100)
json = exporter.export

Instance Method Summary collapse

Constructor Details

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

Instance Method Details

#build_meta(records) ⇒ Object (private)



49
50
51
52
53
54
55
56
57
# File 'lib/synthra/export/json_data.rb', line 49

def build_meta(records)
  {
    "schema" => schema.name,
    "version" => schema.version,
    "count" => records.is_a?(Array) ? records.length : 1,
    "generated_at" => Time.now.iso8601,
    "seed" => options[:seed]
  }.compact
end

#exportObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/synthra/export/json_data.rb', line 13

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

  records = if count == 1
              [schema.generate(seed: seed, mode: mode, registry: registry)]
            else
              schema.generate_many(count, seed: seed, mode: mode, registry: registry)
            end

  if pretty
    JSON.pretty_generate(wrap_output(records))
  else
    JSON.generate(wrap_output(records))
  end
end

#wrap_output(records) ⇒ Object (private)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/synthra/export/json_data.rb', line 34

def wrap_output(records)
  # :nocov:
  if options[:wrap] == false
    records
  # :nocov:
  elsif options[:envelope]
    {
      options[:envelope] => records,
      "meta" => build_meta(records)
    }
  else
    records
  end
end