Class: Synthra::Export::JsonData
- Inherits:
-
Base
- Object
- Base
- Synthra::Export::JsonData
show all
- Defined in:
- lib/synthra/export/json_data.rb
Overview
Exports generated data as JSON
Instance Method Summary
collapse
Instance Method Details
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
|
#export ⇒ Object
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
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)
if options[:wrap] == false
records
elsif options[:envelope]
{
options[:envelope] => records,
"meta" => build_meta(records)
}
else
records
end
end
|