ngi 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +7 -1
  3. data/bin/ngi +1 -31
  4. data/lib/config/{angular_init.config.json → backup/angular_init.config.json} +0 -0
  5. data/lib/config/backup/config.components.aliases.yml +3 -0
  6. data/lib/config/backup/config.components.yml +55 -0
  7. data/lib/config/backup/config.configurable.yml +1 -0
  8. data/lib/config/backup/config.languages.yml +5 -0
  9. data/lib/config/backup/config.yml +3 -0
  10. data/lib/config/backup/template-skeleton.js +93 -0
  11. data/lib/config/config.components.aliases.yml +3 -0
  12. data/lib/config/config.components.yml +55 -0
  13. data/lib/config/config.configurable.yml +2 -0
  14. data/lib/config/config.languages.yml +5 -0
  15. data/lib/config/config.yml +4 -0
  16. data/lib/ngi.rb +4 -7
  17. data/lib/ngi/configure.rb +236 -75
  18. data/lib/ngi/delegate.rb +3 -8
  19. data/lib/ngi/generator.rb +74 -46
  20. data/lib/ngi/parser.rb +97 -0
  21. data/lib/{templates → ngi/templates}/markup/html/default/index.html +0 -0
  22. data/lib/{templates → ngi/templates}/script/coffee/default/basic.js +0 -0
  23. data/lib/{templates → ngi/templates}/script/coffee/default/config.js +0 -0
  24. data/lib/{templates → ngi/templates}/script/coffee/default/constant.js +0 -0
  25. data/lib/{templates → ngi/templates}/script/coffee/default/module.js +0 -0
  26. data/lib/{templates → ngi/templates}/script/es5/default/basic.js +0 -0
  27. data/lib/{templates → ngi/templates}/script/es5/default/config.js +0 -0
  28. data/lib/{templates → ngi/templates}/script/es5/default/constant.js +0 -0
  29. data/lib/{templates → ngi/templates}/script/es5/default/module.js +0 -0
  30. data/lib/ngi/utils/command_parser.rb +90 -75
  31. data/lib/ngi/utils/current_dir.rb +20 -0
  32. data/lib/ngi/utils/jser.rb +45 -32
  33. data/lib/ngi/utils/user_input.rb +22 -0
  34. data/lib/ngi/utils/utils.rb +4 -21
  35. data/lib/ngi/version.rb +1 -1
  36. data/ngi.gemspec +2 -1
  37. data/test/config/config.components.aliases.yml +3 -0
  38. data/test/config/config.components.yml +55 -0
  39. data/test/config/config.configurable.yml +2 -0
  40. data/test/config/config.languages.yml +5 -0
  41. data/test/config/config.yml +4 -0
  42. data/test/finished_files/script/coffee/directive.finished.coffee +20 -0
  43. data/test/finished_files/script/es5/controller.finished.js +13 -0
  44. data/test/finished_files/script/es5/directive.finished.js +25 -0
  45. data/test/finished_files/script/es5/filter.finished.js +18 -0
  46. data/test/templates/markup/html/default/index.html +11 -0
  47. data/test/templates/script/coffee/default/basic.js +23 -0
  48. data/test/templates/script/coffee/default/config.js +12 -0
  49. data/test/templates/script/coffee/default/constant.js +7 -0
  50. data/test/templates/script/coffee/default/module.js +7 -0
  51. data/test/templates/script/es5/default/basic.js +29 -0
  52. data/test/templates/script/es5/default/config.js +13 -0
  53. data/test/templates/script/es5/default/constant.js +7 -0
  54. data/test/templates/script/es5/default/module.js +7 -0
  55. data/test/test_ngi.rb +274 -56
  56. data/test/testing_utils.rb +26 -0
  57. metadata +78 -36
  58. data/lib/dep/json.rb +0 -62
  59. data/lib/dep/json/add/bigdecimal.rb +0 -28
  60. data/lib/dep/json/add/complex.rb +0 -28
  61. data/lib/dep/json/add/core.rb +0 -11
  62. data/lib/dep/json/add/date.rb +0 -34
  63. data/lib/dep/json/add/date_time.rb +0 -50
  64. data/lib/dep/json/add/exception.rb +0 -31
  65. data/lib/dep/json/add/ostruct.rb +0 -31
  66. data/lib/dep/json/add/range.rb +0 -29
  67. data/lib/dep/json/add/rational.rb +0 -27
  68. data/lib/dep/json/add/regexp.rb +0 -30
  69. data/lib/dep/json/add/struct.rb +0 -30
  70. data/lib/dep/json/add/symbol.rb +0 -25
  71. data/lib/dep/json/add/time.rb +0 -38
  72. data/lib/dep/json/common.rb +0 -484
  73. data/lib/dep/json/ext.rb +0 -21
  74. data/lib/dep/json/ext/.keep +0 -0
  75. data/lib/dep/json/generic_object.rb +0 -70
  76. data/lib/dep/json/pure.rb +0 -21
  77. data/lib/dep/json/pure/generator.rb +0 -522
  78. data/lib/dep/json/pure/parser.rb +0 -359
  79. data/lib/dep/json/version.rb +0 -8
  80. data/test/sup/test.config.json +0 -100
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a59338ccc566495d2d77474f11a156be3eb2f94
4
- data.tar.gz: 7981be7a346fb035357d197964f8e798612414cb
3
+ metadata.gz: bbad7c67486f004d161fbd0edbe47ec93bf3ffb3
4
+ data.tar.gz: ae9b49af3939e3805550532f6d5347d85218bb77
5
5
  SHA512:
