robject 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a8d4b9c98a92abfdae610517e7d9ebc3bbc3f2a
4
+ data.tar.gz: c38d7155f4cd47ee00a6253e70b2ee50b4df7388
5
+ SHA512:
6
+ metadata.gz: 149e35c8a0836faba7799024b188c15f624853b05481c91148280ed5d3c16e1954077ca7a8ebe25de91c77a571c3110b176c9d54a7e746e0587f361dc9cdf056
7
+ data.tar.gz: bcc08bcad3817594d8bb0194b3d8036de1cd67a2b99c679f590acd783de7bb041a9020f50c69531d53f9e3db4ed8ae1f865e1cd9cdfbc956c4b472ddc48e48a2
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in robject.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tatsuya Takamura
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.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # RObject
2
+
3
+ RObject is a wrapper library for RSRuby.
4
+
5
+ RSRuby provide to access R functions and variables from Ruby code. And, RObject wrap RSRuby's class to appropriate RObject class.
6
+
7
+ ## Convert R class to RObject
8
+
9
+ * `numeric` => `RObject::Numeric`
10
+ * `vector` => `RObject::Vector`
11
+ * `matrix` => `RObject::Matrix`
12
+ * `list` => `RObject::List`
13
+ * `array` => `RObject::Array`
14
+ * `data.frame` => `RObject::DataFrame`
15
+ * `character` => `RObject::String`
16
+ * other => `RObject::Base`
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'robject'
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install robject
31
+
32
+ ## Usage
33
+
34
+ Execute `robj_console` to open R-console
35
+
36
+ ```sh
37
+ robj_console
38
+
39
+ > m = matrix (1..9).to_a, ncol: 3
40
+ => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
41
+
42
+ > print m * m
43
+ [,1] [,2] [,3]
44
+ [1,] 30 66 102
45
+ [2,] 36 81 126
46
+ [3,] 42 96 150
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/robj_console ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../lib/robject', __FILE__)
3
+
4
+ begin
5
+ require 'pry'
6
+ rescue LoadError => e
7
+ require 'irb'
8
+ end
9
+
10
+ if defined? Pry
11
+ RObject::R.run do
12
+ Pry.start binding, print: ->(output, value){ output.puts "=> #{value.inspect}" }
13
+ end
14
+ else
15
+ RObject::R.run do
16
+ IRB.start
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ module RObject
3
+ class Base < DelegateClass(RObj)
4
+ attr_reader :robj
5
+
6
+ def self.delegate_to_R from_to
7
+ from_to.each do |from, to|
8
+ define_method(from) do |*args|
9
+ R[to].call @robj, *args
10
+ end
11
+ end
12
+ end
13
+
14
+ def self.match? robj, type
15
+ true
16
+ end
17
+
18
+ delegate_to_R '=~' => '==',
19
+ '**' => '%*%',
20
+ 'is_data_frame?' => 'is_data_frame',
21
+ 'is_list?' => 'is_list'
22
+
23
+ def initialize robj
24
+ @robj = robj
25
+ super robj
26
+ end
27
+
28
+ def method_missing name, *args, &block
29
+ if name =~ /^[\-+*\/!><&|\^]+$/
30
+ R[name.to_s].call @robj, *args
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def __getobj__
37
+ to_ruby
38
+ end
39
+
40
+ def as_r
41
+ @robj.as_r
42
+ end
43
+
44
+ def to_ruby mode = ::RSRuby::BASIC_CONVERSION
45
+ @robj.to_ruby mode
46
+ end
47
+
48
+ def is_function?
49
+ R.rsruby.is_function(@robj).to_ruby
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,28 @@
1
+ module RObject
2
+ class Convert
3
+ class NotSupportTypeError < Exception; end
4
+
5
+ def self.call_R_without_convert func, *args
6
+ original_mode = RSRuby.get_default_mode
7
+ RSRuby.set_default_mode RSRuby::BASIC_CONVERSION
8
+ result = R.rsruby[func].call(*args)
9
+ ensure
10
+ RSRuby.set_default_mode original_mode
11
+ result
12
+ end
13
+
14
+ def convert robj
15
+ type = check_R_type robj
16
+ [DataFrame, Function, List, Array, Matrix, String, Vector, Integer, Numeric, Base].each do |klass|
17
+ if klass.match? robj, type
18
+ break klass.new robj
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+ def check_R_type robj
25
+ Convert.call_R_without_convert :class, robj
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ Fixnum.class_eval do
2
+ prepend RObject::Helper::NumericDelegateR
3
+ end
4
+
5
+ Bignum.class_eval do
6
+ prepend RObject::Helper::NumericDelegateR
7
+ end
8
+
9
+ Float.class_eval do
10
+ prepend RObject::Helper::NumericDelegateR
11
+ end
12
+
13
+ Array.class_eval do
14
+ prepend RObject::Helper::NumericDelegateR
15
+ include RObject::Helper::MatrixMultiply
16
+ end
@@ -0,0 +1,53 @@
1
+ module RObject
2
+ module Helper
3
+ module MatrixMultiply
4
+ def * val
5
+ if val.respond_to?(:is_robj_matrix_multiply?) && val.is_robj_matrix_multiply?
6
+ R['%*%'].call self, val
7
+ else
8
+ super
9
+ end
10
+ end
11
+
12
+ def is_robj_matrix_multiply?
13
+ true
14
+ end
15
+ end
16
+
17
+ module NumericDelegateR
18
+ def + val
19
+ is_robj_matrix?(val) ? R[:+].call(self, val) : super
20
+ end
21
+
22
+ def - val
23
+ is_robj_matrix?(val) ? R[:-].call(self, val) : super
24
+ end
25
+
26
+ def / val
27
+ is_robj_matrix?(val) ? R[:/].call(self, val) : super
28
+ end
29
+
30
+ def * val
31
+ if is_robj_matrix?(val)
32
+ if val.respond_to?(:is_robj_matrix_multiply?) && val.is_robj_matrix_multiply? &&
33
+ self.respond_to?(:is_robj_matrix_multiply?) && self.is_robj_matrix_multiply?
34
+ R['%*%'].call(self, val)
35
+ else
36
+ R[:*].call(self, val)
37
+ end
38
+ else
39
+ super
40
+ end
41
+ end
42
+
43
+ def ** val
44
+ is_robj_matrix?(val) ? R[:**].call(self, val) : super
45
+ end
46
+
47
+ private
48
+ def is_robj_matrix? val
49
+ val.is_a?(RObject::Vector) || val.is_a?(RObject::Matrix)
50
+ end
51
+ end
52
+ end
53
+ end
data/lib/robject/r.rb ADDED
@@ -0,0 +1,50 @@
1
+ module RObject
2
+ module R
3
+ class << self
4
+ def method_missing name, *args, &block
5
+ if name =~ /^(.*)=$/
6
+ rsruby[:assign].call $1.to_s, args.first
7
+ elsif rsruby.respond_to? name
8
+ rsruby.send name, *args
9
+ else
10
+ obj = rsruby[name]
11
+ if obj.is_function?
12
+ obj.call *args
13
+ else
14
+ obj
15
+ end
16
+ end
17
+ end
18
+
19
+ def run &block
20
+ instance_eval &block
21
+ end
22
+
23
+ def print *args
24
+ rsruby[:print].call *args
25
+ end
26
+
27
+ def lazy_expression r_exp
28
+ rsruby.eval_R "quote(#{r_exp})"
29
+ end
30
+ alias_method "`", :lazy_expression
31
+
32
+ def rsruby
33
+ @rsruby ||= init_rsruby
34
+ end
35
+
36
+ def init_rsruby
37
+ RSRuby.set_default_mode RSRuby::PROC_CONVERSION
38
+ converter = RObject::Convert.new
39
+ @rsruby = RSRuby.instance
40
+ @rsruby.proc_table[->(x){ true }] = ->(x) { converter.convert x }
41
+ @rsruby
42
+ end
43
+
44
+ private
45
+ def reset
46
+ @rsruby = nil
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class Array < Base
3
+ def self.match? robj, type
4
+ type == 'array'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class DataFrame < Base
3
+ def self.match? robj, type
4
+ type == 'data.frame'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class Function < Base
3
+ def self.match? robj, type
4
+ type == 'function'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class Integer < Numeric
3
+ def self.match? robj, type
4
+ type == 'integer'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class List < Base
3
+ def self.match? robj, type
4
+ type == 'list'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module RObject
2
+ class Matrix < Base
3
+ include Helper::MatrixMultiply
4
+
5
+ def self.match? robj, type
6
+ type == 'matrix'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class Numeric < Base
3
+ def self.match? robj, type
4
+ type == 'numeric'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module RObject
2
+ class String < Base
3
+ def self.match? robj, type
4
+ type == 'character'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module RObject
2
+ class Vector < Base
3
+ include Helper::MatrixMultiply
4
+
5
+ def self.match? robj, type
6
+ (type == 'numeric' || type == 'integer') &&
7
+ Convert.call_R_without_convert(:length, robj) > 1
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module RObject
2
+ VERSION = "0.0.2"
3
+ end
data/lib/robject.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rsruby'
2
+ require 'delegate'
3
+ require File.expand_path('../robject/version', __FILE__)
4
+ require File.expand_path('../robject/helper', __FILE__)
5
+ require File.expand_path('../robject/r', __FILE__)
6
+ require File.expand_path('../robject/base', __FILE__)
7
+ require File.expand_path('../robject/convert', __FILE__)
8
+ require File.expand_path('../robject/type/matrix', __FILE__)
9
+ require File.expand_path('../robject/type/vector', __FILE__)
10
+ require File.expand_path('../robject/type/numeric', __FILE__)
11
+ require File.expand_path('../robject/type/integer', __FILE__)
12
+ require File.expand_path('../robject/type/string', __FILE__)
13
+ require File.expand_path('../robject/type/list', __FILE__)
14
+ require File.expand_path('../robject/type/array', __FILE__)
15
+ require File.expand_path('../robject/type/data_frame', __FILE__)
16
+ require File.expand_path('../robject/type/function', __FILE__)
17
+ require File.expand_path('../robject/core_ext', __FILE__)
18
+
19
+ module RObject
20
+ end
data/robject.gemspec ADDED
@@ -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 'robject/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "robject"
8
+ spec.version = RObject::VERSION
9
+ spec.authors = ["Tatsuya Takamura"]
10
+ spec.email = ["tkmr2000@gmail.com"]
11
+ spec.description = %q{RObject is a wrapper library for RSRuby.}
12
+ spec.summary = %q{RObject is a wrapper library for RSRuby.}
13
+ spec.homepage = "https://github.com/ttakamura"
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 = ["lib"]
20
+
21
+ spec.add_dependency "rsruby", ">= 0.5"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 2.13.0"
25
+ spec.add_development_dependency "rspec-mocks", "~> 2.13.0"
26
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe RObject::Base do
4
+ let!(:rsruby) { rsruby_stub }
5
+
6
+ def robject original_value
7
+ rsruby_obj = double("rsruby::robj (#{original_value})")
8
+ rsruby_obj.stub(:to_ruby).and_return original_value
9
+ RObject::Base.new rsruby_obj
10
+ end
11
+
12
+ context 'robject([1, 2, 3])' do
13
+ subject { robject([1,2,3]) }
14
+
15
+ it { should be_include 3 }
16
+ it { should == [1,2,3] }
17
+
18
+ its(:to_ruby) { should == [1,2,3] }
19
+ its(:inspect) { should == '[1, 2, 3]' }
20
+ end
21
+
22
+ describe 'auto conversion' do
23
+ describe 'R-object receive a method' do
24
+ subject { robject([5,6]) }
25
+ it { should == [5,6] }
26
+ it { should be_include 5 }
27
+ end
28
+
29
+ describe 'Ruby-object receive a method with R-object' do
30
+ subject { [5,6].include? robject(5) }
31
+ it { should be_true }
32
+ end
33
+ end
34
+
35
+ describe '#method_missing' do
36
+ shared_examples_for 'call a R-function' do |name, right_val, response|
37
+ before do
38
+ func = double("R-function")
39
+ func.should_receive(:call).with(obj.robj, right_val).and_return response
40
+ rsruby.should_receive(:[]).with(name).and_return func
41
+ end
42
+ it { should == response }
43
+ end
44
+
45
+ context 'robject([1,2,3])' do
46
+ let(:obj) { robject([1,2,3]) }
47
+
48
+ describe '+ 100' do
49
+ subject { obj + 100 }
50
+ it_behaves_like 'call a R-function', '+', 100, [101, 102, 103]
51
+ end
52
+
53
+ describe '- 100' do
54
+ subject { obj - 100 }
55
+ it_behaves_like 'call a R-function', '-', 100, [-99, -98, -97]
56
+ end
57
+
58
+ describe '* 100' do
59
+ subject { obj * 100 }
60
+ it_behaves_like 'call a R-function', '*', 100, [100, 200, 300]
61
+ end
62
+ end
63
+
64
+ context 'call a Ruby-method' do
65
+ subject { robject(999) }
66
+ its(:to_s) { should == '999' }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe RObject::Convert do
4
+ let!(:rsruby) { rsruby_stub }
5
+ let(:converter) { RObject::Convert.new }
6
+
7
+ def robj original_value
8
+ rsruby_obj = double("rsruby::robj (#{original_value})")
9
+ rsruby_obj.stub(:to_ruby).and_return original_value
10
+ rsruby_obj
11
+ end
12
+
13
+ describe '#convert' do
14
+ let(:length_val) { 0 }
15
+
16
+ shared_examples_for 'convert RObj to' do |klass|
17
+ before do
18
+ rsruby.stub(:[]).with(:length).and_return double(call: length_val)
19
+ rsruby.stub(:[]).with(:class).and_return double(call: r_class)
20
+ end
21
+ subject { converter.convert robj(value) }
22
+ it { should be_a klass }
23
+ it { should == value }
24
+ end
25
+
26
+ context 'DataFrame' do
27
+ let(:value) { { x: [1,2,3] } }
28
+ let(:r_class) { 'data.frame' }
29
+ it_behaves_like 'convert RObj to', RObject::DataFrame
30
+ end
31
+
32
+ context 'Function' do
33
+ let(:value) { 'R-function' }
34
+ let(:r_class) { 'function' }
35
+ it_behaves_like 'convert RObj to', RObject::Function
36
+ end
37
+
38
+ context 'List' do
39
+ let(:value) { [1, 2, 3] }
40
+ let(:r_class) { 'list' }
41
+ it_behaves_like 'convert RObj to', RObject::List
42
+ end
43
+
44
+ context 'Array' do
45
+ let(:value) { [1, 2, 3] }
46
+ let(:r_class) { 'array' }
47
+ it_behaves_like 'convert RObj to', RObject::Array
48
+ end
49
+
50
+ context 'Matrix' do
51
+ let(:value) { [[1, 2], [3, 4]] }
52
+ let(:r_class) { 'matrix' }
53
+ it_behaves_like 'convert RObj to', RObject::Matrix
54
+ end
55
+
56
+ context 'Vector' do
57
+ let(:length_val) { 2 }
58
+ let(:value) { [1, 2, 3] }
59
+ let(:r_class) { 'numeric' }
60
+ it_behaves_like 'convert RObj to', RObject::Vector
61
+ end
62
+
63
+ context 'String' do
64
+ let(:value) { "hello world" }
65
+ let(:r_class) { 'character' }
66
+ it_behaves_like 'convert RObj to', RObject::String
67
+ end
68
+
69
+ context 'Numeric' do
70
+ let(:value) { 99.0 }
71
+ let(:r_class) { 'numeric' }
72
+ it_behaves_like 'convert RObj to', RObject::Numeric
73
+ end
74
+
75
+ context 'Integer' do
76
+ let(:value) { 31 }
77
+ let(:r_class) { 'integer' }
78
+ it_behaves_like 'convert RObj to', RObject::Integer
79
+ end
80
+
81
+ context 'Base' do
82
+ let(:value) { 100 }
83
+ let(:r_class) { 'unknown' }
84
+ it_behaves_like 'convert RObj to', RObject::Base
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe RObject::Matrix do
4
+ def robj original_value
5
+ rsruby_obj = double("rsruby::robj (#{original_value})")
6
+ rsruby_obj.stub(:to_ruby).and_return original_value
7
+ rsruby_obj
8
+ end
9
+
10
+ let!(:rsruby) { rsruby_stub }
11
+ let(:matrix) { RObject::Matrix.new robj([[1, 2], [3, 4]]) }
12
+ let(:vector) { RObject::Vector.new robj([10, 100]) }
13
+
14
+ describe 'multiplication' do
15
+ let(:result) { [[7, 10], [15, 22]] }
16
+
17
+ context 'robj * robj-matrix' do
18
+ before { rsruby_func_stub rsruby, '%*%', [matrix, matrix], result }
19
+ subject { matrix * matrix }
20
+ it { should == result }
21
+ end
22
+
23
+ context 'robj * robj-vector' do
24
+ let(:result) { [310, 420] }
25
+ before { rsruby_func_stub rsruby, '%*%', [matrix, vector], result }
26
+ subject { matrix * vector }
27
+ it { should == result }
28
+ end
29
+
30
+ context 'robj * ruby-array' do
31
+ let(:result) { [120, 340] }
32
+ before { rsruby_func_stub rsruby, '%*%', [matrix, [100, 10]], result }
33
+ subject { matrix * [100, 10] }
34
+ it { should == result }
35
+ end
36
+
37
+ context 'ruby-array * robj' do
38
+ let(:result) { [120, 340] }
39
+ before { rsruby_func_stub rsruby, '%*%', [[100, 10], matrix], result }
40
+ subject { [100, 10] * matrix }
41
+ it { should == result }
42
+ end
43
+
44
+ context 'robj * ruby-number' do
45
+ let(:result) { [[10, 20], [30, 40]] }
46
+ before { rsruby_func_stub rsruby, '*', [matrix.robj, 10], result }
47
+ subject { matrix * 10 }
48
+ it { should == result }
49
+ end
50
+
51
+ context 'ruby-number * robj' do
52
+ let(:result) { [[10, 20], [30, 40]] }
53
+ before { rsruby_func_stub rsruby, :*, [10, matrix], result }
54
+ subject { 10 * matrix }
55
+ it { should == result }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe RObject::R do
4
+ let!(:rsruby) { rsruby_stub }
5
+
6
+ describe '#lazy_expression' do
7
+ before do
8
+ rsruby.should_receive(:eval_R).with('quote(a + b)') { RObject::Base.new nil }
9
+ end
10
+
11
+ subject { @expression = RObject::R.lazy_expression('a + b') }
12
+ it { should be_a RObject::Base }
13
+ end
14
+
15
+ describe '#method_missing' do
16
+ context 'call a RSRuby method' do
17
+ before { rsruby.should_receive(:eval_R).with('11 + 44').and_return 55 }
18
+ subject { RObject::R.eval_R '11 + 44' }
19
+ it { should == 55 }
20
+ end
21
+
22
+ context 'call a R-function' do
23
+ before { rsruby_func_stub rsruby, :sum, [11, 22], 33 }
24
+ subject { RObject::R.sum 11, 22 }
25
+ it { should == 33 }
26
+ end
27
+
28
+ context 'assign a value to R-variable' do
29
+ before do
30
+ rsruby_variable_stub rsruby, :x, 100
31
+ RObject::R.x = 100
32
+ end
33
+ subject { RObject::R.x }
34
+ it { should == 100 }
35
+ end
36
+
37
+ context 'get a R-variable' do
38
+ before { rsruby_variable_stub rsruby, :y, 900 }
39
+ subject { RObject::R.y }
40
+ it { should == 900 }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../../lib/robject', __FILE__)
2
+
3
+ module RObject::SpecDSL
4
+ def rsruby_stub
5
+ rsruby = double("rsruby")
6
+ RObject::R.stub(:rsruby).and_return(rsruby)
7
+ proc_table = {}
8
+ rsruby.stub(:default_mode=).with(4)
9
+ rsruby.stub(:proc_table) { proc_table }
10
+ rsruby
11
+ end
12
+
13
+ def rsruby_func_stub rsruby, name, args, response
14
+ func = double(name.to_s)
15
+ func.stub(:call).with(*args).and_return response
16
+ func.stub(:is_function?).and_return true
17
+ rsruby.stub(:[]).with(name).and_return func
18
+ func
19
+ end
20
+
21
+ def rsruby_variable_stub rsruby, name, value
22
+ variable = RObject::Base.new double(name.to_s)
23
+ variable.stub(:to_ruby).and_return value
24
+ variable.stub(:is_function?).and_return false
25
+ rsruby_func_stub rsruby, :assign, [name.to_s, value], variable
26
+ rsruby.stub(:[]).with(name).and_return variable
27
+ variable
28
+ end
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ config.mock_framework = :rspec
33
+ config.include RObject::SpecDSL
34
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robject
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Tatsuya Takamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rsruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.13.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.13.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-mocks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.13.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.13.0
83
+ description: RObject is a wrapper library for RSRuby.
84
+ email:
85
+ - tkmr2000@gmail.com
86
+ executables:
87
+ - robj_console
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/robj_console
97
+ - lib/robject.rb
98
+ - lib/robject/base.rb
99
+ - lib/robject/convert.rb
100
+ - lib/robject/core_ext.rb
101
+ - lib/robject/helper.rb
102
+ - lib/robject/r.rb
103
+ - lib/robject/type/array.rb
104
+ - lib/robject/type/data_frame.rb
105
+ - lib/robject/type/function.rb
106
+ - lib/robject/type/integer.rb
107
+ - lib/robject/type/list.rb
108
+ - lib/robject/type/matrix.rb
109
+ - lib/robject/type/numeric.rb
110
+ - lib/robject/type/string.rb
111
+ - lib/robject/type/vector.rb
112
+ - lib/robject/version.rb
113
+ - robject.gemspec
114
+ - spec/base_spec.rb
115
+ - spec/convert_spec.rb
116
+ - spec/matrix_spec.rb
117
+ - spec/robject_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: https://github.com/ttakamura
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: RObject is a wrapper library for RSRuby.
143
+ test_files:
144
+ - spec/base_spec.rb
145
+ - spec/convert_spec.rb
146
+ - spec/matrix_spec.rb
147
+ - spec/robject_spec.rb
148
+ - spec/spec_helper.rb
149
+ has_rdoc: