class SleepingKingStudios::Tasks::File::NewTask
Thor task for generating a new Ruby source file.
Attributes
directory[R]
file_name[R]
file_path[R]
relative_path[R]
spec_path[R]
Public Class Methods
description()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 16 def self.description 'Creates a Ruby source file and corresponding spec file.' end
Public Instance Methods
call(file_path)
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 51 def call file_path split_file_path(file_path) check_for_existing_file file_path check_for_existing_file spec_path if spec? preview return unless prompt_confirmation create_source_file unless dry_run? create_spec_file unless dry_run? say 'Complete!', :green end
Private Instance Methods
check_for_existing_file(file_path)
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 77 def check_for_existing_file file_path return unless File.exist?(file_path) raise "file already exists at #{file_path}" unless force? end
create_directories(*directory_names)
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 83 def create_directories *directory_names directory_path = directory_names.compact.join(File::SEPARATOR) FileUtils.mkdir_p directory_path unless directory_path.empty? end
create_source_file()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 89 def create_source_file create_directories directory, *relative_path File.write file_path, rendered_source end
create_spec_file()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 95 def create_spec_file return unless spec? return if spec_file? create_directories 'spec', *relative_path File.write spec_path, rendered_spec end
find_template(name)
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 105 def find_template name template_paths.each do |template_dir| template_path = File.expand_path(File.join template_dir, name) next unless File.exist?(template_path) return File.read(template_path) end # each raise MissingTemplateError, "No template found for \"#{name}\"" end
preview()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 117 def preview say 'Files to create:' say "\n" preview_files say "\n" unless verbose? end
preview_file(file_path, max:, template: str = " %- { || ... }
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 126 def preview_file file_path, max:, template: str = " %-#{max}.#{max}s # using template '%s'" say format(str, file_path, template) return unless verbose? say "\n" say yield say "\n" end
preview_files()
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 139 def preview_files max = spec? ? [file_path.length, spec_path.length].max : file_path.length template = spec_file? ? 'rspec.erb' : 'ruby.erb' preview_file file_path, :max => max, :template => template do tools.str.indent(rendered_source, 4) end # preview_file return unless spec? && !spec_file? preview_file spec_path, :max => max, :template => 'rspec.erb' do tools.str.indent(rendered_spec, 4) end # preview_file end
prompt_confirmation()
click to toggle source
rubocop:enable Metrics/AbcSize
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 156 def prompt_confirmation return true unless prompt? unless yes? 'Create files? (yes|no)\n>' say "\n" say 'Cancelled!', :yellow return false end # if-else say "\n" true end
render_template(name, locals = {})
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 171 def render_template name, locals = {} template = find_template(name) source = Erubi::Engine.new(template).src binding = Object.new.send(:binding) locals.each do |key, value| binding.local_variable_set key, value end # each binding.eval source end
rendered_source()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 183 def rendered_source template = spec_file? ? 'rspec.erb' : 'ruby.erb' @rendered_source ||= render_template( template, :file_path => file_path, :file_name => file_name, :relative_path => relative_path ) # end render template end
rendered_spec()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 195 def rendered_spec @rendered_spec ||= render_template( 'rspec.erb', :file_path => spec_path, :file_name => "#{file_name}_spec", :relative_path => relative_path ) # end render template end
spec_file?()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 205 def spec_file? directory == 'spec' && file_name.end_with?('_spec') end
split_file_path(file_path)
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 209 def split_file_path file_path @file_path = file_path @directory = nil extension = File.extname(file_path) fragments = file_path.split(File::SEPARATOR) @file_name = File.basename(fragments.pop, extension) split_relative_path fragments @spec_path = File.join 'spec', *relative_path, "#{file_name}_spec#{extension}" end
split_relative_path(fragments)
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 222 def split_relative_path fragments if %w[app apps lib spec tmp vendor].include?(fragments.first) @directory = fragments.shift end # if @relative_path = fragments end
template_paths()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 230 def template_paths SleepingKingStudios::Tasks.configuration.file.template_paths end
tools()
click to toggle source
# File lib/sleeping_king_studios/tasks/file/new_task.rb, line 234 def tools SleepingKingStudios::Tools::Toolbelt.instance end