class Configure

Run the user through an interactive session of configuring ngi

Constants

JSer

Attributes

file[RW]
location[RW]

Private Class Methods

new(location = nil) { |self| ... } click to toggle source

The only thing we do here is load the JSON config file basically as just a string in JSON format. It will be converted to a Ruby hash in from_json below

# File lib/ngi/configure.rb, line 306
def initialize(location = nil)
  unless location.nil?
    @location = location
    f = File.open(@location, 'r')
    @file = f.read
    f.close
  end

  yield(self) if block_given?
end
run(args) click to toggle source

All this method does is handle retrieving the file from config/angular_init.config.json so that it can pass it off to Questioner, which can in turn change the Hash and pass it back to Configure, which can then choose to actually write the file in JSON format to config/angular_init.config.json

# File lib/ngi/configure.rb, line 361
def self.run(args)
  Configure.new do |c|
    c.file = Configure::Questioner.run(args)

    if args[:write] == true
      c.write(
        destination: args[:destination],
        file: c.from_ruby(to: args[:to])
      )
    end
  end
end

Private Instance Methods

from_ruby(args) click to toggle source

Convert the file from a Ruby hash into whatever language is specified Usage: <Configure instance>.from_ruby(to: 'yaml')

# File lib/ngi/configure.rb, line 336
def from_ruby(args)
  case args[:to]
  when 'json'
    JSON.pretty_generate(@file)
  when 'yaml'
    @file.to_yaml
  end
end
to_ruby(args) click to toggle source

Convert the file to a Ruby hash Usage: Configure.new('file/path').to_ruby(from: 'yaml')

# File lib/ngi/configure.rb, line 324
def to_ruby(args)
  case args[:from]
  when 'json'
    JSON.parse(@file)
  when 'yaml'
    YAML.load(@file)
  end
end
write(args) click to toggle source

Here we actually write the new JSON config file to config/angular_init.config.json

# File lib/ngi/configure.rb, line 347
def write(args)
  File.open(args[:destination], 'w') do |f|
    f.write(args[:file])
    f.close
  end
end