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: AskLoop, Configurable, Questioner

Constant Summary collapse

Utils =
::Utils
JSArray =
Utils::JSArray
JSHash =
Utils::JSHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) {|_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



152
153
154
155
156
157
# File 'lib/ngi/configure.rb', line 152

def initialize(location)
  @location = location
  @file = IO.read(@location)

  yield(self) if block_given?
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#locationObject

Returns the value of attribute location.



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

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ngi/configure.rb', line 192

def self.run(args)
  Configure.new(args[:file_path]) do |c|
    # Get the JSON file as a Ruby Hash
    json_file = c.from_json

    # Pass it off to Questioner so that
    # we can interact with the user and
    # have the user configure the Hash.
    # Then, we set the Configure file
    # to the output of however the user
    # configured it with Questioner
    c.file = Configure::Questioner.run(json_file)

    # We'll write the hash to
    # config/angular_init.config.json.
    # Configure#write converts the Hash
    # to JSON and then uses File.write
    c.write if args[:write] == true
  end
end

Instance Method Details

#from_jsonObject

Convert the file to a Ruby hash



160
161
162
# File 'lib/ngi/configure.rb', line 160

def from_json
  JSON.parse(@file)
end

#to_jsonObject

Generate a “prettified” JSON version of our newly updated Ruby hash with the user’s options



166
167
168
169
170
171
172
# File 'lib/ngi/configure.rb', line 166

def to_json
  if @file.is_a? Hash
    JSON.pretty_generate(@file)
  else
    @file
  end
end

#writeObject

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



176
177
178
179
180
181
182
183
# File 'lib/ngi/configure.rb', line 176

def write
  new_file = to_json

  File.open(@location, 'w') do |f|
    f.write(new_file)
    f.close
  end
end