scaruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Scaruby
4
+
5
+ class Future
6
+
7
+ attr_accessor :result, :mutex
8
+
9
+ def initialize(mutex)
10
+ assert_type(mutex, Mutex)
11
+ @mutex = mutex
12
+ @result = nil
13
+ end
14
+
15
+ def get
16
+ @mutex.synchronize {
17
+ @result
18
+ }
19
+ end
20
+
21
+ end
22
+
23
+ module ConcurrentOps
24
+
25
+ def spawn(&block)
26
+ Thread.new do
27
+ yield
28
+ end
29
+ end
30
+
31
+ def future(&block)
32
+ f = Future.new(Mutex.new)
33
+ Thread.new do
34
+ f.mutex.synchronize do
35
+ f.result = yield
36
+ end
37
+ end
38
+ f
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ include Scaruby::ConcurrentOps
46
+
@@ -1,6 +1,8 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module Scaruby
4
+ class AssertionError < Exception
5
+ end
4
6
  class NoSuchElementException < Exception
5
7
  end
6
8
  end
data/lib/scaruby/io.rb ADDED
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module Scaruby
7
+ module IO
8
+ class Source
9
+
10
+ attr :string_io
11
+
12
+ def initialize(string_io)
13
+ assert_type(string_io, StringIO)
14
+ @string_io = string_io
15
+ end
16
+
17
+ def self.from_bytes(bytes, encoding='UTF-8')
18
+ content = bytes.to_a.pack('c*').force_encoding(encoding)
19
+ Source.new(StringIO.new(content))
20
+ end
21
+
22
+ def self.from_file(file, encoding='UTF-8')
23
+ content = ''
24
+ File::open(file, "r:#{encoding}") do |file|
25
+ while line = file.gets
26
+ content += line
27
+ end
28
+ end
29
+ Source.new(StringIO.new(content))
30
+ end
31
+
32
+ def self.from_url(url, encoding='UTF-8')
33
+ uri = URI.parse(url)
34
+ http = Net::HTTP.new(uri.host, uri.port)
35
+ http.use_ssl = uri.is_a?(URI::HTTPS)
36
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
37
+
38
+ http.start {|http|
39
+ req = Net::HTTP::Get.new(uri.request_uri)
40
+ res = http.request(req)
41
+ Source.new(StringIO.new(res.body))
42
+ }
43
+ end
44
+
45
+ def get_lines
46
+ Seq.new(@string_io.map {|line| line })
47
+ end
48
+
49
+ def to_seq
50
+ chars = []
51
+ while @string_io.eof? do
52
+ chars.push(@string_io.getc)
53
+ end
54
+ Seq.new(chars)
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+
61
+ include Scaruby::IO
62
+
data/lib/scaruby/map.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  module Scaruby
4
4
  class Map < Hash
5
5
 
6
+ attr :hash
7
+
6
8
  def each(&block)
7
9
  @hash.each do |k,v|
8
10
  yield k, v
@@ -15,6 +17,7 @@ module Scaruby
15
17
  end
16
18
 
17
19
  def initialize(hash)
20
+ assert_type(hash, Hash)
18
21
  @hash = hash
19
22
  end
20
23
 
@@ -1,10 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require 'scaruby/no_such_element_exception'
4
-
5
3
  module Scaruby
6
4
  class Option
7
5
 
6
+ attr :value
7
+
8
8
  def self.apply(value)
9
9
  Option.new(value)
10
10
  end
data/lib/scaruby/seq.rb CHANGED
@@ -4,6 +4,8 @@ module Scaruby
4
4
  class Seq
5
5
  include Enumerable
6
6
 
7
+ attr :array
8
+
7
9
  def each(&block)
8
10
  @array.each do |e|
9
11
  yield e
@@ -16,11 +18,8 @@ module Scaruby
16
18
  end
17
19
 
18
20
  def initialize(enumerable)
