diff --git a/Gemfile b/Gemfile index b888538..1e07f23 100644 --- a/Gemfile +++ b/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 diff --git a/lib/typedeaf/classmethods.rb b/lib/typedeaf/classmethods.rb index 16a01d2..d1236c0 100644 --- a/lib/typedeaf/classmethods.rb +++ b/lib/typedeaf/classmethods.rb @@ -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, diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e0ad9f0..0d9e798 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,9 @@ require 'rubygems' require 'typedeaf' +require 'rspec' +require 'rspec/its' + unless RUBY_PLATFORM == 'java' require 'debugger/pry' end diff --git a/spec/typedeaf_spec.rb b/spec/typedeaf_spec.rb index a6408dd..2cb729b 100644 --- a/spec/typedeaf_spec.rb +++ b/spec/typedeaf_spec.rb @@ -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 diff --git a/typedeaf.gemspec b/typedeaf.gemspec index a390ce5..0c99893 100644 --- a/typedeaf.gemspec +++ b/typedeaf.gemspec @@ -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