class EmberExcerpt::CLI::Controller

Public Class Methods

new(options) click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 11
def initialize(options)
  @options = options
end

Public Instance Methods

extract() click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 42
def extract
  begin
    data = load(@options.input)

    extractor = get_extractor(@options.type)
    items = extractor.extract(data)

    write_output(items)
  rescue => err
    puts 'Extract Failed'
    if @options.verbose
      puts err.backtrace
    end
  end
end
get_extractor(type) click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 72
def get_extractor(type)
  if type == 'class'
    return ClassExtractor.new
  elsif type == 'method'
    return MethodExtractor.new
  else
    raise "Extractor not found for type: #{type}"
  end
end
load(input) click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 91
def load(input)
  puts "Loading #{input}"
  data = open(input)
  yml = YAML.load(data)

  return yml
end
show_error(msg = @options.error) click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 23
def show_error(msg = @options.error)
  puts "Error: #{msg}"
  puts

  show_help
end
show_help() click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 19
def show_help
  puts @options.options
end
show_invalid_option() click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 30
def show_invalid_option
  show_error @options.error
end
show_missing_args() click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 34
def show_missing_args
  show_error @options.error
end
show_parser_error() click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 38
def show_parser_error
  show_error @options.error
end
show_version() click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 15
def show_version
  puts VERSION
end
uri?(string) click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 82
def uri?(string)
  uri = URI.parse(string)
  %w( http https ).include?(uri.scheme)
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end
write_output(items) click to toggle source
# File lib/ember_excerpt/cli/controller.rb, line 58
def write_output(items)
  if items.length == 0
    puts "Nothing to output for #{options.type}"
  else
    file = File.open(@options.output, 'w')
    items.each do |item|
      file.puts item
    end

    file.close
    puts "Wrote #{items.length} items to #{@options.output}"
  end
end