martin 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Lewis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ = Martin
2
+
3
+ An ode to the Rat Pack (Dean Martin) with a Sinatra style DSL for creating command line interfaces to your applications
4
+
5
+ == Example
6
+
7
+ calc.rb
8
+ class Calculator
9
+ def add(*nums)
10
+ nums.reduce(:+)
11
+ end
12
+ end
13
+
14
+ app.rb
15
+ require File.dirname(__FILE__) + 'calc'
16
+ require 'martin'
17
+
18
+ configure do
19
+ @calc = Calculator.new
20
+ end
21
+
22
+ command /add (\d+) to (\d+)/ do |a, b|
23
+ puts @calc.add(a.to_i, b.to_i)
24
+ end
25
+
26
+ error do |input|
27
+ puts 'Error: command ' + input.split.first + ' not found.'
28
+ end
29
+
30
+ And that's it! Now run `ruby app add 15 to 25` and the application
31
+ should return `40`
32
+
33
+ == Note on Patches/Pull Requests
34
+
35
+ * Fork the project.
36
+ * Make your feature addition or bug fix.
37
+ * Add tests for it. This is important so I don't break it in a
38
+ future version unintentionally.
39
+ * Commit, do not mess with rakefile, version, or history.
40
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
41
+ * Send me a pull request. Bonus points for topic branches.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2010 Ryan Lewis. See LICENSE for details.
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "martin"
10
+ gem.summary = %Q{A Sinatra style DSL for creating command line applications}
11
+ gem.description = %Q{An ode to the Rat Pack (Dean Martin) with a Sinatra style DSL for creating command line interfaces to your applications}
12
+ gem.email = "c00lryguy@gmail.com"
13
+ gem.homepage = "http://github.com/c00lryguy/martin"
14
+ gem.authors = ["Ryan Lewis"]
15
+ # gem.add_development_dependency "shoulda", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ end
27
+
28
+ task :test => :check_dependencies
29
+
30
+ task :default => :test
31
+
32
+ Rake::RDocTask.new do |rdoc|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
34
+
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = "martin #{version}"
37
+ rdoc.rdoc_files.include('README*')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,5 @@
1
+ libdir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
3
+
4
+ require 'martin/base'
5
+ require 'martin/application'
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module Martin
4
+ # Martin delegation mixin. Mixing this module into an object causes all
5
+ # methods to be delegated to the Martin::Application class. Used primarily
6
+ # at the top-level.
7
+ # 100% ripped from Sinatra::Delegator
8
+ module Delegator #:nodoc:
9
+ def self.delegate(*methods)
10
+ methods.each do |method_name|
11
+ eval <<-RUBY, binding, '(__DELEGATE__)', 1
12
+ def #{method_name}(*args, &b)
13
+ ::Martin::Application.send(#{method_name.inspect}, *args, &b)
14
+ end
15
+ private #{method_name.inspect}
16
+ RUBY
17
+ end
18
+ end
19
+
20
+ delegate :configure, :command, :error, :run
21
+ end
22
+
23
+ # This is the class that gets loaded if you just require 'martin'
24
+ class Application < Base
25
+ error do |input|
26
+ puts 'Could not find method ' + input.split.first
27
+ end
28
+ end
29
+
30
+ at_exit { Application.run }
31
+ end
32
+
33
+ include Martin::Delegator
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/dsl'
2
+
3
+ # An ode to the Rat Pack (Dean Martin) with a Sinatra style DSL for creating
4
+ # Command Line Interfaces to your applications
5
+ #
6
+ # @example
7
+ module Martin
8
+ module InstanceMethods
9
+
10
+ end
11
+
12
+ module ClassMethods
13
+ Configures, Commands, Errors = [], [], []
14
+
15
+ # Runs once, at the beginning of the application
16
+ # If there are multiple configure blocks, then each one will run in the
17
+ # order the were created.
18
+ #
19
+ # @param [Regexp] cmd the regexp you want to match to run the command
20
+ def configure(&block)
21
+ Configures << DSL::Configure.new(block)
22
+ end
23
+
24
+ # Adds a command
25
+ #
26
+ # @param [Regexp] cmd the regexp you want to match to run the command
27
+ def command(regexp, &block)
28
+ Commands << DSL::Command.new(regexp, block)
29
+ end
30
+
31
+ # When the user input matches non of the user's commands then this
32
+ # method will run
33
+ # If there are multiple error blocks, then each one will run in the
34
+ # order the were created.
35
+ #
36
+ # @param [Regexp] cmd the regexp you want to match to run the command
37
+ def error(&block)
38
+ Errors << DSL::Error.new(block)
39
+ end
40
+
41
+ # Runs the application on the given arguments
42
+ #
43
+ # @param [String] args The arguments to run the application on. Default is ARGV.join(' ')
44
+ def run(args = ARGV.join(' '))
45
+ got_error = false
46
+ Configures.each do |configure|
47
+ configure.block.call
48
+ end
49
+ Commands.each do |command|
50
+ match = command.regexp.match(args)
51
+ unless match.nil?
52
+ command.block.call(*match.captures)
53
+ got_error = false
54
+ else
55
+ got_error = true
56
+ end
57
+ end
58
+ Errors.each do |error|
59
+ error.block.call(args) if got_error
60
+ end
61
+ self
62
+ end
63
+ end
64
+
65
+ # The base class for command line applications
66
+ # Subclass this class for the functionality
67
+ class Base
68
+ def self.inherited(base)
69
+ extend ClassMethods
70
+ include InstanceMethods
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,39 @@
1
+ class Object
2
+ # Throw a TypeError unless this object's type is cls
3
+ #
4
+ # @param cls The class this object should be
5
+ def should_be_a(cls)
6
+ raise(TypeError, "#{self.to_s} must be of type #{cls.to_s}") unless self.is_a?(cls)
7
+ end
8
+ end
9
+
10
+ module Martin
11
+ module DSL
12
+ # This is just an Array that checks the type of the object being pushed to it
13
+ class TypeList < Array
14
+ def <<(obj)
15
+ obj.should_be_a(@containing_type || Object)
16
+ super
17
+ end
18
+ end
19
+
20
+ class Method
21
+ attr_accessor :block
22
+ def initialize(block)
23
+ block.should_be_a(Proc)
24
+ @block = block
25
+ end
26
+ end
27
+
28
+ class Configure < Method; end
29
+ class Command < Method
30
+ attr_accessor :regexp
31
+ def initialize(regexp, block)
32
+ super(block)
33
+ regexp.should_be_a(Regexp)
34
+ @regexp = regexp
35
+ end
36
+ end
37
+ class Error < Method; end
38
+ end
39
+ end
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{martin}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Lewis"]
12
+ s.date = %q{2010-04-17}
13
+ s.description = %q{An ode to the Rat Pack (Dean Martin) with a Sinatra style DSL for creating command line interfaces to your applications}
14
+ s.email = %q{c00lryguy@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/martin.rb",
27
+ "lib/martin/application.rb",
28
+ "lib/martin/base.rb",
29
+ "lib/martin/dsl.rb",
30
+ "martin.gemspec",
31
+ "test/helper.rb",
32
+ "test/test_martin.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/c00lryguy/martin}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{A Sinatra style DSL for creating command line applications}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_martin.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ else
50
+ end
51
+ else
52
+ end
53
+ end
54
+
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'martin/base'
8
+
9
+ class Test::Unit::TestCase
10
+ end
11
+
12
+ class Calculator
13
+ def add(*nums)
14
+ nums.reduce(:+)
15
+ end
16
+ end
17
+
18
+ class CLI < Martin::Base
19
+ def CLI.answer; @answer; end
20
+
21
+ configure do
22
+ @calc = Calculator.new
23
+ end
24
+
25
+ command /add (\d+) to (\d+)/ do |a, b|
26
+ @answer = @calc.add(a.to_i, b.to_i)
27
+ end
28
+
29
+ error do |input|
30
+ @answer = 'Error'
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+
3
+ class TestMartin < Test::Unit::TestCase
4
+ context 'A CLI instance' do
5
+ should 'output 30' do
6
+ CLI.run('add 20 to 10')
7
+ assert_equal 30, CLI.answer
8
+ end
9
+
10
+ should 'output error message' do
11
+ CLI.run('subtract 15 from 25')
12
+ assert_equal 'Error', CLI.answer
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: martin
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Ryan Lewis
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: An ode to the Rat Pack (Dean Martin) with a Sinatra style DSL for creating command line interfaces to your applications
22
+ email: c00lryguy@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/martin.rb
38
+ - lib/martin/application.rb
39
+ - lib/martin/base.rb
40
+ - lib/martin/dsl.rb
41
+ - martin.gemspec
42
+ - test/helper.rb
43
+ - test/test_martin.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/c00lryguy/martin
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A Sinatra style DSL for creating command line applications
74
+ test_files:
75
+ - test/helper.rb
76
+ - test/test_martin.rb