Class: ArubaDoubles::Double
- Inherits:
-
Object
- Object
- ArubaDoubles::Double
- Defined in:
- lib/aruba-doubles/double.rb
Class Attribute Summary collapse
-
.doubles ⇒ Object
readonly
Returns the value of attribute doubles.
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
-
.all ⇒ Array<ArubaDoubles::Double>
Return all registered doubles.
-
.bindir ⇒ String
Return the doubles directory.
-
.create(filename, &block) ⇒ Object
Initialize and create a new double.
-
.each ⇒ Object
Iterate over all registered doubles.
- .find(filename) ⇒ Object
-
.run(&block) ⇒ Object
Initialize and run a new double.
-
.setup ⇒ Object
Setup the doubles environment.
-
.teardown ⇒ Object
Teardown the doubles environment.
Instance Method Summary collapse
-
#create(&block) ⇒ String
Create the executable double.
-
#delete ⇒ Object
Delete the executable double.
-
#initialize(cmd, default_output = {}, &block) ⇒ ArubaDoubles::Double
constructor
Instantiate and register new double.
-
#on(argv, output = nil) ⇒ Object
Add ARGV matcher with output.
-
#run(argv = ARGV) ⇒ Object
Run the double.
-
#to_ruby ⇒ String
Export the double to executable Ruby code.
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
.doubles ⇒ Object (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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
92 93 94 |
# File 'lib/aruba-doubles/double.rb', line 92 def filename @filename end |
#output ⇒ Object (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
.all ⇒ Array<ArubaDoubles::Double>
Return all registered doubles.
59 60 61 |
# File 'lib/aruba-doubles/double.rb', line 59 def all self.doubles.values end |
.bindir ⇒ String
Return the doubles directory.
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 |
.each ⇒ Object
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 |
.setup ⇒ Object
Setup the doubles environment.
12 13 14 |
# File 'lib/aruba-doubles/double.rb', line 12 def setup patch_path end |
.teardown ⇒ Object
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.
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 |
#delete ⇒ Object
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_ruby ⇒ String
Export the double to executable Ruby code.
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.('..', 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 |