Class: Laser::Cutter::CLI::ConfigFile
- Inherits:
-
Object
- Object
- Laser::Cutter::CLI::ConfigFile
- Defined in:
- lib/laser/cutter/cli/config_file.rb
Overview
A configuration saved as JSON. The path '-' stands for STDIN when reading, and for the command's own output when writing.
Constant Summary collapse
- STDIO =
The path that stands for STDIN or the command's output.
'-'
Instance Method Summary collapse
-
#initialize(path) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #read ⇒ Hash{String => Object}
- #write(settings, out) ⇒ void
Constructor Details
#initialize(path) ⇒ ConfigFile
Returns a new instance of ConfigFile.
13 14 15 |
# File 'lib/laser/cutter/cli/config_file.rb', line 13 def initialize(path) @path = path end |
Instance Method Details
#read ⇒ Hash{String => Object}
19 20 21 22 23 |
# File 'lib/laser/cutter/cli/config_file.rb', line 19 def read JSON.parse(@path == STDIO ? $stdin.read : File.read(@path)) rescue SystemCallError, JSON::ParserError => e raise Error, "cannot read the configuration from #{@path}: #{e.}" end |
#write(settings, out) ⇒ void
This method returns an undefined value.
29 30 31 32 33 34 |
# File 'lib/laser/cutter/cli/config_file.rb', line 29 def write(settings, out) json = JSON.pretty_generate(settings) @path == STDIO ? out.puts(json) : File.write(@path, "#{json}\n") rescue SystemCallError => e raise Error, "cannot write the configuration to #{@path}: #{e.}" end |