method_man 0.0.1

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,21 @@
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
18
+
19
+ # ignore RubyMine project files
20
+ .idea
21
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,2 @@
1
+ 1.9.3-p374
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in method_man.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Clay Shentrup
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Clay Shentrup
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,31 @@
1
+ # Method Man
2
+
3
+ Defines a MethodObject class which implements Kent Beck's "[method object](http://c2.com/cgi/wiki?MethodObject)".
4
+
5
+ ## Example
6
+
7
+ ```ruby
8
+ require 'method_object'
9
+
10
+ MakeArbitraryArray = MethodObject.new do
11
+ def call(name, age = 21, &test_block)
12
+ [shortname, age, yield(1), test_value]
13
+ end
14
+
15
+ private
16
+
17
+ def shortname
18
+ # all passed (not optional default) arguments in the
19
+ # signature for call() are available as instance methods
20
+ name.slice(0,4).upcase
21
+ end
22
+
23
+ def test_block_value
24
+ # block is available to be called by name
25
+ test_block.call(2)
26
+ end
27
+ end
28
+
29
+ MakeArbitraryArray.call(name: 'Elliot') { |input| input + 1 }
30
+ => ["ELLI", 21, 2, 3]
31
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,63 @@
1
+ require 'method_object/version'
2
+ require 'method_object/base'
3
+
4
+ class MethodObject
5
+ def self.new
6
+ super.call
7
+ end
8
+
9
+ def initialize(&definition_block)
10
+ @definition_block = definition_block
11
+ end
12
+
13
+ def call
14
+ add_generator
15
+ add_getters
16
+ method_object_class
17
+ end
18
+
19
+ def block_name
20
+ block_parameter.fetch(1)
21
+ end
22
+
23
+ def non_block_parameter_names
24
+ @non_block_parameter_names ||= non_block_parameters.map { |parameter| parameter.fetch(1) }
25
+ end
26
+
27
+ def block_parameter
28
+ @block_parameter ||= parameters.find { |parameter| parameter[0] == :block }
29
+ end
30
+
31
+ private
32
+
33
+ def add_generator
34
+ method_object_class.instance_variable_set(:@generator, self)
35
+ end
36
+
37
+ def method_object_class
38
+ @method_object_class ||= Class.new(Base, &@definition_block)
39
+ end
40
+
41
+ def add_getters
42
+ add_parameter_getters
43
+ add_block_getter if block_parameter
44
+ end
45
+
46
+ def add_parameter_getters
47
+ non_block_parameter_names.each do |parameter_name|
48
+ method_object_class.send(:attr_reader, parameter_name)
49
+ end
50
+ end
51
+
52
+ def add_block_getter
53
+ method_object_class.send(:attr_reader, block_name)
54
+ end
55
+
56
+ def parameters
57
+ method_object_class.instance_method(:call).parameters
58
+ end
59
+
60
+ def non_block_parameters
61
+ parameters - [block_parameter]
62
+ end
63
+ end
@@ -0,0 +1,27 @@
1
+ class MethodObject::Base
2
+ class << self
3
+ private :new
4
+
5
+ def call(options = {}, &block)
6
+ new(
7
+ options,
8
+ block,
9
+ @generator)
10
+ .call(*arguments_from_options(options), &block)
11
+ end
12
+
13
+ private
14
+
15
+ def arguments_from_options(options)
16
+ options.values_at(*@generator.non_block_parameter_names)
17
+ end
18
+ end
19
+
20
+ def initialize(options, block, generator)
21
+ options.each do |parameter_name, value|
22
+ instance_variable_set("@#{parameter_name}", value)
23
+ end
24
+ return unless generator.block_parameter
25
+ instance_variable_set("@#{generator.block_name}", block)
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ class MethodObject
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'method_object/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'method_man'
8
+ spec.version = MethodObject::VERSION
9
+ spec.authors = ['Clay Shentrup']
10
+ spec.email = %w(cshentrup@gmail.com)
11
+ spec.summary = %q{Provides a MethodObject class which implements Kent Beck's "method object" pattern.}
12
+ spec.description = %q{Provides a MethodObject class which implements Kent Beck's "method object" pattern.}
13
+ spec.homepage = 'https://github.com/brokenladder/method_man'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(lib)
20
+
21
+ spec.required_ruby_version = '>= 1.9'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ end
@@ -0,0 +1,62 @@
1
+ require 'method_object'
2
+
3
+ describe MethodObject do
4
+ subject do
5
+ described_class.new do
6
+ def call(
7
+ required_arg_1,
8
+ required_arg_2,
9
+ optional_arg_1 = :optional_arg_1,
10
+ optional_arg_2 = :optional_arg_2,
11
+ &my_block)
12
+ result << optional_arg_2 << yield(1)
13
+ end
14
+
15
+ def result
16
+ [
17
+ required_arg_1,
18
+ required_arg_2,
19
+ optional_arg_1,
20
+ my_block.call(2),
21
+ ]
22
+ end
23
+
24
+ def required_arg_1
25
+ :required_arg_1
26
+ end
27
+ end
28
+ end
29
+
30
+ it 'works' do
31
+ result = subject.call(
32
+ required_arg_1: :required_arg_1,
33
+ required_arg_2: :required_arg_2,
34
+ optional_arg_1: :optional_arg_1,
35
+ optional_arg_2: :optional_arg_2,
36
+ ) do |block_arg|
37
+ block_arg + 1
38
+ end
39
+
40
+ expect(result).to eq [
41
+ :required_arg_1,
42
+ :required_arg_2,
43
+ :optional_arg_1,
44
+ 3,
45
+ :optional_arg_2,
46
+ 2,
47
+ ]
48
+ end
49
+
50
+ context 'without arguments' do
51
+ subject do
52
+ described_class.new do
53
+ def call
54
+ end
55
+ end
56
+ end
57
+
58
+ it 'works' do
59
+ subject.call
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_man
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Clay Shentrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
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
+ description: Provides a MethodObject class which implements Kent Beck's "method object"
63
+ pattern.
64
+ email:
65
+ - cshentrup@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - .ruby-version
73
+ - Gemfile
74
+ - LICENSE
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - lib/method_object.rb
79
+ - lib/method_object/base.rb
80
+ - lib/method_object/version.rb
81
+ - method_man.gemspec
82
+ - spec/method_man_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: https://github.com/brokenladder/method_man
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '1.9'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.23
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Provides a MethodObject class which implements Kent Beck's "method object"
109
+ pattern.
110
+ test_files:
111
+ - spec/method_man_spec.rb
112
+ - spec/spec_helper.rb