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.



93
94
95
96
97
98
# File 'lib/aruba-doubles/double.rb', line 93

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.



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

def doubles
  @doubles
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



89
90
91
# File 'lib/aruba-doubles/double.rb', line 89

def filename
  @filename
end

#outputObject (readonly)

Returns the value of attribute output.



89
90
91
# File 'lib/aruba-doubles/double.rb', line 89

def output
  @output
end

Class Method Details

.allArray<ArubaDoubles::Double>

Return all registered doubles.

Returns:



56
57
58
# File 'lib/aruba-doubles/double.rb', line 56

def all
  self.doubles.values
end

.bindirString

Return the doubles directory.

Returns:

  • (String)


41
42
43
# File 'lib/aruba-doubles/double.rb', line 41

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.



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

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

.eachObject

Iterate over all registered doubles.



50
51
52
# File 'lib/aruba-doubles/double.rb', line 50

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

.find(filename) ⇒ Object



45
46
47
# File 'lib/aruba-doubles/double.rb', line 45

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.



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

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

.setupObject

Setup the doubles environment.



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

def setup
  patch_path
end

.teardownObject

Teardown the doubles environment.



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

def teardown
  delete_all
  restore_path
end

Instance Method Details

#create(&block) ⇒ String

Create the executable double.

Returns:

  • (String)

    full path to the double.



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/aruba-doubles/double.rb', line 117

def create(&block)
  self.instance_eval(&block) if block_given?
  self.class.doubles[@filename] = self
  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.



144
145
146
147
# File 'lib/aruba-doubles/double.rb', line 144

def delete
  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.



101
102
103
# File 'lib/aruba-doubles/double.rb', line 101

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

#run(argv = ARGV) ⇒ Object

Run the double.

This will actually display any outputs if defined and exit.



108
109
110
111
112
113
# File 'lib/aruba-doubles/double.rb', line 108

def run(argv = 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



133
134
135
136
137
138
139
140
141
# File 'lib/aruba-doubles/double.rb', line 133

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