6
- metadata.gz: ae746334de0c717fb963b9af9bd41757bed32be13a7e4e5a50676141ea55c1de45d6e1da4bb506eeef3d173cc5d30f3bf7dac900304a1181a1cb87bebddf4ed6
7
- data.tar.gz: bf02f0b2d863130e0f7c2e790f1af3348c0d1f47a1bf85ba28b28e86ff7d8e037ee5dfc0ba4889b0506cc2e8ebc9d0bec64baa790ab67a8f1a82b24fbc400d81
6
+ metadata.gz: 017f5aa9bb956fb047eec65b14fde636a8e29c0a17a0feef887d22c5dfe5c8bbd49a8928c2244fb057b6d53ff460c183abc6731f204d7ebc6961efb418ebb486
7
+ data.tar.gz: 1772205977bb7e65530f2e735b4de58f92246ba1e376251d4023e41f2147ea64b77fee18227f11838d93670cfb148caa5a3997b080dc1090357d572a7c0ea44d
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ # require 'rake/testtask'
3
+ # require 'memfs'
2
4
 
5
+ # Rake::TestTask.new do |t|
6
+ # t.libs << 'test'
7
+ # t.test_files = FileList['test/test*.rb']
8
+ # end
data/bin/ngi CHANGED
@@ -5,36 +5,6 @@
5
5
  # github.com/joshbeam/angular_init
6
6
  # MIT License
7
7
 
8
- # Ngi is the main module (src/ruby/angular_init.rb)
9
8
  require_relative '../lib/ngi'
10
- require_relative '../lib/ngi/utils/utils'
11
9
 
