Class: Laser::Cutter::CLI::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/laser-cutter/cli/serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Serializer

Returns a new instance of Serializer.



9
10
11
# File 'lib/laser-cutter/cli/serializer.rb', line 9

def initialize(options = {})
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/laser-cutter/cli/serializer.rb', line 8

def options
  @options
end

Instance Method Details

#deserializeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/laser-cutter/cli/serializer.rb', line 13

def deserialize
  string = if options.read_file.eql?('-')
             $stdin.read
           elsif File.exist?(options.read_file)
             File.read(options.read_file)
           end
  if string
    options.replace(JSON.load(string))
  end
rescue Exception => e
  STDERR.puts "Error reading options from file #{options.read_file}, #{e.message}".red
  if options.verbose
    STDERR.puts e.backtrace.join("\n").red
  end
  exit 1
end

#serializeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/laser-cutter/cli/serializer.rb', line 30

def serialize
  output = if options.write_file.eql?('-')
             $stdout
           elsif options.write_file
             File.open(options.write_file, 'w')
           else
             nil
           end
  output.puts(JSON.pretty_generate(options))
  output.close if output != $stdout
rescue Exception => e
  STDERR.puts "Error writing options to file #{options.write_file}, #{e.message}".red
  if options.verbose
    STDERR.puts e.backtrace.join("\n").red
  end
  exit 1
end