Class: RSpec::Scaffold::Generator

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

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Generator

Generates an array of lines that can be joined into an RSpec file based.

Parameters:

  • parser (Ryan, #name, #funcs, #initialization_args, #class?, #module?)

    object that is used to build the rspec file



8
9
10
# File 'lib/rspec/scaffold/generator.rb', line 8

def initialize(parser)
  @parser = parser
end

Instance Method Details

#perform(parser = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rspec/scaffold/generator.rb', line 12

def perform(parser=nil)
  # for backwards compat with arg to perform
  @parser = (parser.nil? ? @parser : parser)

  indent = (' ' * 2)
  second_indent = indent * 2

  # header
  lines = [
    %Q(# spring rspec),
    %Q(describe #{@parser.name} do)
  ]

  if @parser.class?
    @parser.initialization_args.each do |arg|
      lines << %Q(#{indent}let(:#{arg.to_s.sub(/^[&*]/, '')}) {})
    end
    lines << %Q()
    lines << %Q(#{indent}subject { described_class.new #{@parser.initialization_args.join(', ')} })
  elsif @parser.module?
    lines << %Q(#{indent}subject { Class.new { include #{@parser.name} }.new })
  end

  lines << %Q()

  # handle Rails model scopes
  if scope_definitions.any?
    lines << %Q|#{indent}describe 'Scopes' do|

    scope_definitions.each do |scope_name|
      lines << %Q|#{second_indent}describe '.#{scope_name}' do|
      lines << %Q|#{' ' * 6}xit "should collect TODO" do|
      lines << %Q|#{' ' * 6}end|
      lines << %Q|#{second_indent}end|
      lines << %Q||
    end

    lines << %Q|#{indent}end|
    lines << %Q()
  end

  # handle class and instance methods
  @parser.funcs.reject(&:private?).each do |func|
    lines << %Q(#{indent}describe "#{func.class? ? '.' : '#'}#{func.name}" do)
    func.assignments.each do |assignment|
      lines << %Q(#{second_indent}it "#{assignment}" do) << %Q(#{second_indent}end)
    end
    lines << %Q() if func.conditions.any? and func.assignments.any?
    func.conditions.each do |condition|
      lines.concat ConditionExhibit.new(condition, second_indent).render
    end
    lines << %Q(#{indent}end) << %Q()
  end

  lines << %Q(end) << %Q()

  return lines
end