Sha256: d89246e816ea929b28a16dabde572bbc741218f6c6a6b2df9824cf357b5b3bae

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'thor'

module Codelog
  module CLIs
    class Interactive < Thor
      include Thor::Actions

      no_commands do
        def initialize
          @sections = YAML.load_file('changelogs/template.yml').keys
        end

        def run
          changes_hash = Hash.new([])
          loop do
            change_category = ask_for_type
            say "\nType the entries for #{set_color(change_category, :yellow)}(ENTER to stop):"
            changes_hash[change_category] += ask_for_changes
            break if no? "\nWould you like to add a new log(Y|N)?"
          end

          changes_hash
        end

        private

        def ask_for_type
          say 'Enter a change type number:'
          @sections.each_with_index do |section, index|
            say "#{index + 1}. #{section}"
          end
          @sections[ask('>').to_i - 1]
        end

        def ask_for_changes(changes = [], level = 1)
          change = ask('>' * level)

          return changes if change.empty?

          change = { change.chomp(':') => ask_for_changes([], level + 1) } if subcategory?(change)

          ask_for_changes(changes.push(change), level)
        end

        def subcategory?(change)
          change.end_with?(':')
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
codelog-0.8.0 lib/codelog/clis/interactive.rb
codelog-0.7.0 lib/codelog/clis/interactive.rb
codelog-0.6.0 lib/codelog/clis/interactive.rb