Class: Natsukantou::Setup::ConfigPrompt

Inherits:
Object
  • Object
show all
Defined in:
lib/natsukantou/setup/config_prompt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigPrompt

Returns a new instance of ConfigPrompt.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/natsukantou/setup/config_prompt.rb', line 95

def initialize
  @prompt = TTY::Prompt.new

  registry = Natsukantou::Setup::Registry.instance

  @translators = registry.translators.map do |args|
    Natsukantou::Setup::Component.new(*args)
  end

  @filters = registry.middlewares.map do |args|
    Natsukantou::Setup::Component.new(*args)
  end

  @config = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



111
112
113
# File 'lib/natsukantou/setup/config_prompt.rb', line 111

def config
  @config
end

#filtersObject (readonly)

Returns the value of attribute filters.



111
112
113
# File 'lib/natsukantou/setup/config_prompt.rb', line 111

def filters
  @filters
end

#know_rubyObject (readonly)

Returns the value of attribute know_ruby.



112
113
114
# File 'lib/natsukantou/setup/config_prompt.rb', line 112

def know_ruby
  @know_ruby
end

#promptObject (readonly)

Returns the value of attribute prompt.



111
112
113
# File 'lib/natsukantou/setup/config_prompt.rb', line 111

def prompt
  @prompt
end

#translatorsObject (readonly)

Returns the value of attribute translators.



111
112
113
# File 'lib/natsukantou/setup/config_prompt.rb', line 111

def translators
  @translators
end

Instance Method Details

#askObject



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
# File 'lib/natsukantou/setup/config_prompt.rb', line 119

def ask
  @know_ruby = prompt.yes?('Do you know Ruby or JSON syntax?')

  translator = prompt.select(
    "Choose translation engine:",
    translators.map { |t| [t.name, t] }.to_h
  )

  config[:translator] = {
    component: translator,
    parameters: ask_initialize_parameters(translator)
  }

  config[:filters] = []

  selected_filters = prompt.multi_select(
    "Select one or many filters (using spacebar):",
    filters.map { |t| [t.name, t] }.to_h
  )

  selected_filters.each do |filter|
    config[:filters] << {
      component: filter,
      parameters: ask_initialize_parameters(filter)
    }
  end
end

#executeObject



114
115
116
117
# File 'lib/natsukantou/setup/config_prompt.rb', line 114

def execute
  ask
  output
end

#outputObject



147
148
149
150
151
152
153
# File 'lib/natsukantou/setup/config_prompt.rb', line 147

def output
  template = ERB.new(
    File.read((File.expand_path 'output.erb', __dir__)),
    trim_mode: "<>-"
  )
  template.result(binding)
end