class Wire::SpecWriter

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

Public Class Methods

new(target_dir, spec_contents) click to toggle source

create SpecWriter in target_dir directory with given spec_contents

# File lib/wire/commands/spec_writer.rb, line 15
def initialize(target_dir, spec_contents)
  @target_dir = target_dir
  @spec_contents = spec_contents
end

Public Instance Methods

ensure_directory_structure() click to toggle source

make sure that we have a rspec-conformant dir structure

# 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_files() click to toggle source

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

# 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
write() click to toggle source

writes spec to disk

# File lib/wire/commands/spec_writer.rb, line 21
def write
  ensure_directory_structure
  ensure_files
end
write_template(template, file) click to toggle source

writes erb template to open file object

# 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

Private Instance Methods

ensure_directory(target_dir) click to toggle source

make sure that target_dir exists

# File lib/wire/commands/spec_writer.rb, line 72
def ensure_directory(target_dir)
  return if File.exist?(target_dir)
  begin
    FileUtils.mkdir_p(target_dir)
  rescue => excpt
    $log.error "ERROR: Unable to create #{target_dir}: #{excpt}"
  end
end
file?(target_file) click to toggle source

checks if target_file exists

# File lib/wire/commands/spec_writer.rb, line 82
def file?(target_file)
  File.exist?(target_file) && File.file?(target_file)
end