Class: Chaindrive::Config

Inherits:
Hashie::Dash
  • Object
show all
Defined in:
lib/chaindrive/config.rb

Overview

Define the basic settings for the Chaindrive runtime environment. These are directly loaded from on-disk JSON files.

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Config

Construct a JSON configuration from disk.

Parameters:

  • filename (String)

    Path to the configuration file.



11
12
13
14
# File 'lib/chaindrive/config.rb', line 11

def initialize(filename)
  # Load up the disk configuration if it exists.
  super MultiJson.load(IO.read(filename)) if File.exists?(filename)
end

Instance Method Details

#save(filename) ⇒ Object

Write the JSON configuration file to disk.

Parameters:

  • filename (String)

    Path to save configuration file.



18
19
20
21
22
23
24
25
# File 'lib/chaindrive/config.rb', line 18

def save(filename)
  # Create the configuration directory if it does not exist.
  dp = File.expand_path(File.dirname(filename))
  FileUtils.mkdir_p(dp) unless File.directory?(dp)
  fail SystemCallError, "Failed saving '#{dp}'" unless File.directory?(dp)

  IO.write(filename, MultiJson.dump(self, pretty: true))
end