12
- # This class is just a wrapper for the main process
13
- # of ngi
14
- class Parser
15
- CURRENT_DIR = File.dirname(__FILE__)
16
- def self.parse(args)
17
- p = Utils::CommandParser.new do |parser|
18
- config_file = "#{CURRENT_DIR}/../lib/config/angular_init.config.json"
19
- config = Ngi::Delegate::Configure.new(config_file).from_json
20
- components = config['global']['components'].collect { |t| t['name'] }
21
- types = Ngi::UserInput.new(valid_inputs: components)
22
-
23
- parser.name = 'ngi'
24
- parser.version = Ngi::VERSION
25
- parser.banner << "\n(<command> can be one of the following)"
26
-
27
- parser.on(types.valid_inputs, 'Create a new component') do |command|
28
- Ngi::Delegate::Generator.run(type: command, config: config)
29
- end
30
-
31
- parser.on(['-o', '--options'], 'Configure ngi') do
32
- Ngi::Delegate::Configure.run(write: true, file_path: config_file)
33
- end
34
- end
35
-
36
- p.parse(args)
37
- end
38
- end
39
-
40
- Parser.parse(ARGV)
10
+ Ngi::Parser.parse(ARGV)
@@ -0,0 +1,3 @@
1
+ aliases:
2
+ config:
3
+ - routes
@@ -0,0 +1,55 @@
1
+ -
2
+ name: "directive"
3
+ type: "script"
4
+ using: "default"
5
+ template: "basic.js"
6
+ -
7
+ name: "controller"
8
+ type: "script"
9
+ using: "default"
10
+ template: "basic.js"
11
+ -
12
+ name: "factory"
13
+ type: "script"
14
+ using: "default"
15
+ template: "basic.js"
16
+ -
17
+ name: "service"
18
+ type: "script"
19
+ using: "default"
20
+ template: "basic.js"
21
+ -
22
+ name: "config"
23
+ type: "script"
24
+ using: "default"
25
+ template: "config.js"
26
+ -
27
+ name: "run"
28
+ type: "script"
29
+ using: "default"
30
+ template: "config.js"
31
+ -
32
+ name: "routes"
33
+ type: "script"
34
+ using: "default"
35
+ template: "config.js"
36
+ -
37
+ name: "filter"
38
+ type: "script"
39
+ using: "default"
40
+ template: "basic.js"
41
+ -
42
+ name: "module"
43
+ type: "script"
44
+ using: "default"
45
+ template: "module.js"
46
+ -
47
+ name: "constant"
48
+ type: "script"
49
+ using: "default"
50
+ template: "constant.js"
51
+ -
52
+ name: "index"
53
+ type: "markup"
54
+ using: "default"
55
+ template: "index.html"
@@ -0,0 +1 @@
1
+ - language
@@ -0,0 +1,5 @@
1
+ script:
2
+ - coffee
3
+ - es5
4
+ markup:
5
+ - html
@@ -0,0 +1,3 @@
1
+ language:
2
+ script: es5
3
+ markup: html
@@ -0,0 +1,93 @@
1
+ /*
2
+ Hello!
3
+
4
+ Use this file to create your own template to use with ngi.
5
+
6
+ Erase this tutorial when finished with your template.
7
+
8
+ Then, do the following in your terminal:
9
+
10
+ $ ngi -o
11
+ $ [?] Option: template
12
+ $ <file path>
13
+ $ [?] Language:
14
+
15
+ Example (basic.js)
16
+ ==================
17
+
18
+ ;(function(app) {
19
+
20
+ 'use strict';
21
+
22
+ app.{{type}}('{{name}}',{{name}});
23
+
24
+ {{name}}.$inject = {{inject | array_string}};
25
+
26
+ function {{name}}({{inject | comma_delimited_variables}}) {
27
+ {% if directive %}
28
+ var d = {
29
+ restrict: 'A',
30
+ link: link
31
+ };
32
+
33
+ return d;
34
+
35
+ function link(scope, element, attrs) {
36
+
37
+ }
38
+ {% endif directive %}
39
+ {% if filter %}
40
+ return function(input) {
41
+ return;
42
+ }
43
+ {% endif filter %}
44
+ }
45
+
46
+ })(angular.module('{{module}}'));
47
+
48
+ Syntax
49
+ ======
50
+
51
+ Use normal JavaScript or CoffeeScript syntax.
52
+
53
+ (1) Placeholders
54
+
55
+ Use the following *placeholders* in your template:
56
+
57
+ {{type}}
58
+ The type of component (directive, controller, etc. Type ngi -h to see the available components)
59
+
60
+ {{name}}
61
+ [?] <component> name: <user input>
62
+ The user will inject the name of the component here (e.g. MyAwesomeController)
63
+
64
+ {{inject | array_string}}
65
+ [?] Inject: <user input>
66
+ This will turn the user's input for the injectable components into an array
67
+
68
+ For example:
69
+ [?] Inject: someService, anotherService
70
+ => ['someService', 'anotherService']
71
+
72
+ {{inject | comma_delimited_variables}}
73
+ [?] Inject: <user input>
74
+ This will turn the user's input for the injectable components into a
75
+ comma-delimited string of arguments
76
+
77
+ For example:
78
+ [?] Inject: someService, anotherService
79
+ => 'someService', 'anotherService'
80
+
81
+ (2) Tags
82
+
83
+ Tags allow you to use the same template for multiple types of components.
84
+ For example, the above example template for "basic.js" allows you
85
+ to use the same template for multiple components, including controllers,
86
+ directives, and filters.
87
+
88
+ Use the following *tags* in your template:
89
+
90
+ {% if <component> %}
91
+ // conditional code here
92
+ {% endif <component> %}
93
+ */
@@ -0,0 +1,3 @@
1
+ aliases:
2
+ config:
3
+ - routes
@@ -0,0 +1,55 @@
1
+ -
2
+ name: "directive"
3
+ type: "script"
4
+ using: "default"
5
+ template: "basic.js"
6
+ -
7
+ name: "controller"
8
+ type: "script"
9
+ using: "default"
10
+ template: "basic.js"
11
+ -
12
+ name: "factory"
13
+ type: "script"
14
+ using: "default"
15
+ template: "basic.js"
16
+ -
17
+ name: "service"
18
+ type: "script"
19
+ using: "default"
20
+ template: "basic.js"
21
+ -
22
+ name: "config"
23
+ type: "script"
24
+ using: "default"
25
+ template: "config.js"
26
+ -
27
+ name: "run"
28
+ type: "script"
29
+ using: "default"
30
+ template: "config.js"
31
+ -
32
+ name: "routes"
33
+ type: "script"
34
+ using: "default"
35
+ template: "config.js"
36
+ -
37
+ name: "filter"
38
+ type: "script"
39
+ using: "default"
40
+ template: "basic.js"
41
+ -
42
+ name: "module"
43
+ type: "script"
44
+ using: "default"
45
+ template: "module.js"
46
+ -
47
+ name: "constant"
48
+ type: "script"
49
+ using: "default"
50
+ template: "constant.js"
51
+ -
52
+ name: "index"
53
+ type: "markup"
54
+ using: "default"
55
+ template: "index.html"
@@ -0,0 +1,2 @@
1
+ - language
2
+ - templates
@@ -0,0 +1,5 @@
1
+ script:
2
+ - coffee
3
+ - es5
4
+ markup:
5
+ - html
@@ -0,0 +1,4 @@
1
+ ---
2
+ language:
3
+ script: es5
4
+ markup: html
data/lib/ngi.rb CHANGED
@@ -3,17 +3,14 @@
3
3
  # github.com/joshbeam/angular_init