19
- if enumerable.nil? || enumerable.is_a?(Enumerable) then
20
- @array = enumerable.to_a
21
- else
22
- raise ArgumentError, 'Invalid argument (' + enumerable.to_s + ')'
23
- end
21
+ assert_type(enumerable, Enumerable)
22
+ @array = enumerable.to_a
24
23
  end
25
24
 
26
25
  def to_a
@@ -170,7 +169,7 @@ module Scaruby
170
169
  end
171
170
 
172
171
  def indices
173
- Seq.new(0.upto @array.length-1)
172
+ Seq.new(0.upto(@array.length-1))
174
173
  end
175
174
 
176
175
  def init
@@ -236,7 +235,7 @@ module Scaruby
236
235
 
237
236
  def partition(&predicate)
238
237
  Seq.new(@array.chunk(&predicate).inject([[],[]]) {|z, matched_and_elm|
239
- matched, elm = matched_and_elm[0], matched_and_elm[1][0]
238
+ matched, elm = matched_and_elm[0], matched_and_elm[1].first
240
239
  if matched then
241
240
  [z[0].push(elm), z[1]]
242
241
  else
@@ -246,7 +245,6 @@ module Scaruby
246
245
  end
247
246
 
248
247
  def patch(from, patch, replaced)
249
- # -- compatible with Ruby 1.8.7
250
248
  #Seq.new(@array.take(from).concat(patch).concat(@array.drop(from).drop(replaced)))
251
249
  Seq.new(@array.take(from) + patch + @array.drop(from).drop(replaced))
252
250
  end
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module Scaruby
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
6
6
 
data/lib/scaruby.rb CHANGED
@@ -1,22 +1,58 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'scaruby/appliable_proc'
4
+ require 'scaruby/concurrent'
5
+ require 'scaruby/converter'
6
+ require 'scaruby/io'
4
7
  require 'scaruby/map'
5
- require 'scaruby/no_such_element_exception'
8
+ require 'scaruby/exception'
6
9
  require 'scaruby/option'
7
10
  require 'scaruby/seq'
8
11
  require 'scaruby/version'
9
12
 
10
13
  module Scaruby
11
- end
12
14
 
13
- # alias
14
- class AppliableProc < Scaruby::AppliableProc
15
- end
16
- class Map < Scaruby::Map
17
- end
18
- class Option < Scaruby::Option
19
- end
20
- class Seq < Scaruby::Seq
15
+ module Commons
16
+
17
+ def assert_type(v, *types)
18
+ is_matched = false
19
+ unless v.nil? then
20
+ found = types.inject(false) {|found,type|
21
+ if found then true
22
+ else v.is_a?(type)
23
+ end
24
+ }
25
+ unless found then
26
+ raise AssertionError,
27
+ "The type of `#{v}` should be whichever of [#{types.join(', ')}] but actually #{v.class}."
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ module Predef
35
+
36
+ def assert(assertion)
37
+ unless assertion then
38
+ raise AssertionError, 'Assertion failed'
39
+ end
40
+ end
41
+
42
+ def println(any)
43
+ puts any.to_s
44
+ end
45
+
46
+ def read_line
47
+ input = readline
48
+ input.sub(/\r?\n/, '')
49
+ end
50
+
51
+ end
21
52
  end
22
53
 
54
+ include Scaruby
55
+ include Scaruby::Commons
56
+ include Scaruby::Predef
57
+
58
+
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'scaruby'
4
+
5
+ describe Scaruby::ConcurrentOps do
6
+ it 'has #spawn' do
7
+ is_done = false
8
+ spawn do
9
+ sleep 0.3
10
+ is_done = true
11
+ end
12
+ is_done.should eq(false)
13
+ sleep 0.5
14
+ is_done.should eq(true)
15
+ end
16
+ it 'has #future' do
17
+ is_done = false
18
+ future = future {
19
+ sleep 0.3
20
+ is_done = true
21
+ sleep 0.5
22
+ :ok
23
+ }
24
+ is_done.should eq(false)
25
+ sleep 0.5
26
+ is_done.should eq(true)
27
+ future.get.should eq(:ok)
28
+ is_done.should eq(true)
29
+ end
30
+ end
31
+
@@ -0,0 +1,56 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'scaruby'
4
+
5
+ describe Scaruby::IO::Source do
6
+
7
+ http_url = 'http://seratch.github.com/'
8
+ https_url = 'https://github.com/seratch/scaruby'
9
+
10
+ it 'has from_file' do
11
+ file = 'spec/scaruby_spec.rb'
12
+ source = Source.from_file(file)
13
+ source.to_seq.foreach do |c|
14
+ line.is_a?(String).should eq(true)
15
+ end
16
+ lines = source.get_lines
17
+ lines.is_a?(Enumerable).should eq(true)
18
+ lines.foreach do |line|
19
+ line.is_a?(String).should eq(true)
20
+ end
21
+ end
22
+ it 'has from_bytes' do
23
+ str = 'zzz 日本語を含む文字列 999'
24
+ bytes = str.bytes.to_a
25
+ source = Source.from_bytes(bytes)
26
+ source.to_seq.foreach do |c|
27
+ c.is_a?(String).should eq(true)
28
+ end
29
+ source.get_lines.mk_string.should eq(str)
30
+ end
31
+ it 'has from_url with a http url' do
32
+ source = Source.from_url(http_url)
33
+ source.to_seq.foreach do |c|
34
+ c.is_a?(String).should eq(true)
35
+ end
36
+ end
37
+ it 'has from_url with a https url' do
38
+ source = Source.from_url(https_url)
39
+ source.to_seq.foreach do |c|
40
+ c.is_a?(String).should eq(true)
41
+ end
42
+ end
43
+ it 'has #get_lines' do
44
+ source = Source.from_url(http_url,'Shift_JIS')
45
+ source.get_lines.foreach do |line|
46
+ line.is_a?(String).should eq(true)
47
+ end
48
+ end
49
+ it 'has #to_seq' do
50
+ source = Source.from_url(http_url)
51
+ source.to_seq.foreach do |c|
52
+ c.is_a?(String).should eq(true)
53
+ end
54
+ end
55
+
56
+ end
@@ -6,6 +6,14 @@ describe Map do
6
6
 
7
7
  hash = {123 => 'abc', 234 => 'bcd', 345 => 'cde', 4 => 'd', 56 => 'ef', 7 => 'g', 89 => 'hi' }
8
8
 
9
+ it 'does not accept invalid args' do
10
+ begin
11
+ map = Map.new(1234)
12
+ raise 'Expected exception did not be raised!'
13
+ rescue AssertionError
14
+ end
15
+ end
16
+
9
17
  it 'has self.new' do
10
18
  map = Map.new({1 => 'a', 2 => 'b'})
11
19
  map.should_not eq(nil)
@@ -10,11 +10,13 @@ describe Seq do
10
10
  it 'does not accept invalid args' do
11
11
  begin
12
12
  Seq.new('aaaa').should eq(nil)
13
- rescue ArgumentError
13
+ raise 'Expected exception did not be raised!'
14
+ rescue AssertionError
14
15
  end
15
16
  begin
16
17
  Seq.new(12345).should eq(nil)
17
- rescue ArgumentError
18
+ raise 'Expected exception did not be raised!'
19
+ rescue AssertionError
18
20
  end
19
21
  end
20
22
 
data/spec/scaruby_spec.rb CHANGED
@@ -1,7 +1,32 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
+ require 'scaruby'
4
+
3
5
  describe Scaruby do
4
- it 'is available' do
6
+ it 'has assert_type' do
7
+ assert_type(123, Fixnum)
8
+ assert_type(123, String, Fixnum, Hash)
9
+ begin
10
+ assert_type(123, Hash, Array)
11
+ raise 'Expected exception did not be raised!'
12
+ rescue AssertionError => e
13
+ e.message.should eq('The type of `123` should be whichever of [Hash, Array] but actually Fixnum.')
14
+ end
15
+ assert_type([1,2,3], Array)
16
+ assert_type({1=>'a'}, Hash)
17
+ assert_type(nil, Array)
18
+ assert_type(nil, Hash)
19
+ end
20
+ it 'has assert' do
21
+ assert('1' != 1)
22
+ begin
23
+ assert('1' == 1)
24
+ raise 'Expected exception did not be raised!'
25
+ rescue AssertionError => e
26
+ end
27
+ end
28
+ it 'has converter' do
29
+ [1,2,3].to_scaruby.should_not eq(nil)
5
30
  end
6
31
  end
7
32
 
metadata CHANGED
@@ -1,23 +1,28 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: scaruby
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.0.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Kazuhiro Sera
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-05 00:00:00.000000000 Z
12
+
13
+ date: 2012-03-06 00:00:00 Z
13
14
  dependencies: []
15
+
14
16
  description: Scala API in Ruby
15
- email:
17
+ email:
16
18
  - seratch@gmail.com
17
19
  executables: []
20
+
18
21
  extensions: []
22
+
19
23
  extra_rdoc_files: []
20
- files:
24
+
25
+ files:
21
26
  - .gitignore
22
27
  - .travis.yml
23
28
  - Gemfile
@@ -25,52 +30,61 @@ files:
25
30
  - Rakefile
26
31
  - lib/scaruby.rb
27
32
  - lib/scaruby/appliable_proc.rb
33
+ - lib/scaruby/concurrent.rb
28
34
  - lib/scaruby/converter.rb
29
35
  - lib/scaruby/core_ext.rb
30
36
  - lib/scaruby/core_ext/enumerable.rb
31
37
  - lib/scaruby/core_ext/hash.rb
38
+ - lib/scaruby/exception.rb
39
+ - lib/scaruby/io.rb
32
40
  - lib/scaruby/map.rb
33
- - lib/scaruby/no_such_element_exception.rb
34
41
  - lib/scaruby/option.rb
35
42
  - lib/scaruby/seq.rb
36
43
  - lib/scaruby/version.rb
37
44
  - readme.md
38
45
  - scaruby.gemspec
46
+ - spec/scaruby/concurrent_spec.rb
39
47
  - spec/scaruby/converter_spec.rb
40
48
  - spec/scaruby/core_ext/enumerable_spec.rb
41
49
  - spec/scaruby/core_ext/hash_spec.rb
50
+ - spec/scaruby/io_spec.rb
42
51
  - spec/scaruby/map_spec.rb
43
52
  - spec/scaruby/option_spec.rb
44
53
  - spec/scaruby/seq_spec.rb
45
54
  - spec/scaruby_spec.rb
46
55
  homepage: https://github.com/seratch/scaruby
47
56
  licenses: []
57
+
48
58
  post_install_message:
49
59
  rdoc_options: []
50
- require_paths:
60
+
61
+ require_paths:
51
62
  - lib
52
- required_ruby_version: !ruby/object:Gem::Requirement
63
+ required_ruby_version: !ruby/object:Gem::Requirement
53
64
  none: false
54
- requirements:
55
- - - ! '>='
56
- - !ruby/object:Gem::Version
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
57
68
  version: 1.9.2
58
- required_rubygems_version: !ruby/object:Gem::Requirement
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
70
  none: false
60
- requirements:
61
- - - ! '>='
62
- - !ruby/object:Gem::Version
63
- version: '0'
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
64
75
  requirements: []
76
+
65
77
  rubyforge_project: scaruby
66
- rubygems_version: 1.8.17
78
+ rubygems_version: 1.8.15
67
79
  signing_key:
68
80
  specification_version: 3
69
81
  summary: Scala API in Ruby
70
- test_files:
82
+ test_files:
83
+ - spec/scaruby/concurrent_spec.rb
71
84
  - spec/scaruby/converter_spec.rb
72
85
  - spec/scaruby/core_ext/enumerable_spec.rb
73
86
  - spec/scaruby/core_ext/hash_spec.rb
87
+ - spec/scaruby/io_spec.rb
74
88
  - spec/scaruby/map_spec.rb
75
89
  - spec/scaruby/option_spec.rb
76
90
  - spec/scaruby/seq_spec.rb