Sha256: dd2e12d53d9322d5c8c8f4e62872ecd2dd6b276c71d8c42152b9b8949703fdf9

Contents?: true

Size: 1.48 KB

Versions: 14

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module RailsAppGenerator
  # Utility methods
  class Util
    class << self
      # Log KeyValue pair
      #
      # @example
      #   Util.kv(:key, :value)
      def kv(label, value, len = 35)
        return ' ' * len if label.nil?

        label = label.to_s if label.is_a?(Symbol)
        label = label.length > len ? label.slice(0..len) : label.ljust(len, ' ')

        puts "#{label}: #{value}"
      end

      def line(heading, len = 80)
        heading = "- [ #{heading} ]"
        puts heading.length > len ? heading : heading.ljust(len, '-')
      end

      def write_last_run(file, data)
        file = File.join('~/dev/kgems/rails_app_generator/docs/last_run/', file)
        write_json(file, data)
      end

      def write_json(file, data)
        file = File.expand_path(file)
        puts "Write to #{file}"
        FileUtils.mkdir_p(File.dirname(file))
        File.write(file, JSON.pretty_generate(data))
      end

      # Take array of Thor::Option and extract the data as a hash
      #
      # You can find the array on the class_options reader
      def thor_options_to_hash(thor_options)
        common_keys = thor_options.flat_map(&:instance_variables).uniq
        common_keys = common_keys.map { |k| k.to_s.delete_prefix('@').to_sym }

        thor_options.map do |option|
          common_keys.each_with_object({}) do |key, result|
            result[key] = option.send(key) if option.respond_to?(key)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rails_app_generator-0.2.13 lib/rails_app_generator/util.rb
rails_app_generator-0.2.12 lib/rails_app_generator/util.rb
rails_app_generator-0.2.11 lib/rails_app_generator/util.rb
rails_app_generator-0.2.10 lib/rails_app_generator/util.rb
rails_app_generator-0.2.9 lib/rails_app_generator/util.rb
rails_app_generator-0.2.8 lib/rails_app_generator/util.rb
rails_app_generator-0.2.7 lib/rails_app_generator/util.rb
rails_app_generator-0.2.6 lib/rails_app_generator/util.rb
rails_app_generator-0.2.5 lib/rails_app_generator/util.rb
rails_app_generator-0.2.4 lib/rails_app_generator/util.rb
rails_app_generator-0.2.3 lib/rails_app_generator/util.rb
rails_app_generator-0.2.2 lib/rails_app_generator/util.rb
rails_app_generator-0.2.1 lib/rails_app_generator/util.rb
rails_app_generator-0.2.0 lib/rails_app_generator/util.rb