4
4
  # MIT License
5
5
 
6
- CURRENT_DIR = File.dirname(__FILE__)
7
-
8
- require_relative 'ngi/delegate'
9
- require_relative 'ngi/utils/utils'
10
- require_relative 'ngi/version'
11
-
12
6
  # This module wraps the basic classes
13
7
  # of the tool ngi (in other words, this
14
8
  # module simply provides namespace)
15
9
  module Ngi
10
+ require_relative 'ngi/delegate'
11
+ require_relative 'ngi/parser'
12
+
16
13
  # REVIEW: Is this the best way to do it?
17
14
  Delegate = ::Delegate
18
- UserInput = ::Utils::UserInput
15
+ Parser = ::Parser
19
16
  end
@@ -3,19 +3,30 @@
3
3
  # github.com/joshbeam/angular_init
4
4
  # MIT License
5
5
 
6
- # CURRENT_DIR is defined in angualr_init.rb
6
+ # TODO: Method to clean out unused custom template files
7
7
 
8
- require_relative '../dep/json'
9
8
  require_relative 'utils/utils'
9
+ require 'fileutils'
10
+ require 'yaml'
10
11
 
11
12
  # Run the user through an interactive
12
13
  # session of configuring ngi
13
14
  class Configure
14
15
  attr_accessor :file, :location
15
16
 
16
- Utils = ::Utils
17
- JSArray = Utils::JSArray
18
- JSHash = Utils::JSHash
17
+ JSer = Utils::JSer
18
+ Utils::CurrentDir.dir = File.dirname(__FILE__)
19
+
20
+ # STDIN is separated into a class so that
21
+ # it can be extracted and tested
22
+ class AcceptInput
23
+ def self.str(type)
24
+ case type
25
+ when :stripped
26
+ $stdin.gets.strip
27
+ end
28
+ end
29
+ end
19
30
 
20
31
  # Here, we implement the virtual class "Utils::AskLoop"
21
32
  # (see src/ruby/utils/utils.rb)
@@ -24,35 +35,179 @@ class Configure
24
35
  class AskLoop < Utils::AskLoop
25
36
  def self.ask(args)
26
37
  puts "\nChoose from: #{args[:valid]}"
27
- answer = $stdin.gets.strip
38
+ answer = AcceptInput.str(:stripped)
28
39
 
29
40
  loop do
