Implement support for declaring promise/future methods natively

Fixes #7
This commit is contained in:
R. Tyler Croy 2014-10-25 16:43:11 -07:00
parent 799bdc2b3e
commit 78b513ea3d
5 changed files with 60 additions and 1 deletions

View File

@ -6,6 +6,7 @@ gemspec
group :development do
gem 'rake'
gem 'rspec'
gem 'rspec-its'
gem 'pry'
gem 'debugger-pry', :platform => :mri_19
end

View File

@ -1,3 +1,5 @@
require 'concurrent'
require 'typedeaf/arguments'
require 'typedeaf/errors'
@ -7,6 +9,18 @@ module Typedeaf
return Typedeaf::Arguments::DefaultArgument.new(value, *types)
end
def promise(method_sym, params={}, &block)
define(method_sym, params) do
Concurrent::Promise.new { block.call }.execute
end
end
def future(method_sym, params={}, &block)
define(method_sym, params) do
Concurrent::Future.new { block.call }.execute
end
end
def define(method_sym, params={}, &block)
if block.nil?
raise MissingMethodException,

View File

@ -1,6 +1,9 @@
require 'rubygems'
require 'typedeaf'
require 'rspec'
require 'rspec/its'
unless RUBY_PLATFORM == 'java'
require 'debugger/pry'
end

View File

@ -25,7 +25,6 @@ describe Typedeaf do
end
end
context 'defining a method with no arguments' do
before :each do
klass.class_eval do
@ -142,5 +141,45 @@ describe Typedeaf do
expect(called).to be_truthy
end
end
context 'defining a future method' do
before :each do
klass.class_eval do
future :log, message: String do
'hello'
end
end
end
it { should respond_to :log }
context 'the method result' do
let(:msg) { 'hello' }
subject(:result) { instance.log(msg) }
it { should be_kind_of Concurrent::Future }
its(:value) { should eql(msg) }
end
end
context 'defining a promise method' do
before :each do
klass.class_eval do
promise :log, message: String do
'hello'
end
end
end
it { should respond_to :log }
context 'the method result' do
let(:msg) { 'hello' }
subject(:result) { instance.log(msg) }
it { should be_kind_of Concurrent::Promise }
its(:value) { should eql(msg) }
end
end
end
end

View File

@ -16,4 +16,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_dependency 'concurrent-ruby', '~> 0.7.0'
end