Class: RailsAppGenerator::OptionsBuilder
- Inherits:
-
Object
- Object
- RailsAppGenerator::OptionsBuilder
- Defined in:
- lib/rails_app_generator/options/options_builder.rb
Overview
OptionBuilder can work standalone or as the base class for application generator options used by RailsAppGenerator::Starter
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .add_class_option(option) ⇒ Object
- .add_thor_class_option(thor_option) ⇒ Object
-
.class_option(name, **args) ⇒ Object
Register an option with the builder, this method has the same signature as Thor.
- .class_options ⇒ Object
- .class_options_lookup ⇒ Object
- .reset ⇒ Object
- .to_h ⇒ Object
Instance Method Summary collapse
-
#cmd_line_options ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#debug ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#initialize(opts) ⇒ OptionsBuilder
constructor
A new instance of OptionsBuilder.
- #to_h ⇒ Object
Constructor Details
#initialize(opts) ⇒ OptionsBuilder
Returns a new instance of OptionsBuilder.
9 10 11 12 13 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 9 def initialize(opts) # @opts = opts @options = @options.merge!(opts) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 7 def @options end |
Class Method Details
.add_class_option(option) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 33 def add_class_option(option) if .key?(option.name) # raise ArgumentError, "Option #{option.name} already registered" puts "Option #{option.name} already registered" return end [option.name.to_sym] = option << option end |
.add_thor_class_option(thor_option) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 44 def add_thor_class_option(thor_option) args = { description: thor_option.description, type: thor_option.type, default: thor_option.default, required: thor_option.required } option = BuildOption.new(thor_option.name, **args) add_class_option(option) end |
.class_option(name, **args) ⇒ Object
Register an option with the builder, this method has the same signature as Thor.
This is so options can be used interchangeably between OptionsBuilder and Thor.
27 28 29 30 31 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 27 def class_option(name, **args) option = BuildOption.new(name, **args) add_class_option(option) end |
.class_options ⇒ Object
16 17 18 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 16 def @class_options ||= [] end |
.class_options_lookup ⇒ Object
20 21 22 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 20 def @class_options_lookup ||= {} end |
.reset ⇒ Object
56 57 58 59 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 56 def reset @class_options = nil @class_options_lookup = nil end |
.to_h ⇒ Object
61 62 63 64 65 66 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 61 def to_h { class_option_keys: .map(&:name), class_options: .map(&:to_h) } end |
Instance Method Details
#cmd_line_options ⇒ Object
rubocop:disable Metrics/AbcSize
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 70 def = .clone result = self.class..map do |option| mapper = option.mapper key = option.name.to_sym value = [key] .delete(key) # if available_options.delete(option.name.to_sym) # puts "option IS registered: #{option.name}" # else # puts "option not registered: #{option.name}" # end mapper.map(option.name, value) end.reject(&:blank?) return result if .empty? puts "options not registered: #{.keys.join(', ')}" result # raise 'Unknown options' end |
#debug ⇒ Object
rubocop:enable Metrics/AbcSize
95 96 97 98 99 100 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 95 def debug puts '[ Options JSON ]----------------------------------------------------' puts JSON.pretty_generate() puts '[ Options Command Line ]--------------------------------------------' puts end |
#to_h ⇒ Object
102 103 104 105 106 |
# File 'lib/rails_app_generator/options/options_builder.rb', line 102 def to_h { options: } end |