Class: Configure::Questioner

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

Overview

Make Questioner accesible as in: Configure::Questioner.run() “Questioner” simply starts an interactive prompt to guide the user in configuring src/config/agular_init.config.json, which is a JSON file that holds all the global configurable options (like language to use, templates, etc.)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) {|_self| ... } ⇒ Questioner

Returns a new instance of Questioner.

Yields:

  • (_self)

Yield Parameters:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ngi/configure.rb', line 69

def initialize(file)
  @file = file

  @global = @file['global']
  # TODO: extend array with this inject function?

  # The options for languages to use
  @languages = @global['languages']

  language_types = @languages.select do |type, languages|
    type if languages.size > 1
  end
  # For example, ['script','markup']
  @lang_types = language_types.collect { |type, _| type }.flatten

  # The properties in key => value format
  # of the properties the user can configure
  @configurable = @global['configurable']

  # An array of the properties that the user is allowed to configure,
  # according to src/config/angular_init.config.json
  @configurable_properties = @configurable.collect { |_k, v| v }

  yield(self) if block_given?
end

Instance Attribute Details

#configurableObject (readonly)

Returns the value of attribute configurable.



65
66
67
# File 'lib/ngi/configure.rb', line 65

def configurable
  @configurable
end

#configurable_propertiesObject (readonly)

Returns the value of attribute configurable_properties.



65
66
67
# File 'lib/ngi/configure.rb', line 65

def configurable_properties
  @configurable_properties
end

#fileObject

Returns the value of attribute file.



64
65
66
# File 'lib/ngi/configure.rb', line 64

def file
  @file
end

#globalObject (readonly)

Returns the value of attribute global.



65
66
67
# File 'lib/ngi/configure.rb', line 65

def global
  @global
end

#lang_typesObject (readonly)

Returns the value of attribute lang_types.



65
66
67
# File 'lib/ngi/configure.rb', line 65

def lang_types
  @lang_types
end

#languagesObject (readonly)

Returns the value of attribute languages.



65
66
67
# File 'lib/ngi/configure.rb', line 65

def languages
  @languages
end

Class Method Details

.run(file) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ngi/configure.rb', line 117

def self.run(file)
  questioner = Questioner.new(file) do |q|
    # First, the user chooses a property
    # to configure (from a list of available
    # properties that *can* be configured)
    property = q.choose_configurable_property

    # #configure_property spits out a hash
    # as the result
    result = q.configure_property(property)

    # The hash that was spit out as the
    # result is "merged" into the original
    # Hash from_json object that came from
    # config/angular_init.config.json
    # and is inside of this instance of Questioner
    q.file['global'][property] = result

    # This just tells the user that we were
    # successful
    result_string_hash = JSHash.new(result).to_str
    puts "#{property.capitalize} set to: #{result_string_hash}"
  end

  # Returns the file so that it can be used
  # (For example, Configure might write this
  # new hash as a JSON file to
  # config/angular_init.config.json)
  questioner.file
end

Instance Method Details

#choose_configurable_propertyObject



95
96
97
98
99
100
101
102
103
# File 'lib/ngi/configure.rb', line 95

def choose_configurable_property
  @configurable_properties.each_with_index do |p, i|
    puts "#{i + 1}) #{p.capitalize}: #{JSHash.new(@global[p]).to_str}"
  end

  valid = JSArray.new(@configurable_properties).to_str
  # return
  AskLoop.ask(check: @configurable_properties, valid: valid)
end

#configure_property(property) ⇒ Object

This method delegates to the appropriate Configurable#<method> based on the property that the user has chosen to configure Returns: a Hash of the object, based on the from_json config object from config/angular_init.config.json



110
111
112
113
114
115
# File 'lib/ngi/configure.rb', line 110

def configure_property(property)
  case property
  when @configurable['language']
    return Configurable.language(self)
  end
end