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.
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
.doubles ⇒ Object (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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
89 90 91 |
# File 'lib/aruba-doubles/double.rb', line 89 def filename @filename end |
#output ⇒ Object (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
.all ⇒ Array<ArubaDoubles::Double>
Return all registered doubles.
56 57 58 |
# File 'lib/aruba-doubles/double.rb', line 56 def all self.doubles.values end |
.bindir ⇒ String
Return the doubles directory.
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 |
.each ⇒ Object
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 |
.setup ⇒ Object
Setup the doubles environment.
11 12 13 |
# File 'lib/aruba-doubles/double.rb', line 11 def setup patch_path end |
.teardown ⇒ Object
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.
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 |
#delete ⇒ Object
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_ruby ⇒ String
Export the double to executable Ruby code.
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.('..', 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 |