Class: Synthra::CLI::Commands::Generate
- Inherits:
-
Base
- Object
- Base
- Synthra::CLI::Commands::Generate
show all
- Defined in:
- lib/synthra/cli/commands/generate.rb
Overview
Generate command - generates fake data from a schema
Instance Method Summary
collapse
Instance Method Details
#call ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/synthra/cli/commands/generate.rb', line 12
def call
schema_name = args.shift
unless schema_name
error "Usage: synthra generate <SchemaName> [options]"
return EXIT_PARSE_ERROR
end
registry = Registry.new
load_schemas_from_dir(registry, options[:dir])
schema = registry.schema(schema_name)
generate_output(schema, registry)
EXIT_SUCCESS
rescue KeyError
error "Unknown schema: #{schema_name}"
$stderr.puts "Available schemas: #{registry.schemas.keys.join(", ")}"
EXIT_PARSE_ERROR
end
|
#generate_output(schema, registry) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/synthra/cli/commands/generate.rb', line 34
def generate_output(schema, registry)
gen_options = {
seed: options[:seed],
mode: options[:mode],
registry: registry
}.compact
if options[:format] == :ndjson
schema.generate_stream(count: options[:count], **gen_options).each do |record|
puts JSON.generate(sanitize_for_json(record))
end
else
records = schema.generate_many(options[:count], **gen_options)
output = options[:count] == 1 ? records.first : records
if options[:pretty]
puts JSON.pretty_generate(sanitize_for_json(output))
else
puts JSON.generate(sanitize_for_json(output))
end
end
end
|
#sanitize_for_json(value) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/synthra/cli/commands/generate.rb', line 57
def sanitize_for_json(value)
case value
when Float
(value.infinite? || value.nan?) ? nil : value
when Hash
value.transform_values { |v| sanitize_for_json(v) }
when Array
value.map { |v| sanitize_for_json(v) }
else
value
end
end
|