class Specfac::CLI

Attributes

available_methods[RW]
dir_controllers[RW]
dir_factories[RW]
dir_features[RW]
dir_support[RW]
paths[RW]
working_dirs[RW]
working_file[RW]

Public Instance Methods

__print_version() click to toggle source
# File lib/specfac.rb, line 104
def __print_version
  puts Specfac.show_v
end
create_directories(*dirs) click to toggle source
# File lib/specfac.rb, line 148
def create_directories(*dirs)
  dirs.each {|dir| Dir.mkdir(dir) if !Dir.exists?(dir) }
end
extract(dest) click to toggle source
# File lib/specfac.rb, line 72
def extract(dest)
  @paths = $paths
  @paths[:options]["path"] = dest
  puts "Setting extract destination..."
  create_file(@paths[:options_path])
  opener(nil, @paths[:options].to_json, "w", @paths[:options_path])
end
generate(*args) click to toggle source
# File lib/specfac.rb, line 86
def generate(*args)
  init_vars(options)

  controller = args.shift
  actions = args

  if controller
    sanitize(controller, actions, options)
  else
    puts "Please provide a controller name."
    exit
  end
end
init_vars(options=nil) click to toggle source
# File lib/specfac.rb, line 123
def init_vars(options=nil)
  @working_dirs = ["spec", "controllers", "factories", "support", "features"]
  @dir_support = "#{@working_dirs[0]}/#{@working_dirs[3]}"
  @dir_controllers = "#{@working_dirs[0]}/#{@working_dirs[1]}"
  @dir_factories = "#{@working_dirs[0]}/#{@working_dirs[2]}"
  @dir_features = "#{@working_dirs[0]}/#{@working_dirs[4]}"
  @available_methods = SpecModule.methods(false).to_a.map {|item| item.to_s}
  @working_file = nil

  create_directories(@working_dirs[0])

  if options
    create_directories(@dir_controllers)
    if options[:f]
      create_directories(@dir_factories)
    end
    if options[:e]
      create_directories(@dir_features)
    end
  else
    create_directories(@dir_support, "#{@dir_support}/specfac")
  end

end
opener(mode, lines, open_type, file = @working_file) click to toggle source
# File lib/specfac.rb, line 194
def opener(mode, lines, open_type, file = @working_file)
  filer = lambda do |type, output|
    File.open(file, type) { |handle| handle.puts output}
  end

  if mode == "spec" || mode == "feature"
    filer.call(open_type, nil)
    lines.each do |item|
      filer.call("a", item)
    end
  elsif mode == "end"
    filer.call(open_type, lines)
  else
    filer.call(open_type, lines)
  end
end
pull_src(controller, actions, options=nil) click to toggle source
# File lib/specfac.rb, line 152
def pull_src(controller, actions, options=nil)

  @working_file = "#{@dir_controllers}/#{controller.downcase}_controller_spec.rb"
  # Spec tests
  create_file(@working_file)
  opener(
      "spec",
      ["require 'rails_helper'","RSpec.describe #{controller.capitalize}Controller, type: :controller do"],
      "w"
  )

  Utils.define_utils_methods_params(controller)
  actions != nil ? actions.each {|action| opener("tests", SpecModule.public_send(action.to_sym), "a")} : nil
  opener("end", "end", "a")

  # Factories

  if options
    if options[:f]

      @working_file = "#{@dir_factories}/#{Utils.pluralize(controller.downcase)}.rb"
      create_file(@working_file)
      opener("factory", FactoryModule.create, "w")
      opener("end", nil, "a")
    end

    # end to end tests

    if options[:e]
      @working_file = "#{@dir_features}/#{Utils.pluralize(controller.downcase)}_spec.rb"
      create_file(@working_file)
      opener("feature",
             ["require 'rails_helper'", "describe 'navigation' do"],
             "w")
      actions != nil ? actions.each {|action| opener("tests", E2eModule.public_send(action.to_sym), "a")} : nil
      opener("end", "end", "a")
    end
  end


end
sanitize(controller, actions, options) click to toggle source
# File lib/specfac.rb, line 211
def sanitize(controller, actions, options)

  rem = "_controller"
  if controller.include? "_controller"
    controller.gsub!(rem, "")
    # puts controller
  end
  if actions.include?("ALL")
    matched_actions = @available_methods
  else
    matched_actions = []
    actions.each {|action| matched_actions << action if @available_methods.include?(action)}
  end

  # p matched_actions
  pull_src(controller, matched_actions, options)
end
setup(*args) click to toggle source
# File lib/specfac.rb, line 111
def setup(*args)
  init_vars
  @working_file = "#{@dir_support}/specfac/config.rb"
  create_file(@working_file)
  args != nil ? args.each {|arg| opener("support", SupportModule.public_send(arg.to_sym), "a")} : nil
end