irxrb 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +20 -0
- data/.travis.yml +7 -0
- data/Gemfile +10 -0
- data/LICENSE +22 -0
- data/README.md +32 -0
- data/Rakefile +9 -0
- data/irxrb.gemspec +17 -0
- data/irxrb.sublime-project +8 -0
- data/lib/irxrb/core_ext/generic_type.rb +5 -0
- data/lib/irxrb/core_ext/hash_to_obj.rb +5 -0
- data/lib/irxrb/core_ext/match_data_to_hash.rb +5 -0
- data/lib/irxrb/core_ext/maybe.rb +13 -0
- data/lib/irxrb/core_ext/range_to_regex.rb +7 -0
- data/lib/irxrb/core_ext/to_proc.rb +5 -0
- data/lib/irxrb/generic_type.rb +25 -0
- data/lib/irxrb/hash_object.rb +21 -0
- data/lib/irxrb/lazy_attribute.rb +27 -0
- data/lib/irxrb/maybe.rb +52 -0
- data/lib/irxrb/range_to_regex.rb +68 -0
- data/lib/irxrb/validation_struct.rb +0 -0
- data/lib/irxrb/version.rb +3 -0
- data/lib/irxrb.rb +9 -0
- data/spec/core_ext/hash_to_obj_spec.rb +8 -0
- data/spec/core_ext/match_data_to_hash_spec.rb +12 -0
- data/spec/core_ext/maybe_spec.rb +17 -0
- data/spec/core_ext/range_to_regex_spec.rb +7 -0
- data/spec/core_ext/to_proc_spec.rb +64 -0
- data/spec/generic_type_spec.rb +46 -0
- data/spec/hash_object_spec.rb +24 -0
- data/spec/lazy_attribute_spec.rb +44 -0
- data/spec/maybe_spec.rb +66 -0
- data/spec/range_to_regex_spec.rb +79 -0
- data/spec/spec_helper.rb +6 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YzZlYTIzZjhjNmRhYTIwZGJmZDM0N2I4YzU0N2Y2YmVkOTY5OGU5Mw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MzJlOWIzNTRmY2E0M2E2MzJiZjVjOWZiODI3M2JkNGY1ZWNhMmRhOA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OTNkMWZjYWQ2MTcwYTZlNDI2ZTlmZWQzMjZhNWU1N2E5NjI5MWU0YTkzNWY4
|
10
|
+
OTU5MmZkMTdiYTAyZmRiNzQwOTA5YjY5ZmJkM2E3MjNjNGM2OGNhODkzNmRj
|
11
|
+
ZjYzOTY4MGE3ZGFmZWZiNDllZjI2N2RmYTU4Njk3NDUwMzJlYTI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjQzZWQ1YTM1M2ZkYTdkNTkxMmFlYWVjOTM3MzIyMjJiMjM3MTAzYmI5NjY4
|
14
|
+
YmJhODNjNjI0MmYxZjY3M2MwMWQ2YTY1Njk5YWRjMWMyZGI4NGZjMmJkMGM0
|
15
|
+
YjFmZjJiMjQ3YjY1NDMyMTk0Nzk3Zjk5OGU1NTk0MWMyYTQxYzc=
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 OTA Daichi
|
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,32 @@
|
|
1
|
+
|
2
|
+
[![Build Status](https://secure.travis-ci.org/irxground/irxrb.png?branch=master)](http://travis-ci.org/irxground/irxrb)
|
3
|
+
|
4
|
+
# Irxrb
|
5
|
+
|
6
|
+
TODO: Write a gem description
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'irxrb'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install irxrb
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
TODO: Write usage instructions here
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/irxrb.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/irxrb/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["irxground"]
|
6
|
+
gem.email = ["irxnjhtchlnrw@gmail.com"]
|
7
|
+
gem.description = %q{Ruby extension}
|
8
|
+
gem.summary = %q{Ruby extension}
|
9
|
+
gem.homepage = "https://github.com/irxground/irxrb"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "irxrb"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Irxrb::VERSION
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Irxrb
|
2
|
+
module GenericType
|
3
|
+
def type_parameter(*params)
|
4
|
+
m = self
|
5
|
+
define_singleton_method :[] do |*types|
|
6
|
+
raise ArgumentError if params.size != types.size
|
7
|
+
@_specific_type ||= {}
|
8
|
+
@_specific_type[types] ||=
|
9
|
+
begin
|
10
|
+
k = (m.is_a? Class) ?
|
11
|
+
Class.new(m) :
|
12
|
+
Module.new.tap{ |x| x.instance_eval { include m } }
|
13
|
+
params.zip(types).each do |param_name, type|
|
14
|
+
k.const_set param_name, type
|
15
|
+
k.instance_eval do
|
16
|
+
define_method(param_name){ type }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
k
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Irxrb
|
2
|
+
class HashObject
|
3
|
+
def initialize(hash)
|
4
|
+
raise ArgumentError, 'argument should be Hash' unless Hash === hash
|
5
|
+
@hash = hash
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(action, *args)
|
9
|
+
if @hash.has_key? action
|
10
|
+
self.class.class_eval do
|
11
|
+
define_method(action) do
|
12
|
+
@hash[action]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
return __send__ action
|
16
|
+
end
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Irxrb
|
2
|
+
module LazyAttribute
|
3
|
+
def lazy(name, &block)
|
4
|
+
method_name = "__#{name}_lazy"
|
5
|
+
self.class_eval <<-"END"
|
6
|
+
def #{name}=(value)
|
7
|
+
@#{name} = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def #{name}
|
11
|
+
@#{name} ||= #{method_name}
|
12
|
+
end
|
13
|
+
|
14
|
+
def #{name}?
|
15
|
+
@#{name} != nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def #{name}!
|
19
|
+
@#{name} = #{method_name}
|
20
|
+
end
|
21
|
+
END
|
22
|
+
self.instance_eval do
|
23
|
+
define_method method_name, block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/irxrb/maybe.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
module Irxrb
|
3
|
+
class Maybe
|
4
|
+
class << self
|
5
|
+
def [](value)
|
6
|
+
value != nil ? Just.new(value) : Nothing.instance
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Nothing < Maybe
|
11
|
+
@@instance = new
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
raise "#{self.class} can't be instantize."
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.instance
|
18
|
+
@@instance
|
19
|
+
end
|
20
|
+
|
21
|
+
def !@
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(name, *arg)
|
26
|
+
self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Just < Maybe
|
31
|
+
def initialize(value)
|
32
|
+
raise 'argument should not be nil' if value == nil
|
33
|
+
@value = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def !@
|
37
|
+
@value
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing(name, *arg)
|
41
|
+
ret =
|
42
|
+
block_given? ?
|
43
|
+
@value.__send__(name, *arg){|*a| yield *a } :
|
44
|
+
@value.__send__(name, *arg)
|
45
|
+
return Nothing.instance if ret == nil
|
46
|
+
return self if ret == @value
|
47
|
+
return Just.new(ret)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Irxrb::RangeToRegex
|
2
|
+
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def invoke(range)
|
6
|
+
max = range.max + 1
|
7
|
+
min = range.min
|
8
|
+
pivot = pivot(min, max)
|
9
|
+
|
10
|
+
ret = []
|
11
|
+
|
12
|
+
base = pivot
|
13
|
+
split_by_unit(max - pivot).each do |unit, diff|
|
14
|
+
ret << make_regex(base, unit, diff)
|
15
|
+
base += unit * diff
|
16
|
+
end
|
17
|
+
|
18
|
+
base = pivot
|
19
|
+
split_by_unit(pivot - min).each do |unit, diff|
|
20
|
+
ret.unshift make_regex(base, unit, -diff)
|
21
|
+
base -= unit * diff
|
22
|
+
end
|
23
|
+
|
24
|
+
/#{ret.join('|')}/
|
25
|
+
end
|
26
|
+
|
27
|
+
def unit_of(num)
|
28
|
+
unit = 1
|
29
|
+
loop do
|
30
|
+
return unit if num < unit * 10
|
31
|
+
unit *= 10
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def split_by_unit(num)
|
36
|
+
ret = []
|
37
|
+
unit = 1
|
38
|
+
while num > 0
|
39
|
+
n = num % 10
|
40
|
+
ret.unshift [unit, n] if n != 0
|
41
|
+
unit *= 10
|
42
|
+
num /= 10
|
43
|
+
end
|
44
|
+
return ret
|
45
|
+
end
|
46
|
+
|
47
|
+
def pivot(min, max)
|
48
|
+
unit = unit_of(max)
|
49
|
+
pivot = max / unit * unit
|
50
|
+
while unit > 1 && (min / pivot) == 1
|
51
|
+
unit /= 10
|
52
|
+
pivot = max / unit * unit
|
53
|
+
end
|
54
|
+
return pivot
|
55
|
+
end
|
56
|
+
|
57
|
+
def make_regex(base, unit, diff)
|
58
|
+
if diff < 0
|
59
|
+
return make_regex(base + unit * diff, unit, -diff)
|
60
|
+
end
|
61
|
+
prefix = base / (unit * 10)
|
62
|
+
prefix_str = prefix == 0 ? '' : prefix.to_s
|
63
|
+
current = base / unit % 10
|
64
|
+
current_str = diff == 1 ? current.to_s : "[#{current}-#{current + diff - 1}]"
|
65
|
+
suffix_str = "[0-9]" * Math.log(unit, 10).round
|
66
|
+
return prefix_str + current_str + suffix_str
|
67
|
+
end
|
68
|
+
end
|
File without changes
|
data/lib/irxrb.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'irxrb/version'
|
2
|
+
|
3
|
+
module Irxrb
|
4
|
+
autoload :HashObject, 'irxrb/hash_object'
|
5
|
+
autoload :Maybe, 'irxrb/maybe'
|
6
|
+
autoload :LazyAttribute, 'irxrb/lazy_attribute'
|
7
|
+
autoload :RangeToRegex, 'irxrb/range_to_regex'
|
8
|
+
autoload :GenericType, 'irxrb/generic_type'
|
9
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'irxrb/core_ext/match_data_to_hash'
|
3
|
+
|
4
|
+
describe MatchData, '#to_hash' do
|
5
|
+
subject do
|
6
|
+
/(?<a>\d+)-(?<b>\d+)-(?<c>\d+)/ =~ '12-34-56'
|
7
|
+
$~.to_hash
|
8
|
+
end
|
9
|
+
|
10
|
+
it { should be_an_instance_of(Hash) }
|
11
|
+
it { should == { 'a' => '12', 'b' => '34', 'c' => '56' } }
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'irxrb/core_ext/maybe'
|
3
|
+
|
4
|
+
describe '#maybe' do
|
5
|
+
it 'should raise error without block' do
|
6
|
+
expect{ Object.new.maybe }.to raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
specify { expect([1,2,3].maybe{ self.map{|x| x * x } }).to eq([1,4,9]) }
|
10
|
+
specify { expect( nil.maybe{ self.map{|x| x * x } }).to eq(nil ) }
|
11
|
+
specify { expect( 10.maybe{ self * self }).to eq(100 ) }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#Maybe' do
|
15
|
+
specify { Maybe(100).should be_an_instance_of(Irxrb::Maybe::Just) }
|
16
|
+
specify { Maybe(nil).should be_an_instance_of(Irxrb::Maybe::Nothing) }
|
17
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'irxrb/core_ext/to_proc'
|
3
|
+
|
4
|
+
describe '#to_proc' do
|
5
|
+
describe Object do
|
6
|
+
it 'should behave as ==' do
|
7
|
+
obj1 = Object.new
|
8
|
+
obj2 = Object.new
|
9
|
+
p = obj1.to_proc
|
10
|
+
|
11
|
+
p[obj1].should be_true
|
12
|
+
p[obj2].should_not be_true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Regexp do
|
17
|
+
it 'should behave as matcher' do
|
18
|
+
p = /o/.to_proc
|
19
|
+
|
20
|
+
p['foo'].should be_true
|
21
|
+
p['bar'].should_not be_true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Class do
|
26
|
+
it 'should behave as is_a?' do
|
27
|
+
is_str = String.to_proc
|
28
|
+
|
29
|
+
is_str['foo'].should be_true
|
30
|
+
is_str[12345].should_not be_true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe Proc do
|
35
|
+
it 'should not cause infinit loop' do
|
36
|
+
is_odd = proc{|num| num % 2 != 0 }
|
37
|
+
|
38
|
+
is_odd[1].should be_true
|
39
|
+
is_odd[2].should_not be_true
|
40
|
+
|
41
|
+
case 1
|
42
|
+
when is_odd
|
43
|
+
value = :odd
|
44
|
+
else
|
45
|
+
value = :even
|
46
|
+
end
|
47
|
+
value.should == :odd
|
48
|
+
|
49
|
+
case 2
|
50
|
+
when is_odd
|
51
|
+
value = :odd
|
52
|
+
else
|
53
|
+
value = :even
|
54
|
+
end
|
55
|
+
value.should == :even
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '&proc' do
|
60
|
+
specify { [1,2,3].map(&2).should == [false, true, false] }
|
61
|
+
specify { %w(foo bar baz).select(&/a/).should == %w(bar baz) }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'irxrb/core_ext/generic_type'
|
3
|
+
|
4
|
+
describe Irxrb::GenericType do
|
5
|
+
context 'generic class' do
|
6
|
+
class Factory
|
7
|
+
type_parameter :T
|
8
|
+
|
9
|
+
def create
|
10
|
+
self.T.new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'class' do
|
15
|
+
specify { Factory[String]::T.should == String }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'instance' do
|
19
|
+
subject { Factory[String].new }
|
20
|
+
it { should be_an_instance_of(Factory[String]) }
|
21
|
+
it { should be_kind_of(Factory) }
|
22
|
+
it { should_not be_an_instance_of(Factory[Object]) }
|
23
|
+
|
24
|
+
specify { subject.T == String }
|
25
|
+
specify { subject.create.should == "" }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'generic module' do
|
30
|
+
module Map
|
31
|
+
type_parameter :Key, :Value
|
32
|
+
end
|
33
|
+
|
34
|
+
class ConcreteMap
|
35
|
+
include Map[Integer, String]
|
36
|
+
end
|
37
|
+
|
38
|
+
subject { ConcreteMap }
|
39
|
+
specify { subject::Key.should == Integer }
|
40
|
+
specify { subject::Value.should == String }
|
41
|
+
specify { subject.new.should be_kind_of(Map) }
|
42
|
+
specify { subject.new.should be_kind_of(Map[Integer, String]) }
|
43
|
+
specify { subject.new.should_not be_kind_of(Map[String, String]) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Irxrb::HashObject do
|
4
|
+
describe 'constructor' do
|
5
|
+
it 'should raise error' do
|
6
|
+
expect {
|
7
|
+
Irxrb::HashObject.new("str")
|
8
|
+
}.to raise_error(ArgumentError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should create instance' do
|
12
|
+
expect {
|
13
|
+
Irxrb::HashObject.new(foo: bar)
|
14
|
+
}.not_to raise_error(ArgumentError)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'method' do
|
19
|
+
subject { Irxrb::HashObject.new(foo: 1) }
|
20
|
+
specify { expect( subject.foo ).to eq(1) }
|
21
|
+
specify { expect{ subject.bar }.to raise_error(NoMethodError) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Irxrb::LazyAttribute do
|
4
|
+
subject { TestObj.new }
|
5
|
+
|
6
|
+
its(:counter) { should == 0 }
|
7
|
+
its(:value?) { should == false }
|
8
|
+
|
9
|
+
describe 'after get attribute' do
|
10
|
+
before { subject.value.should == 'value1' }
|
11
|
+
its(:counter) { should == 1 }
|
12
|
+
its(:value?) { should == true }
|
13
|
+
its(:value) { should == 'value1' }
|
14
|
+
|
15
|
+
describe 'redo lazy' do
|
16
|
+
before { subject.value!.should == 'value2' }
|
17
|
+
its(:counter) { should == 2 }
|
18
|
+
its(:value?) { should == true }
|
19
|
+
its(:value) { should == 'value2' }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'redo lazy' do
|
24
|
+
before { subject.value!.should == 'value1' }
|
25
|
+
its(:counter) { should == 1 }
|
26
|
+
its(:value?) { should == true }
|
27
|
+
its(:value) { should == 'value1' }
|
28
|
+
end
|
29
|
+
|
30
|
+
class TestObj
|
31
|
+
extend Irxrb::LazyAttribute
|
32
|
+
|
33
|
+
attr_reader :counter
|
34
|
+
def initialize
|
35
|
+
@counter = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
lazy :value do
|
39
|
+
@counter += 1
|
40
|
+
"value#@counter"
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
data/spec/maybe_spec.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Irxrb::Maybe do
|
4
|
+
describe '.[]' do
|
5
|
+
specify { expect { Irxrb::Maybe[] }.to raise_error }
|
6
|
+
specify { Irxrb::Maybe[nil ].should be_kind_of(Irxrb::Maybe) }
|
7
|
+
specify { Irxrb::Maybe[false].should be_kind_of(Irxrb::Maybe) }
|
8
|
+
|
9
|
+
specify { Irxrb::Maybe[nil ].should be_an_instance_of(Irxrb::Maybe::Nothing) }
|
10
|
+
specify { Irxrb::Maybe[false].should be_an_instance_of(Irxrb::Maybe::Just) }
|
11
|
+
specify { Irxrb::Maybe[true ].should be_an_instance_of(Irxrb::Maybe::Just) }
|
12
|
+
specify { Irxrb::Maybe[1 ].should be_an_instance_of(Irxrb::Maybe::Just) }
|
13
|
+
specify { Irxrb::Maybe["str"].should be_an_instance_of(Irxrb::Maybe::Just) }
|
14
|
+
specify { Irxrb::Maybe[:sym ].should be_an_instance_of(Irxrb::Maybe::Just) }
|
15
|
+
specify { Irxrb::Maybe[Class].should be_an_instance_of(Irxrb::Maybe::Just) }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#!' do
|
19
|
+
specify { expect(!maybe(nil )).to eq(nil ) }
|
20
|
+
specify { expect(!maybe(1 )).to eq(1 ) }
|
21
|
+
specify { expect(!maybe("str")).to eq("str") }
|
22
|
+
specify { obj = Object.new; expect(!maybe(obj )).to eq(obj ) }
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'method call' do
|
26
|
+
describe Irxrb::Maybe::Nothing do
|
27
|
+
describe 'always return Nothing' do
|
28
|
+
subject { Irxrb::Maybe[nil] }
|
29
|
+
specify { expect(!subject ).to eq(nil) }
|
30
|
+
specify { expect(!subject.foo ).to eq(nil) }
|
31
|
+
specify { expect(!subject.foo.bar ).to eq(nil) }
|
32
|
+
specify { expect(!subject.foo(1) ).to eq(nil) }
|
33
|
+
specify { expect(!subject.foo{|x| x}).to eq(nil) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe Irxrb::Maybe::Just do
|
38
|
+
before do
|
39
|
+
@mock = Object.new
|
40
|
+
@mock.stub(ret_nil: nil)
|
41
|
+
@mock.stub(ret_self: @mock)
|
42
|
+
end
|
43
|
+
|
44
|
+
specify { expect(!maybe(@mock).ret_self).to eq(@mock) }
|
45
|
+
specify { expect(!maybe(@mock).ret_nil ).to eq(nil) }
|
46
|
+
specify { expect(!maybe(@mock).ret_self.ret_self).to eq(@mock) }
|
47
|
+
specify { expect(!maybe(@mock).ret_self.ret_nil ).to eq(nil) }
|
48
|
+
specify { expect(!maybe(@mock).ret_nil .ret_self).to eq(nil) }
|
49
|
+
specify { expect(!maybe(@mock).ret_nil .ret_nil ).to eq(nil) }
|
50
|
+
specify { expect(!maybe([1,2,3]).map{|x| x*x }).to eq([1,4,9]) }
|
51
|
+
specify { expect(!maybe([1,2,3]).inject{|a,x| a+x }).to eq(6) }
|
52
|
+
specify { expect(!maybe(a: 1)[:a]).to eq(1) }
|
53
|
+
specify { expect(!maybe(a: 1)[:b][:c]).to eq(nil) }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '.new' do
|
58
|
+
specify { expect{ Irxrb::Maybe::Just.new(nil) }.to raise_error }
|
59
|
+
specify { expect{ Irxrb::Maybe::Nothing.new }.to raise_error }
|
60
|
+
end
|
61
|
+
|
62
|
+
def maybe(value)
|
63
|
+
Irxrb::Maybe[value]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Irxrb::RangeToRegex do
|
4
|
+
include Irxrb::RangeToRegex
|
5
|
+
|
6
|
+
describe '#invoke' do
|
7
|
+
specify { invoke(1.. 9).should == /[1-9]/ }
|
8
|
+
specify { invoke(1... 9).should == /[1-8]/ }
|
9
|
+
specify { invoke(1.. 10).should == /[1-9]|10/ }
|
10
|
+
specify { invoke(1.. 11).should == /[1-9]|1[0-1]/ }
|
11
|
+
specify { invoke(1.. 99).should == /[1-9]|[1-9][0-9]/ }
|
12
|
+
specify { invoke(1..100).should == /[1-9]|[1-9][0-9]|100/ }
|
13
|
+
specify { invoke(1..101).should == /[1-9]|[1-9][0-9]|10[0-1]/ }
|
14
|
+
specify { invoke(1..110).should == /[1-9]|[1-9][0-9]|10[0-9]|110/ }
|
15
|
+
specify { invoke(1..111).should == /[1-9]|[1-9][0-9]|10[0-9]|11[0-1]/ }
|
16
|
+
|
17
|
+
specify { invoke(59.. 79).should == /59|[6-7][0-9]/ }
|
18
|
+
specify { invoke(60.. 79).should == /[6-7][0-9]/ }
|
19
|
+
specify { invoke(61.. 79).should == /6[1-9]|7[0-9]/ }
|
20
|
+
specify { invoke(59.. 80).should == /59|[6-7][0-9]|80/ }
|
21
|
+
specify { invoke(60.. 80).should == /[6-7][0-9]|80/ }
|
22
|
+
specify { invoke(61.. 80).should == /6[1-9]|7[0-9]|80/ }
|
23
|
+
specify { invoke(59.. 81).should == /59|[6-7][0-9]|8[0-1]/ }
|
24
|
+
specify { invoke(60.. 81).should == /[6-7][0-9]|8[0-1]/ }
|
25
|
+
specify { invoke(61.. 81).should == /6[1-9]|7[0-9]|8[0-1]/ }
|
26
|
+
|
27
|
+
specify { invoke(10..100).should == /[1-9][0-9]|100/ }
|
28
|
+
specify do
|
29
|
+
actual = invoke(987..654321)
|
30
|
+
expect = [
|
31
|
+
'98[7-9]|99[0-9]',
|
32
|
+
'[1-9][0-9][0-9][0-9]',
|
33
|
+
'[1-9][0-9][0-9][0-9][0-9]',
|
34
|
+
'[1-5][0-9][0-9][0-9][0-9][0-9]',
|
35
|
+
'6[0-4][0-9][0-9][0-9][0-9]',
|
36
|
+
'65[0-3][0-9][0-9][0-9]',
|
37
|
+
'654[0-2][0-9][0-9]',
|
38
|
+
'6543[0-1][0-9]',
|
39
|
+
'65432[0-1]'].join('|')
|
40
|
+
actual.should == /#{expect}/
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#unit_of' do
|
45
|
+
specify { unit_of( 8).should == 1 }
|
46
|
+
specify { unit_of( 10).should == 10 }
|
47
|
+
specify { unit_of( 99).should == 10 }
|
48
|
+
specify { unit_of(100).should == 100 }
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#split_by_unit' do
|
52
|
+
specify { split_by_unit(1905).should == [[1000, 1], [100, 9], [1, 5]] }
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#pivot' do
|
56
|
+
specify { pivot(100, 200).should == 200 }
|
57
|
+
specify { pivot(199, 200).should == 200 }
|
58
|
+
specify { pivot( 99, 200).should == 200 }
|
59
|
+
specify { pivot(100, 299).should == 200 }
|
60
|
+
specify { pivot(100, 201).should == 200 }
|
61
|
+
|
62
|
+
specify { pivot(1860, 1970).should == 1900 }
|
63
|
+
specify { pivot(1960, 1970).should == 1970 }
|
64
|
+
specify { pivot(1986, 1987).should == 1987 }
|
65
|
+
|
66
|
+
specify { pivot(9876, 9876).should == 9876 }
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#make_regex' do
|
70
|
+
specify { make_regex(100, 10, 2).should == '1[0-1][0-9]' }
|
71
|
+
specify { make_regex(130, 10, 3).should == '1[3-5][0-9]' }
|
72
|
+
specify { make_regex(100, 10, 1).should == '10[0-9]' }
|
73
|
+
specify { make_regex(130, 10, 1).should == '13[0-9]' }
|
74
|
+
|
75
|
+
specify { make_regex( 10, 10, 3).should == '[1-3][0-9]' }
|
76
|
+
specify { make_regex( 100, 100, 3).should == '[1-3][0-9][0-9]' }
|
77
|
+
specify { make_regex(1000, 1000, 3).should == '[1-3][0-9][0-9][0-9]' }
|
78
|
+
end
|
79
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: irxrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- irxground
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby extension
|
14
|
+
email:
|
15
|
+
- irxnjhtchlnrw@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- .travis.yml
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- irxrb.gemspec
|
27
|
+
- irxrb.sublime-project
|
28
|
+
- lib/irxrb.rb
|
29
|
+
- lib/irxrb/core_ext/generic_type.rb
|
30
|
+
- lib/irxrb/core_ext/hash_to_obj.rb
|
31
|
+
- lib/irxrb/core_ext/match_data_to_hash.rb
|
32
|
+
- lib/irxrb/core_ext/maybe.rb
|
33
|
+
- lib/irxrb/core_ext/range_to_regex.rb
|
34
|
+
- lib/irxrb/core_ext/to_proc.rb
|
35
|
+
- lib/irxrb/generic_type.rb
|
36
|
+
- lib/irxrb/hash_object.rb
|
37
|
+
- lib/irxrb/lazy_attribute.rb
|
38
|
+
- lib/irxrb/maybe.rb
|
39
|
+
- lib/irxrb/range_to_regex.rb
|
40
|
+
- lib/irxrb/validation_struct.rb
|
41
|
+
- lib/irxrb/version.rb
|
42
|
+
- spec/core_ext/hash_to_obj_spec.rb
|
43
|
+
- spec/core_ext/match_data_to_hash_spec.rb
|
44
|
+
- spec/core_ext/maybe_spec.rb
|
45
|
+
- spec/core_ext/range_to_regex_spec.rb
|
46
|
+
- spec/core_ext/to_proc_spec.rb
|
47
|
+
- spec/generic_type_spec.rb
|
48
|
+
- spec/hash_object_spec.rb
|
49
|
+
- spec/lazy_attribute_spec.rb
|
50
|
+
- spec/maybe_spec.rb
|
51
|
+
- spec/range_to_regex_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: https://github.com/irxground/irxrb
|
54
|
+
licenses: []
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.1.9
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Ruby extension
|
76
|
+
test_files:
|
77
|
+
- spec/core_ext/hash_to_obj_spec.rb
|
78
|
+
- spec/core_ext/match_data_to_hash_spec.rb
|
79
|
+
- spec/core_ext/maybe_spec.rb
|
80
|
+
- spec/core_ext/range_to_regex_spec.rb
|
81
|
+
- spec/core_ext/to_proc_spec.rb
|
82
|
+
- spec/generic_type_spec.rb
|
83
|
+
- spec/hash_object_spec.rb
|
84
|
+
- spec/lazy_attribute_spec.rb
|
85
|
+
- spec/maybe_spec.rb
|
86
|
+
- spec/range_to_regex_spec.rb
|
87
|
+
- spec/spec_helper.rb
|