30
41
  break if args[:check].include?(answer)
31
42
  puts "Choose from: #{args[:valid]}\n(or press ctrl+c to exit)"
32
- answer = $stdin.gets.strip
43
+ answer = AcceptInput.str(:stripped)
33
44
  end
34
45
  answer
35
46
  end
36
47
  end
37
48
 
49
+ # An extraction of the template file/directory for
50
+ # the generator... This way it can be separated, redefined,
51
+ # and tested.
52
+ class TemplateDir
53
+ attr_reader :d
54
+
55
+ def initialize(component, language, template)
56
+ @d = "#{Utils::CurrentDir.dir}/templates/"
57
+ @d << "#{component['type']}/#{language}"
58
+ @d << "/user/#{template}"
59
+ end
60
+
61
+ def read
62
+ f = File.open(@d)
63
+ content = f.read
64
+ f.close
65
+
66
+ content
67
+ end
68
+ end
69
+
38
70
  # Holds all the configure functions for the properties
39
71
  # that are configurable
40
72
  class Configurable
41
73
  def self.language(config)
42
- v = JSArray.new(config.lang_types).to_str
74
+ v = JSer.new(config.lang_types).to_str
43
75
  type = AskLoop.ask(check: config.lang_types, valid: v)
44
-
45
- curr_lang = config.global['language'][type]
76
+ curr_lang = config.config['language'][type]
46
77
  lang_opts = config.languages[type].reject { |l| l if curr_lang == l }
47
78
 
48
- v = JSArray.new(lang_opts).to_str
79
+ v = JSer.new(lang_opts).to_str
49
80
  language = AskLoop.ask(check: lang_opts, valid: v)
50
81
 
51
- answer = config.global['language']
82
+ answer = config.config['language']
52
83
  answer[type] = language
53
84
 
54
85
  answer
55
86
  end
87
+
88
+ def self.create_template_file(args)
89
+ component = args[:component]
90
+ language = args[:language]
91
+ template = args[:template]
92
+
93
+ template_dir = TemplateDir.new(component, language, template).d
94
+
95
+ destination = File.dirname(template_dir)
96
+
97
+ # Create the directory "user" unless it already exists
98
+ FileUtils.mkdir_p(destination) unless File.directory?(destination)
99
+
100
+ # The actual custom file
101
+ custom_file = template
102
+
103
+ # Copy the custom file into the new or existing "user" directory
104
+ if File.exist? custom_file
105
+ # TODO: Add a 'safety net' by checking if the user
106
+ # has already added the custom template before
107
+ # so that they don't overwrite it
108
+ FileUtils.cp(custom_file, destination)
109
+ else
110
+ puts "Cannot find custom template file: '#{custom_file}'
111
+ Check to make sure the custom template file exists,
112
+ and that you're in the correct directory of the custom template."
113
+
114
+ exit
115
+ end
116
+ end
117
+
118
+ def self.templates(config)
119
+ v_c = JSer.new(config.components).to_str
120
+ component = AskLoop.ask(check: config.components, valid: v_c)
121
+ chosen_component = -> (c) { c['name'] == component }
122
+
123
+ print '[?] Use the following template file: '
124
+ file_name = AcceptInput.str(:stripped)
125
+
126
+ print '[?] What language is this template for?'
127
+ type = config.components_hash.find(&chosen_component)['type']
128
+ v_l = JSer.new(config.languages[type]).to_str
129
+ language = AskLoop.ask(check: config.languages[type], valid: v_l)
130
+ chosen_language = -> (t) { t['language'] == language }
131
+
132
+ # Our answer will be an empty array first,
133
+ # because Tempaltes: might not exist the
134
+ # config.yml file
135
+ answer = []
136
+
137
+ # Set the answer to Templates: from the config.yml
138
+ # file if it exists
139
+ answer = config.config['templates'] if config.config.key? 'templates'
140
+
141
+ # Then we want to see if the component already exists in
142
+ # the config file
143
+ exists = false
144
+ existing_component = answer.find(&chosen_component)
145
+ exists = true if !existing_component == false
146
+
147
+ if file_name != 'default'
148
+ if exists == true
149
+ # Remove the current existing component (it's already saved above)
150
+ answer = answer
151
+ .reject(&chosen_component)
152
+
153
+ # Remove the existing template object
154
+ existing_component['templates'] = existing_component['templates']
155
+ .reject(&chosen_language)
156
+
157
+ # Put in the updated template object
158
+ existing_component['templates'] << {
159
+ 'language' => language,
160
+ 'template' => file_name
161
+ }
162
+
163
+ # Push the existing component back into the templates array
164
+ answer << existing_component
165
+
166
+ elsif exists == false
167
+
168
+ # If it isn't already there,
169
+ # go ahead and add it to the templates array
170
+ answer << {
171
+ 'name' => component,
172
+ 'type' => type,
173
+ 'templates' => [
174
+ {
175
+ 'language' => language,
176
+ 'template' => file_name
177
+ }
178
+ ]
179
+ }
180
+ end
181
+
182
+ create_template_file(
183
+ config: config.config,
184
+ component: answer.last,
185
+ language: language,
186
+ template: file_name
187
+ )
188
+ else
189
+ # If default is chosen as the template,
190
+ # then delete the template from the templates
191
+ # array for that component
192
+ answer
193
+ .find(&chosen_component)['templates']
194
+ .delete_if(&chosen_language)
195
+
196
+ # If the templates array of the component
197
+ # is empty, then delete the entire component
198
+ # from the answer
199
+ answer
200
+ .delete_if(&chosen_component) if answer
201
+ .find(&chosen_component)['templates']
202
+ .size == 0
203
+ end
204
+
205
+ if answer.size == 0
206
+ nil
207
+ else
208
+ answer
209
+ end
210
+ end
56
211
  end
