Skip to content

Commit c901dc3

Browse files
committed
Implement respond_to? in module Monad
Resolves #2, as previous had no tests. See details in #2 PR, breifly: Prompted by @tomstuart statement: "If this was production code, we should remember to implement #respond_to? as well."
1 parent b0c69fd commit c901dc3

6 files changed

Lines changed: 28 additions & 12 deletions

File tree

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ rvm:
55
- 2.0.0
66
- 2.1.2
77
script: bundle exec rspec
8+
before_install:
9+
- gem update --system 2.4.8
10+
- gem install bundler -v 1.7.14

lib/monads/monad.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def method_missing(*args, &block)
1212
end
1313
end
1414

15+
def respond_to_missing?(method_name, include_private=false)
16+
super || within do |value|
17+
value.respond_to?(method_name, include_private)
18+
end
19+
end
20+
1521
private
1622

1723
def ensure_monadic_result(&block)

monads.gemspec

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ lib = File.expand_path('../lib', __FILE__)
22
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
33
require 'monads/version'
44

5-
Gem::Specification.new do |spec|
6-
spec.name = 'monads'
7-
spec.version = Monads::VERSION
8-
spec.author = 'Tom Stuart'
9-
spec.email = 'tom@codon.com'
10-
spec.summary = 'Simple Ruby implementations of some common monads.'
11-
spec.homepage = 'https://github.com/tomstuart/monads'
12-
spec.license = 'MIT'
5+
Gem::Specification.new do |gem|
6+
gem.name = 'monads'
7+
gem.version = Monads::VERSION
8+
gem.author = 'Tom Stuart'
9+
gem.email = 'tom@codon.com'
10+
gem.summary = 'Simple Ruby implementations of some common monads.'
11+
gem.homepage = 'https://github.com/tomstuart/monads'
12+
gem.license = 'MIT'
1313

14-
spec.files = `git ls-files -z`.split("\x0")
15-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16-
spec.require_paths = ['lib']
14+
gem.files = `git ls-files -z`.split("\x0")
15+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16+
gem.require_paths = ['lib']
1717

18-
spec.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
18+
gem.add_runtime_dependency 'bundler', '~> 1.7'
19+
gem.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
1920
end

spec/monads/eventually_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ module Monads
102102
it 'forwards any unrecognised message to the block’s value' do
103103
expect(value).to receive(:challenge)
104104
Eventually.new { |success| success.call(value) }.challenge.run {}
105+
expect { Eventually.new { |success| success.call(value) }.method(:challenge) }.not_to raise_error
106+
expect(Eventually.new { |success| success.call(value) }.respond_to?(:challenge)).to be_truthy
105107
end
106108

107109
it 'returns the message’s result wrapped in an Eventually' do

spec/monads/many_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ module Monads
8787
expect(value).to receive(:challenge)
8888
end
8989
many.challenge
90+
expect { many.method(:challenge) }.not_to raise_error
91+
expect(many.respond_to?(:challenge)).to be_truthy
9092
end
9193

9294
it 'returns the messages’ results wrapped in a Many' do

spec/monads/optional_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ module Monads
8989
it 'forwards any unrecognised message to the value' do
9090
expect(value).to receive(:challenge)
9191
optional.challenge
92+
expect { optional.method(:challenge) }.not_to raise_error
93+
expect(optional.respond_to?(:challenge)).to be_truthy
9294
end
9395

9496
it 'returns the message’s result wrapped in an Optional' do

0 commit comments

Comments
 (0)