Implement support for declaring promise/future methods natively
Fixes #7
This commit is contained in:
parent
799bdc2b3e
commit
78b513ea3d
1
Gemfile
1
Gemfile
|
@ -6,6 +6,7 @@ gemspec
|
|||
group :development do
|
||||
gem 'rake'
|
||||
gem 'rspec'
|
||||
gem 'rspec-its'
|
||||
gem 'pry'
|
||||
gem 'debugger-pry', :platform => :mri_19
|
||||
end
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
require 'rubygems'
|
||||
require 'typedeaf'
|
||||
|
||||
require 'rspec'
|
||||
require 'rspec/its'
|
||||
|
||||
unless RUBY_PLATFORM == 'java'
|
||||
require 'debugger/pry'
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue