Class: Wire::SpecWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/wire/commands/spec_writer.rb

Overview

SpecWriter is able to create a directory structure according to basic serverspec needs and fill in the templates

Instance Method Summary collapse

Constructor Details

#initialize(target_dir, spec_contents) ⇒ SpecWriter

create SpecWriter in target_dir directory with given spec_contents



15
16
17
18
# File 'lib/wire/commands/spec_writer.rb', line 15

def initialize(target_dir, spec_contents)
  @target_dir = target_dir
  @spec_contents = spec_contents
end

Instance Method Details

#ensure_directory_structureObject

make sure that we have a rspec-conformant dir structure



27
28
29
30
31
# File 'lib/wire/commands/spec_writer.rb', line 27

def ensure_directory_structure
  ensure_directory @target_dir
  ensure_directory File.join(@target_dir, 'spec')
  ensure_directory File.join(@target_dir, 'spec', 'localhost')
end

#ensure_filesObject

ensures that all serverspec skeleton files such as Rakefile, spec_helper etc. exist Then writes the models specification files into the skeleton



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
# File 'lib/wire/commands/spec_writer.rb', line 43

def ensure_files
  rakefile_name = File.join(@target_dir, 'Rakefile')
  file?(rakefile_name) || File.open(rakefile_name, 'w') do |file|
    write_template(SpecTemplates.template_rakefile, file)
  end

  spechelper_name = File.join(@target_dir, 'spec', 'spec_helper.rb')
  file?(spechelper_name) || File.open(spechelper_name, 'w') do |file|
    write_template(SpecTemplates.template_spec_helper, file)
  end

  specfile_name = File.join(@target_dir, 'spec', 'localhost', 'wire_spec.rb')
  File.open(specfile_name, 'w') do |file|
    template = <<ERB
require 'spec_helper.rb'

# begin of generated specs

<%= @spec_contents.join('\n') %>

# end of spec file
ERB
    write_template(template, file)
  end
end

#writeObject

writes spec to disk



21
22
23
24
# File 'lib/wire/commands/spec_writer.rb', line 21

def write
  ensure_directory_structure
  ensure_files
end

#write_template(template, file) ⇒ Object

writes erb template to open file object



34
35
36
37
# File 'lib/wire/commands/spec_writer.rb', line 34

def write_template(template, file)
  erb = ERB.new(template, nil, '%')
  file.puts(erb.result(binding))
end