57
212
 
58
213
  # Make Questioner accesible as in: Configure::Questioner.run()
@@ -62,44 +217,38 @@ class Configure
62
217
  # options (like language to use, templates, etc.)
63
218
  class Questioner
64
219
  attr_accessor :file
65
- attr_reader :configurable_properties,
66
- :languages, :global,
67
- :configurable, :lang_types
68
-
69
- def initialize(file)
70
- @file = file
71
-
72
- @global = @file['global']
73
- # TODO: extend array with this inject function?
74
-
75
- # The options for languages to use
76
- @languages = @global['languages']
77
-
220
+ attr_reader :configurable,
221
+ :languages,
222
+ :config,
223
+ :components,
224
+ :components_hash,
225
+ :lang_types
226
+
227
+ def initialize(args)
228
+ @languages = args[:languages]
78
229
  language_types = @languages.select do |type, languages|
79
230
  type if languages.size > 1
80
231
  end
81
- # For example, ['script','markup']
82
232
  @lang_types = language_types.collect { |type, _| type }.flatten
83
-
84
- # The properties in key => value format
85
- # of the properties the user can configure
86
- @configurable = @global['configurable']
87
-
88
- # An array of the properties that the user is allowed to configure,
89
- # according to src/config/angular_init.config.json
90
- @configurable_properties = @configurable.collect { |_k, v| v }
233
+ @components_hash = args[:components_hash]
234
+ @components = args[:components]
235
+ @config = args[:config]
236
+ @configurable = args[:configurable]
91
237
 
92
238
  yield(self) if block_given?
93
239
  end
94
240
 
95
241
  def choose_configurable_property
96
- @configurable_properties.each_with_index do |p, i|
97
- puts "#{i + 1}) #{p.capitalize}: #{JSHash.new(@global[p]).to_str}"
242
+ puts "Current settings\n================"
243
+ @configurable.each_with_index do |p, i|
244
+ json_string = 'Currently using default settings'
245
+ json_string = JSer.new(@config[p]).to_str if @config.key? p
246
+ puts "#{i + 1}) #{p.capitalize}: #{json_string}"
98
247
  end
99
248
 
100
- valid = JSArray.new(@configurable_properties).to_str
249
+ valid = JSer.new(@configurable).to_str
101
250
  # return
102
- AskLoop.ask(check: @configurable_properties, valid: valid)
251
+ AskLoop.ask(check: @configurable, valid: valid)
103
252
  end
104
253
 
105
254
  # This method delegates to the appropriate
@@ -109,8 +258,10 @@ class Configure
109
258
  # from_json config object from config/angular_init.config.json
110
259
  def configure_property(property)
111
260
  case property
112
- when @configurable['language']
261
+ when 'language'
113
262
  return Configurable.language(self)
