Class: Configure

Inherits:
Object
  • Object
show all
Defined in:
lib/ngi/configure.rb

Overview

Run the user through an interactive session of configuring ngi

Defined Under Namespace

Classes: AcceptInput, AskLoop, Configurable, Questioner, TemplateDir

Constant Summary collapse

JSer =
Utils::JSer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) {|_self| ... } ⇒ Configure

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Configure)

    the object that the method was called on



306
307
308
309
310
311
312
313
314
315
# 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

Instance Attribute Details

#fileObject

Returns the value of attribute file.



15
16
17
# File 'lib/ngi/configure.rb', line 15

def file
  @file
end

#locationObject

Returns the value of attribute location.



15
16
17
# File 'lib/ngi/configure.rb', line 15

def location
  @location
end

Class Method Details

.run(args) ⇒ Object

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



361
362
363
364
365
366
367
368
369
370
371
372
# 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

Instance Method Details

#from_ruby(args) ⇒ Object

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



336
337
338
339
340
341
342
343
# 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) ⇒ Object

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



324
325
326
327
328
329
330
331
# 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) ⇒ Object

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



347
348
349
350
351
352
# 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