Class: RSpec::Scaffold::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/scaffold/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • file (Pathname)


7
8
9
# File 'lib/rspec/scaffold/runner.rb', line 7

def initialize(file)
  @file = Pathname.new(file)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/rspec/scaffold/runner.rb', line 4

def file
  @file
end

Instance Method Details

#generate_spec(ruby_file) ⇒ Object

Private



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/scaffold/runner.rb', line 31

def generate_spec(ruby_file)
  spec = RSpec::Scaffold::Generator.new Pathname.new(File.expand_path(ruby_file))
  if spec.funcs.any?
    spec.perform
  else
    log "- #{ruby_file} - no methods", :gray
    nil
  end
rescue => e
  log "! #{ruby_file} - #{e.inspect.gsub /^#<|>$/, ''}", :red
end

#log(msg = nil, color = :green) ⇒ Object



72
73
74
# File 'lib/rspec/scaffold/runner.rb', line 72

def log(msg = nil, color = :green)
  HighLine.new.say %Q(  <%= color('#{msg}', :#{color}) %>)
end

#performObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/scaffold/runner.rb', line 11

def perform
  fail ArgumentError, %Q(File or directory does not exist: "#{file}") if !File.exists?(file) && !File.exists?("#{file}.rb")
  ruby_files.each do |ruby_file|
    rspec_file = Pathname.new(spec_file(ruby_file))
    spec_file_path = rspec_file.to_s[%r|/(spec/.+)|, 1]
    next if rspec_file.exist?.tap { |exists| log "- #{spec_file_path} - already exists", :gray if exists }
    spec = generate_spec(ruby_file)
    next unless spec
    log "+ #{spec_file_path}"
    FileUtils.mkdir_p(rspec_file.parent)
    File.open(rspec_file, 'wb') do |f|
      f << spec.join("\n")
    end
  end
end

#ruby_filesObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/scaffold/runner.rb', line 43

def ruby_files
  if File.directory?(file)
    Dir[File.join(file, '**', '*.rb')]
  else
    if file.extname.empty?
      ["#{file}.rb"]
    else
      [file]
    end
  end
end

#spec_file(ruby_file) ⇒ Object

Note:

subbing out /app/ is Rails specific



56
57
58
# File 'lib/rspec/scaffold/runner.rb', line 56

def spec_file(ruby_file)
  File.join(spec_path, "#{specify(ruby_file)}").sub '/app/', '/'
end

#spec_pathObject



64
65
66
67
68
69
70
# File 'lib/rspec/scaffold/runner.rb', line 64

def spec_path
  if File.directory?(File.expand_path('spec'))
    File.expand_path('spec')
  else
    fail "Couldn't find spec directory"
  end
end

#specify(file_name) ⇒ Object



60
61
62
# File 'lib/rspec/scaffold/runner.rb', line 60

def specify(file_name)
  file_name.sub('.rb', '_spec.rb')
end