Class: MiniService::Commands::Generate
- Inherits:
-
MiniService::Command
- Object
- MiniService::Command
- MiniService::Commands::Generate
- Defined in:
- lib/mini_service/commands/generate.rb
Defined Under Namespace
Classes: NameError
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #ask_error_message ⇒ Object
- #ask_name ⇒ Object
- #execute(_input: $stdin, output: $stdout) ⇒ Object
- #file_content ⇒ Object
-
#initialize(name, *arguments) ⇒ Generate
constructor
A new instance of Generate.
- #location ⇒ Object
- #parsed_name ⇒ Object
- #puts_info(output:) ⇒ Object
- #select_options ⇒ Object
- #subd_word(word) ⇒ Object
- #underscore(camel_cased_word) ⇒ Object
Methods inherited from MiniService::Command
Constructor Details
#initialize(name, *arguments) ⇒ Generate
Returns a new instance of Generate.
11 12 13 14 15 16 17 |
# File 'lib/mini_service/commands/generate.rb', line 11 def initialize(name, *arguments) @name = name @arguments = arguments ask_name rescue TTY::Reader::InputInterrupt puts "\nNot OK" end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
19 20 21 |
# File 'lib/mini_service/commands/generate.rb', line 19 def arguments @arguments end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/mini_service/commands/generate.rb', line 19 def name @name end |
Instance Method Details
#ask_error_message ⇒ Object
38 39 40 |
# File 'lib/mini_service/commands/generate.rb', line 38 def 'Invalid class name, please start with an uppercase letter' end |
#ask_name ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/mini_service/commands/generate.rb', line 29 def ask_name unless name @name = prompt.ask 'Name?', required: true, default: 'SomeModule::SomeService' do |q| q.validate %r{\A[A-Z]}, end end end |
#execute(_input: $stdin, output: $stdout) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/mini_service/commands/generate.rb', line 21 def execute(_input: $stdin, output: $stdout) raise NameError unless name puts_info output: output generator.create_file location, file_content output.puts 'OK' end |
#file_content ⇒ Object
46 47 48 |
# File 'lib/mini_service/commands/generate.rb', line 46 def file_content MiniService::FileGenerator.file name.split('::'), arguments end |
#location ⇒ Object
42 43 44 |
# File 'lib/mini_service/commands/generate.rb', line 42 def location prompt.select 'Dónde?', end |
#parsed_name ⇒ Object
62 63 64 |
# File 'lib/mini_service/commands/generate.rb', line 62 def parsed_name underscore(name) end |
#puts_info(output:) ⇒ Object
50 51 52 53 54 |
# File 'lib/mini_service/commands/generate.rb', line 50 def puts_info(output:) output.puts "@name: #{name}" output.puts "@arguments: #{arguments.join(', ')}" output.puts "parsed_name: #{parsed_name}" end |
#select_options ⇒ Object
56 57 58 59 60 |
# File 'lib/mini_service/commands/generate.rb', line 56 def = ['lib/services'] + Dir['**/services'] .map! { |item| item + '/' + parsed_name + '.rb' } .uniq end |
#subd_word(word) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/mini_service/commands/generate.rb', line 72 def subd_word(word) word.gsub!(%r{([A-Z\d]+)([A-Z][a-z])}, '\1_\2') word.gsub!(%r{([a-z\d])([A-Z])}, '\1_\2') word.tr!('-', '_') word.downcase! end |
#underscore(camel_cased_word) ⇒ Object
66 67 68 69 70 |
# File 'lib/mini_service/commands/generate.rb', line 66 def underscore(camel_cased_word) return camel_cased_word unless %r{[A-Z-]|::}.match?(camel_cased_word) subd_word camel_cased_word.to_s.gsub('::', '/') end |