Class: RailsAppGenerator::OptionsBuilder

Inherits:
Object
  • Object
show all
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

RailsOptions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = default_options
  @options.merge!(opts)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rails_app_generator/options/options_builder.rb', line 7

def options
  @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 class_options_lookup.key?(option.name)
    # raise ArgumentError, "Option #{option.name} already registered"
    puts "Option #{option.name} already registered"
    return
  end

  class_options_lookup[option.name.to_sym] = option
  class_options << 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_optionsObject



16
17
18
# File 'lib/rails_app_generator/options/options_builder.rb', line 16

def class_options
  @class_options ||= []
end

.class_options_lookupObject



20
21
22
# File 'lib/rails_app_generator/options/options_builder.rb', line 20

def class_options_lookup
  @class_options_lookup ||= {}
end

.resetObject



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_hObject



61
62
63
64
65
66
# File 'lib/rails_app_generator/options/options_builder.rb', line 61

def to_h
  {
    class_option_keys: class_options.map(&:name),
    class_options: class_options.map(&:to_h)
  }
end

Instance Method Details

#cmd_line_optionsObject

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 cmd_line_options
  available_options = options.clone

  result = self.class.class_options.map do |option|
    mapper = option.mapper
    key = option.name.to_sym
    value = options[key]
    available_options.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 available_options.empty?

  puts "options not registered: #{available_options.keys.join(', ')}"
  result
  # raise 'Unknown options'
end

#debugObject

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(options)
  puts '[ Options Command Line ]--------------------------------------------'
  puts cmd_line_options
end

#to_hObject



102
103
104
105
106
# File 'lib/rails_app_generator/options/options_builder.rb', line 102

def to_h
  {
    options: options
  }
end