class Codecom::Runner
Attributes
configuration[RW]
Public Class Methods
new()
click to toggle source
# File lib/runner.rb, line 77 def initialize process_comments end
Public Instance Methods
extract_git_revision()
click to toggle source
# File lib/runner.rb, line 31 def extract_git_revision path = File.expand_path('../..', File.dirname(__FILE__)) `git --git-dir #{path}/.git rev-parse --short HEAD`.strip end
extract_method_arguments(line)
click to toggle source
# File lib/runner.rb, line 17 def extract_method_arguments(line) method = extract_method_name(line) if method.include?('(') method.scan(/\(([^\)]+)\)/)[0][0].split(',').map(&:strip) else [] end end
extract_method_name(line)
click to toggle source
# File lib/runner.rb, line 36 def extract_method_name(line) line.strip.split('def ')[1] end
extract_method_name_without_arguments(line)
click to toggle source
# File lib/runner.rb, line 40 def extract_method_name_without_arguments(line) name = extract_method_name(line) name.include?('(') ? name.split('(')[0] : name.split(' ')[0] end
indent_template(template, index)
click to toggle source
# File lib/runner.rb, line 67 def indent_template(template, index) striped_template(template).map do |slice| slice.prepend(' ' * index) end.join("\n") + "\n" end
param_template()
click to toggle source
# File lib/runner.rb, line 63 def param_template "# @param %{param} [Class] Write param definition here." end
process_comments(source_path = ".", end_with = "*.rb")
click to toggle source
# File lib/runner.rb, line 81 def process_comments(source_path = ".", end_with = "*.rb") Dir.glob("./#{source_path}/**/#{end_with}").each do |path| comments = [] temp_file = Tempfile.new(SecureRandom.hex) begin File.open(path).each_with_index do |line, index| if line.strip.start_with?('#') comments.push(line) else if line.strip.start_with?('def ') method_name = extract_method_name_without_arguments(line) data = comments.none? ? process_line(line, index) : comments.join temp_file.print(data) else temp_file.print(comments.join) end comments = [] temp_file.write(line) end end temp_file.print(comments.join) temp_file.close FileUtils.mv(temp_file.path, path) ensure temp_file.close temp_file.unlink end end end
process_line(line, index)
click to toggle source
# File lib/runner.rb, line 45 def process_line(line, index) method_name = template_options(line).fetch(:method_name) replaced_template = replace_template(template, template_options(line)) indent_template(replaced_template, line.index('def ')) end
replace_template(data, options = {})
click to toggle source
# File lib/runner.rb, line 51 def replace_template(data, options = {}) data = data.gsub('%{method_name}', options.fetch(:method_name)) data = data.gsub('%{author_name}', options.fetch(:author_name)) params = options.fetch(:params_name).map do |param| param_template.gsub('%{param}', param) end params = params.any? ? params.join("\n").prepend("#\n") : '# ' data.gsub('# %{params}', params) end
striped_template(template)
click to toggle source
# File lib/runner.rb, line 73 def striped_template(template) template.strip.split("\n").map(&:strip) end
template(template_name = 'template.txt')
click to toggle source
# File lib/runner.rb, line 116 def template(template_name = 'template.txt') File.read([File.dirname(__FILE__), template_name].join('/')) end
template_options(line)
click to toggle source
# File lib/runner.rb, line 9 def template_options(line) { author_name: extract_author_name, method_name: extract_method_name(line), params_name: extract_method_arguments(line) } end