263
+ when 'templates'
264
+ return Configurable.templates(self)
114
265
  end
115
266
  end
116
267
 
@@ -130,11 +281,14 @@ class Configure
130
281
  # Hash from_json object that came from
131
282
  # config/angular_init.config.json
132
283
  # and is inside of this instance of Questioner
133
- q.file['global'][property] = result
284
+ q.config[property] = result
285
+
286
+ # delete any properties that are nil
287
+ q.config.delete_if { |_, v| v.nil? }
134
288
 
135
289
  # This just tells the user that we were
136
290
  # successful
137
- result_string_hash = JSHash.new(result).to_str
291
+ result_string_hash = JSer.new(result).to_str rescue 'null'
138
292
  puts "#{property.capitalize} set to: #{result_string_hash}"
139
293
  end
140
294
 
@@ -142,42 +296,57 @@ class Configure
142
296
  # (For example, Configure might write this
143
297
  # new hash as a JSON file to
144
298
  # config/angular_init.config.json)
145
- questioner.file
299
+ questioner.config
146
300
  end
147
301
  end
148
302
 
149
303
  # The only thing we do here is load the JSON config file basically
150
304
  # as just a string in JSON format.
151
305
  # It will be converted to a Ruby hash in from_json below
152
- def initialize(location)
153
- @location = location
154
- @file = IO.read(@location)
306
+ def initialize(location = nil)
307
+ unless location.nil?
308
+ @location = location
309
+ f = File.open(@location, 'r')
310
+ @file = f.read
311
+ f.close
312
+ end
155
313
 
156
314
  yield(self) if block_given?
157
315
  end
158
316
 
159
317
  # Convert the file to a Ruby hash
160
- def from_json
161
- JSON.parse(@file)
318
+ # def from_json
319
+ # JSON.parse(@file)
320
+ # end
321
+
322
+ # Convert the file to a Ruby hash
323
+ # Usage: Configure.new('file/path').to_ruby(from: 'yaml')
324
+ def to_ruby(args)
325
+ case args[:from]
326
+ when 'json'
327
+ JSON.parse(@file)
328
+ when 'yaml'
329
+ YAML.load(@file)
330
+ end
162
331
  end
163
332
 
164
- # Generate a "prettified" JSON version of our
165
- # newly updated Ruby hash with the user's options
166
- def to_json
167
- if @file.is_a? Hash
333
+ # Convert the file from a Ruby hash
334
+ # into whatever language is specified
335
+ # Usage: <Configure instance>.from_ruby(to: 'yaml')
336
+ def from_ruby(args)
337
+ case args[:to]
338
+ when 'json'
168
339
  JSON.pretty_generate(@file)
169
- else
170
- @file
340
+ when 'yaml'
341
+ @file.to_yaml
171
342
  end
172
343
  end
173
344
 
174
345
  # Here we actually write the new JSON config file
175
346
  # to config/angular_init.config.json
176
- def write
177
- new_file = to_json
178
-
179
- File.open(@location, 'w') do |f|
180
- f.write(new_file)
347
+ def write(args)
348
+ File.open(args[:destination], 'w') do |f|
349
+ f.write(args[:file])
181
350
  f.close
182
351
  end
183
352
  end
@@ -190,23 +359,15 @@ class Configure
190
359
  # to actually write the file in JSON format
191
360
  # to config/angular_init.config.json
192
361
  def self.run(args)
193
- Configure.new(args[:file_path]) do |c|
194
- # Get the JSON file as a Ruby Hash
195
- json_file = c.from_json
196
-
197
- # Pass it off to Questioner so that
198
- # we can interact with the user and
199
- # have the user configure the Hash.
200
- # Then, we set the Configure file
201
- # to the output of however the user
202
- # configured it with Questioner
203
- c.file = Configure::Questioner.run(json_file)
204
-
205
- # We'll write the hash to
206
- # config/angular_init.config.json.
207
- # Configure#write converts the Hash
208
- # to JSON and then uses File.write
209
- c.write if args[:write] == true
362
+ Configure.new do |c|
363
+ c.file = Configure::Questioner.run(args)
364
+
365
+ if args[:write] == true
366
+ c.write(
367
+ destination: args[:destination],
368
+ file: c.from_ruby(to: args[:to])
369
+ )
370
+ end
210
371
  end
211
372
  end
212
373
  end