Class: ArubaDoubles::Double

Inherits:
Object
  • Object
show all
Defined in:
lib/aruba-doubles/double.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, default_output = {}, &block) ⇒ ArubaDoubles::Double

Instantiate and register new double.



97
98
99
100
101
102
# File 'lib/aruba-doubles/double.rb', line 97

def initialize(cmd, default_output = {}, &block)
  @filename = cmd
  @default_output = {:puts => nil, :warn => nil, :exit => nil}.merge(default_output)
  @outputs = {}
  self.instance_eval(&block) if block_given?
end

Class Attribute Details

.doublesObject (readonly)

Returns the value of attribute doubles.



9
10
11
# File 'lib/aruba-doubles/double.rb', line 9

def doubles
  @doubles
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



92
93
94
# File 'lib/aruba-doubles/double.rb', line 92

def filename
  @filename
end

#outputObject (readonly)

Returns the value of attribute output.



92
93
94
# File 'lib/aruba-doubles/double.rb', line 92

def output
  @output
end

Class Method Details

.allArray<ArubaDoubles::Double>

Return all registered doubles.

Returns:



59
60
61
# File 'lib/aruba-doubles/double.rb', line 59

def all
  self.doubles.values
end

.bindirString

Return the doubles directory.

Returns:

  • (String)


43
44
45
# File 'lib/aruba-doubles/double.rb', line 43

def bindir
  @bindir ||= Dir.mktmpdir
end

.create(filename, &block) ⇒ Object

Initialize and create a new double.

It accepts an optional block to setup define the doubles output.



25
26
27
28
29
# File 'lib/aruba-doubles/double.rb', line 25

def create(filename, &block)
  double = new(filename)
  double.instance_eval(&block) if block_given?
  double.create
end

.eachObject

Iterate over all registered doubles.



52
53
54
# File 'lib/aruba-doubles/double.rb', line 52

def each
  all.each { |double| yield(double) }
end

.find(filename) ⇒ Object



47
48
49
# File 'lib/aruba-doubles/double.rb', line 47

def find(filename)
  self.doubles[filename]
end

.run(&block) ⇒ Object

Initialize and run a new double.

It accepts an optional block to setup define the doubles output.



34
35
36
37
38
# File 'lib/aruba-doubles/double.rb', line 34

def run(&block)
  double = new(File.basename($PROGRAM_NAME))
  double.instance_eval(&block) if block_given?
  double.run
end

.setupObject

Setup the doubles environment.



12
13
14
# File 'lib/aruba-doubles/double.rb', line 12

def setup
  patch_path
end

.teardownObject

Teardown the doubles environment.



17
18
19
20
# File 'lib/aruba-doubles/double.rb', line 17

def teardown
  delete_all
  restore_path
end

Instance Method Details

#create(&block) ⇒ String

Create the executable double.

Returns:

  • (String)

    full path to the double.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aruba-doubles/double.rb', line 124

def create(&block)
  register
  self.instance_eval(&block) if block_given?
  content = self.to_ruby
  fullpath = File.join(self.class.bindir, filename)
  #puts "creating double: #{fullpath} with content:\n#{content}" # debug
  f = File.open(fullpath, 'w')
  f.puts content
  f.close
  FileUtils.chmod(0755, File.join(self.class.bindir, filename))
  self
end

#deleteObject

Delete the executable double.



151
152
153
154
155
# File 'lib/aruba-doubles/double.rb', line 151

def delete
  deregister
  fullpath = File.join(self.class.bindir, filename)
  FileUtils.rm(fullpath) if File.exists?(fullpath)
end

#on(argv, output = nil) ⇒ Object

Add ARGV matcher with output.



105
106
107
# File 'lib/aruba-doubles/double.rb', line 105

def on(argv, output = nil)
  @outputs[argv] = output
end

#run(argv = ARGV) ⇒ Object

Run the double.

This will append the call to the doubles history, display any outputs if defined and exit.



113
114
115
116
117
118
119
# File 'lib/aruba-doubles/double.rb', line 113

def run(argv = ARGV)
  history << [filename] + argv
  output = @outputs[argv] || @default_output
  puts output[:puts] if output[:puts]
  warn output[:warn] if output[:warn]
  exit output[:exit] if output[:exit]
end

#to_rubyString

Export the double to executable Ruby code.

Returns:

  • (String)

    serialized double



140
141
142
143
144
145
146
147
148
# File 'lib/aruba-doubles/double.rb', line 140

def to_ruby
  ruby = ['#!/usr/bin/env ruby']
  ruby << "$: << '#{File.expand_path('..', File.dirname(__FILE__))}'"
  ruby << 'require "aruba-doubles"'
  ruby << 'ArubaDoubles::Double.run do'
  @outputs.each_pair { |argv,output| ruby << "  on #{argv.inspect}, #{output.inspect}" }
  ruby << 'end'
  ruby.join("\n")
end