argumentative 0.0.1beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dotfile_linker.gemspec
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^test/(.*)_test\.rb|)
3
+ watch(%r|^lib/(.*)\.rb|) { "test" }
4
+ watch(%r|^test/test_helper\.rb|) { "test" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dillon Kearns
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # Argumentative
2
+
3
+ For flexible argument processing in a readable, declarative style!
4
+
5
+ ```ruby
6
+ include Argumentative
7
+
8
+ def method_with_flexible_args(*args)
9
+ argumentative(args) do
10
+ when_type(String) do |string|
11
+ "I was called with a string (#{string})"
12
+ end
13
+
14
+ when_type(Numeric) do |number|
15
+ "I was called with a number (#{number})"
16
+ end
17
+
18
+ when_type(String.*, Hash) do |*strings, options|
19
+ "I got strings #{strings.inspect} and options #{options.inspect}"
20
+ end
21
+ end
22
+ end
23
+
24
+ method_with_flexible_args('string') # => "I was called with a string (string)"
25
+ method_with_flexible_args(123.5) # => "I was called with a number (123.5)"
26
+ method_with_flexible_args('one', 'two', 'three', :four => 4)
27
+ # => 'I got strings ["one", "two", "three"] and options {:four=>4}'
28
+ ```
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ require File.expand_path("../lib/argumentative/version", __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Dillon Kearns"]
5
+ gem.email = ["dillon@dillonkearns.com"]
6
+ gem.description = "Flexible argument processing in a readable, declarative style!"
7
+ gem.summary = gem.description
8
+ gem.homepage = "https://github.com/dillonkearns/argumentative"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "argumentative"
14
+ gem.require_paths = ['lib']
15
+ gem.version = Argumentative::VERSION
16
+
17
+ gem.required_ruby_version = ">=1.9.3"
18
+
19
+ gem.add_development_dependency "rake", "~> 10.0.3"
20
+ gem.add_development_dependency "minitest", "~> 4.6.2"
21
+ gem.add_development_dependency "minitest-reporters"
22
+ gem.add_development_dependency "mocha", "~> 0.13.2"
23
+ gem.add_development_dependency "guard-minitest"
24
+ gem.add_development_dependency "growl"
25
+ gem.add_development_dependency "rb-fsevent"
26
+ gem.add_development_dependency "coveralls"
27
+ end
@@ -0,0 +1,83 @@
1
+ module Argumentative
2
+ def argumentative(args)
3
+ @@checker = Argumentative.new(*args)
4
+ yield
5
+ raise ArgumentError.new("No matches found for #{@@checker.args.inspect}") unless @@checker.found_match?
6
+ return_value = @@checker.return_value
7
+ @@checker = nil
8
+ return_value
9
+ end
10
+
11
+ def when_type(*types)
12
+ @@checker.check_match(types) do
13
+ yield(*@@checker.args)
14
+ end
15
+ end
16
+
17
+ module ParameterMatchers
18
+ class Base
19
+ def match?(types)
20
+ raise 'Called abstract method match?'
21
+ end
22
+ end
23
+
24
+ class Star < Base
25
+ def initialize(klass)
26
+ @klass = klass
27
+ end
28
+
29
+ def match?(types)
30
+ true
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+ class Argumentative
37
+ attr_reader :args
38
+
39
+ def initialize(*args)
40
+ @args = args
41
+ @@handlers = []
42
+ @found_match = false
43
+ end
44
+
45
+ def check_match(types)
46
+ if match?(types)
47
+ @return_value = yield
48
+ @found_match = true
49
+ end
50
+ end
51
+
52
+ def found_match?
53
+ @found_match
54
+ end
55
+
56
+ def return_value
57
+ @return_value
58
+ end
59
+
60
+ private
61
+ def contain_matchers?(types)
62
+ types.any? { |type| type.is_a?(ParameterMatchers::Base) }
63
+ end
64
+
65
+ def fuzzy_match?(types)
66
+ true
67
+ end
68
+
69
+ def match?(types)
70
+ if contain_matchers?(types)
71
+ fuzzy_match?(types)
72
+ elsif types.count == @args.count
73
+ @args.zip(types).all? { |arg, type| arg.is_a?(type) }
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ class Class
80
+ def *
81
+ Argumentative::ParameterMatchers::Star.new(self)
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module Argumentative
2
+ VERSION = "0.0.1beta"
3
+ end
@@ -0,0 +1,71 @@
1
+ require_relative 'test_helper'
2
+
3
+ require_relative '../lib/argumentative'
4
+ require 'minitest/autorun'
5
+
6
+
7
+ describe Argumentative do
8
+ include Mocha::Integration::MiniTest
9
+ include Argumentative
10
+ it 'raises error when type not handled' do
11
+ def flexible_args_method(*args)
12
+ argumentative(args) do
13
+ when_type(Array) { raise "Shouldn't evaluate non-matching block" }
14
+ when_type(Numeric) { raise "Shouldn't evaluate non-matching block" }
15
+ end
16
+ end
17
+
18
+ assert_raises(ArgumentError) { flexible_args_method("some string") }
19
+ end
20
+
21
+ it 'runs clause for type when first matches' do
22
+ def flexible_args_method(*args)
23
+ argumentative(args) do
24
+ when_type(Numeric) { raise "Shouldn't evaluate non-matching block" }
25
+ when_type(String) { 'Handled String' }
26
+ end
27
+ end
28
+
29
+ assert_equal 'Handled String', flexible_args_method("some string")
30
+ end
31
+
32
+ it 'runs clause for type when last matches' do
33
+ def flexible_args_method(*args)
34
+ argumentative(args) do
35
+ when_type(String) { 'Handled String' }
36
+ when_type(Numeric) { raise "Shouldn't evaluate non-matching block" }
37
+ end
38
+ end
39
+
40
+ assert_equal 'Handled String', flexible_args_method("some string")
41
+ end
42
+
43
+ it 'passes original args through to the executed block' do
44
+ def flexible_args_method(*args)
45
+ argumentative(args) do
46
+ when_type(String) do |string_arg|
47
+ assert_equal "some string", string_arg
48
+ 'Handled String'
49
+ end
50
+
51
+ when_type(Numeric) { raise "Shouldn't evaluate non-matching block" }
52
+ end
53
+ end
54
+
55
+ assert_equal 'Handled String', flexible_args_method("some string")
56
+ end
57
+
58
+ it 'matches Class.*' do
59
+ def flexible_args_method(*args)
60
+ argumentative(args) do
61
+ when_type(String, Hash) { raise "Shouldn't evaluate non-matching block" }
62
+
63
+ when_type(String.*) do |*strings|
64
+ "Matched String.* with #{strings.inspect}"
65
+ end
66
+ end
67
+ end
68
+
69
+ assert_equal 'Matched String.* with ["one", "two", "three"]', flexible_args_method('one', 'two', 'three')
70
+ end
71
+ end
@@ -0,0 +1,10 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require "minitest/autorun"
5
+ require "minitest/reporters"
6
+
7
+ # for attaching tests to rubymine
8
+ MiniTest::Reporters.use! if ENV['RUBYMINE']
9
+
10
+ require 'mocha/setup'
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: argumentative
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1beta
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Dillon Kearns
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 10.0.3
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 10.0.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 4.6.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 4.6.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest-reporters
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mocha
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.13.2
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.13.2
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard-minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: growl
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rb-fsevent
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: coveralls
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: Flexible argument processing in a readable, declarative style!
143
+ email:
144
+ - dillon@dillonkearns.com
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - .gitignore
150
+ - .travis.yml
151
+ - Gemfile
152
+ - Guardfile
153
+ - LICENSE
154
+ - README.md
155
+ - Rakefile
156
+ - argumentative.gemspec
157
+ - lib/argumentative.rb
158
+ - lib/argumentative/version.rb
159
+ - test/argumentative_test.rb
160
+ - test/test_helper.rb
161
+ homepage: https://github.com/dillonkearns/argumentative
162
+ licenses: []
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ none: false
169
+ requirements:
170
+ - - ! '>='
171
+ - !ruby/object:Gem::Version
172
+ version: 1.9.3
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ! '>'
177
+ - !ruby/object:Gem::Version
178
+ version: 1.3.1
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 1.8.23
182
+ signing_key:
183
+ specification_version: 3
184
+ summary: Flexible argument processing in a readable, declarative style!
185
+ test_files:
186
+ - test/argumentative_test.rb
187
+ - test/test_helper.rb