From 622c2c1bb4f96a396083616ad8146288e397c3e0 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 13 Jul 2011 10:22:13 -0400 Subject: [PATCH] add_command support, make things even tighter --- README.md | 12 ++++++++++++ lib/iterm_window.rb | 4 ++++ spec/iterm_window_spec.rb | 18 ++++++++++++++++++ spec/spec_helper.rb | 6 ++++++ 4 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 9a929c7..1b49812 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,18 @@ In a nutshell: * `:dir` changes all tabs to the given directory before executing commands. +### Adding commands that tabs can execute + +You can define your own tab commands with `ItermWindow.add_command`: + +``` ruby +ItermWindow.add_command :guard do |group = nil| + command = "bundle exec guard" + command << " -g #{group}" if group + write_text command +end +``` + More docs coming soon! Also, look at `lib/iterm_window.rb` for more usage examples. * Developed March 17, 2008 by Chris Powers diff --git a/lib/iterm_window.rb b/lib/iterm_window.rb index 49b0b52..81b9031 100755 --- a/lib/iterm_window.rb +++ b/lib/iterm_window.rb @@ -81,6 +81,10 @@ class ItermWindow def colors @colors end + + def add_command(name, &block) + ItermWindow::Tab.send(:define_method, name, block) + end end ItermWindow.colors = { diff --git a/spec/iterm_window_spec.rb b/spec/iterm_window_spec.rb index bafcb97..a05b326 100644 --- a/spec/iterm_window_spec.rb +++ b/spec/iterm_window_spec.rb @@ -39,6 +39,24 @@ FILE end end + describe '.add_command' do + before do + ItermWindow::Tab.send(:remove_method, :test) rescue nil + end + + it 'should define a method on the Tab class' do + ItermWindow.add_command :test do |params| + params + end + + ItermWindow::Tab.should have_a_method_named(:test, 1) + end + + after do + ItermWindow::Tab.send(:remove_method, :test) rescue nil + end + end + describe "opening a tab (example 1)" do before(:each) do ItermWindow.expects(:new).returns(@window) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d472384..26a7ee7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,3 +2,9 @@ RSpec.configure do |c| c.mock_with :mocha end +RSpec::Matchers.define :have_a_method_named do |name, arity| + match do |mod| + mod.method_defined?(name).should be_true + mod.instance_method(name).arity.should == arity + end +end