lego 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +13 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +3 -0
- data/Rakefile +7 -0
- data/lego.gemspec +21 -0
- data/lib/lego.rb +22 -0
- data/lib/lego/either.rb +91 -0
- data/lib/lego/model.rb +69 -0
- data/lib/lego/value.rb +15 -0
- data/lib/lego/value/base.rb +34 -0
- data/lib/lego/value/boolean.rb +26 -0
- data/lib/lego/value/date.rb +12 -0
- data/lib/lego/value/float.rb +22 -0
- data/lib/lego/value/integer.rb +22 -0
- data/lib/lego/value/set.rb +39 -0
- data/lib/lego/value/string.rb +20 -0
- data/spec/lego/either/fail_spec.rb +38 -0
- data/spec/lego/either/just_spec.rb +50 -0
- data/spec/lego/either/none_spec.rb +37 -0
- data/spec/lego/model_spec.rb +93 -0
- data/spec/lego/value/base_spec.rb +14 -0
- data/spec/lego/value/boolean_spec.rb +62 -0
- data/spec/lego/value/date_spec.rb +71 -0
- data/spec/lego/value/float_spec.rb +93 -0
- data/spec/lego/value/integer_spec.rb +86 -0
- data/spec/lego/value/set_spec.rb +77 -0
- data/spec/lego/value/string_spec.rb +106 -0
- data/spec/spec_helper.rb +38 -0
- metadata +142 -0
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lego::Value::Integer do
|
4
|
+
|
5
|
+
describe '#parse' do
|
6
|
+
subject { Lego::Value::Integer.new(opts) }
|
7
|
+
|
8
|
+
let(:opts){ {} }
|
9
|
+
|
10
|
+
context 'nil' do
|
11
|
+
specify { subject.parse(nil).should be_nothing }
|
12
|
+
specify { subject.parse('').should be_nothing }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'invalid integer' do
|
16
|
+
specify { subject.parse(' ').should be_error("invalid integer: ' '") }
|
17
|
+
specify { subject.parse(123.456).should be_error("invalid integer: '123.456'") }
|
18
|
+
specify { subject.parse('2012-02').should be_error("invalid integer: '2012-02'") }
|
19
|
+
specify { subject.parse('invalid').should be_error("invalid integer: 'invalid'") }
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'parses integer' do
|
23
|
+
subject.parse(123).should be_just(123)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'parses string integer' do
|
27
|
+
subject.parse('-134').should be_just(-134)
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'minimum' do
|
31
|
+
let(:opts){ { min: 4 } }
|
32
|
+
specify { subject.parse(4).should be_just(4) }
|
33
|
+
specify { subject.parse(3).should be_error("less than minimum of 4: '3'") }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'maximum' do
|
37
|
+
let(:opts){ { max: 4 } }
|
38
|
+
specify { subject.parse(4).should be_just(4) }
|
39
|
+
specify { subject.parse(5).should be_error("more than maximum of 4: '5'") }
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'minimium and maximum' do
|
43
|
+
let(:opts){ { min: 0, max: 4 } }
|
44
|
+
specify { subject.parse(1).should be_just(1) }
|
45
|
+
specify { subject.parse(-1).should be_error("less than minimum of 0: '-1'") }
|
46
|
+
specify { subject.parse(5).should be_error("more than maximum of 4: '5'") }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#coerce' do
|
51
|
+
context 'nothing' do
|
52
|
+
context 'default' do
|
53
|
+
it 'raises error' do
|
54
|
+
expect{ subject.coerce(nil) }.to raise_error(Lego::CoerceError, 'missing value')
|
55
|
+
expect{ subject.coerce('') }.to raise_error(Lego::CoerceError, 'missing value')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with :default handler' do
|
60
|
+
subject { Lego::Value::Integer.new(default: handler) }
|
61
|
+
|
62
|
+
context 'nil handler' do
|
63
|
+
let(:handler) { ->{ nil } }
|
64
|
+
specify { subject.coerce(nil).should be_nil }
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'lambda handler' do
|
68
|
+
let(:handler) { ->{ 42 } }
|
69
|
+
specify { subject.coerce('').should == 42 }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'failure' do
|
75
|
+
it 'raises error' do
|
76
|
+
expect{ subject.coerce('foo') }.to raise_error(Lego::CoerceError, "invalid integer: 'foo'")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'success' do
|
81
|
+
it 'returns integer' do
|
82
|
+
subject.coerce('42').should == 42
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lego::Value::Set do
|
4
|
+
|
5
|
+
def set(elem)
|
6
|
+
Array(elem).to_set
|
7
|
+
end
|
8
|
+
|
9
|
+
subject { Lego::Value::Set.new(:string) }
|
10
|
+
|
11
|
+
describe '#parse' do
|
12
|
+
context 'nil' do
|
13
|
+
specify { subject.parse(nil).should be_nothing }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'invalid set or item' do
|
17
|
+
specify { subject.parse('').should be_error("invalid set: ''") }
|
18
|
+
specify { subject.parse('2012-02').should be_error("invalid set: '2012-02'") }
|
19
|
+
specify { subject.parse(['one', 123, 'two']).should be_error("invalid string: '123'") }
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'parses array' do
|
23
|
+
subject.parse(['one']).should be_just(set('one'))
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'parses set' do
|
27
|
+
subject.parse(set('one')).should be_just(set('one'))
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'allow empty' do
|
31
|
+
subject { Lego::Value::Set.new(:string, allow_empty: true) }
|
32
|
+
specify { subject.parse([]).should be_just(set([])) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'disallow empty' do
|
36
|
+
subject { Lego::Value::Set.new(:string, allow_empty: false) }
|
37
|
+
specify { subject.parse([]).should be_nothing }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#coerce' do
|
42
|
+
context 'missing' do
|
43
|
+
it 'raises error' do
|
44
|
+
expect{ subject.coerce(nil) }.to raise_error(Lego::CoerceError, 'missing value')
|
45
|
+
expect{ subject.coerce([]) }.to raise_error(Lego::CoerceError, 'missing value')
|
46
|
+
expect{ subject.coerce([].to_set) }.to raise_error(Lego::CoerceError, 'missing value')
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with :default handler' do
|
50
|
+
subject { Lego::Value::String.new(default: handler) }
|
51
|
+
|
52
|
+
context 'nil handler' do
|
53
|
+
let(:handler) { ->{ nil } }
|
54
|
+
specify { subject.coerce(nil).should be_nil }
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'lambda handler' do
|
58
|
+
let(:handler) { proc{ Set.new } }
|
59
|
+
specify { subject.coerce('').should == [].to_set }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'failure' do
|
65
|
+
it 'raises error' do
|
66
|
+
expect{ subject.coerce([123]) }.to raise_error(Lego::CoerceError, "invalid string: '123'")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'success' do
|
71
|
+
it 'returns set' do
|
72
|
+
subject.coerce(['foo']).should == set('foo')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lego::Value::String do
|
4
|
+
|
5
|
+
describe '#parse' do
|
6
|
+
subject { Lego::Value::String.new(opts) }
|
7
|
+
|
8
|
+
let(:allow_blank) { true }
|
9
|
+
let(:strip) { true }
|
10
|
+
|
11
|
+
let(:opts) do
|
12
|
+
{
|
13
|
+
allow_blank: allow_blank,
|
14
|
+
strip: strip
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'nil' do
|
19
|
+
specify { subject.parse(nil).should be_nothing }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'not a string' do
|
23
|
+
specify { subject.parse(Date.new(2012, 12, 26)).should be_error("invalid string: '2012-12-26'") }
|
24
|
+
specify { subject.parse(123).should be_error("invalid string: '123'") }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'allow blank' do
|
28
|
+
let(:allow_blank) { true }
|
29
|
+
|
30
|
+
context 'strip' do
|
31
|
+
let(:strip) { true }
|
32
|
+
specify { subject.parse('').should be_just('') }
|
33
|
+
specify { subject.parse(' ').should be_just('') }
|
34
|
+
specify { subject.parse(' foo ').should be_just('foo') }
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'no strip' do
|
38
|
+
let(:strip) { false }
|
39
|
+
specify { subject.parse('').should be_just('') }
|
40
|
+
specify { subject.parse(' ').should be_just(' ') }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'disallow blank' do
|
45
|
+
let(:allow_blank) { false }
|
46
|
+
|
47
|
+
context 'strip' do
|
48
|
+
let(:strip) { true }
|
49
|
+
specify { subject.parse('').should be_nothing }
|
50
|
+
specify { subject.parse(' ').should be_nothing }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'no strip' do
|
54
|
+
let(:strip) { false }
|
55
|
+
specify { subject.parse('').should be_nothing }
|
56
|
+
specify { subject.parse(' ').should be_nothing }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'defaults' do
|
61
|
+
subject { Lego::Value::String.new }
|
62
|
+
|
63
|
+
its(:allow_blank?){ should == false }
|
64
|
+
its(:strip?){ should == true }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
describe '#coerce' do
|
70
|
+
context 'nothing' do
|
71
|
+
context 'default' do
|
72
|
+
it 'raises error' do
|
73
|
+
expect{ subject.coerce(nil) }.to raise_error(Lego::CoerceError, 'missing value')
|
74
|
+
expect{ subject.coerce('') }.to raise_error(Lego::CoerceError, 'missing value')
|
75
|
+
expect{ subject.coerce(' ') }.to raise_error(Lego::CoerceError, 'missing value')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with :default handler' do
|
80
|
+
subject { Lego::Value::String.new(default: handler) }
|
81
|
+
|
82
|
+
context 'nil handler' do
|
83
|
+
let(:handler) { ->{ nil } }
|
84
|
+
specify { subject.coerce(nil).should be_nil }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'lambda handler' do
|
88
|
+
let(:handler) { ->{ 'missing' } }
|
89
|
+
specify { subject.coerce(' ').should == 'missing' }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'failure' do
|
95
|
+
it 'raises error' do
|
96
|
+
expect{ subject.coerce(123) }.to raise_error(Lego::CoerceError, "invalid string: '123'")
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'success' do
|
101
|
+
it 'returns string' do
|
102
|
+
subject.coerce('foo bar').should == 'foo bar'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative '../lib/lego'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :be_nothing do
|
4
|
+
match do |actual|
|
5
|
+
actual.kind_of?(Lego::Either::None)
|
6
|
+
end
|
7
|
+
|
8
|
+
failure_message_for_should do |actual|
|
9
|
+
"Expected <Lego::None>, not #{actual.inspect}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec::Matchers.define :be_error do |expected|
|
14
|
+
match do |actual|
|
15
|
+
actual.kind_of?(Lego::Either::Fail) and actual.error == expected
|
16
|
+
end
|
17
|
+
|
18
|
+
failure_message_for_should do |actual|
|
19
|
+
"Expected <Lego::Either::Fail #{expected.inspect}>, not #{actual.inspect}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec::Matchers.define :be_just do |expected|
|
24
|
+
match do |actual|
|
25
|
+
actual.kind_of?(Lego::Either::Just) and actual.value == expected
|
26
|
+
end
|
27
|
+
|
28
|
+
failure_message_for_should do |actual|
|
29
|
+
"Expected <Lego::Either::Just #{expected.inspect}>, not #{actual.inspect}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
RSpec.configure do |config|
|
35
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
36
|
+
config.run_all_when_everything_filtered = true
|
37
|
+
config.filter_run :focus
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lego
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Norbert Wojtowicz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Data building blocks for you app.
|
63
|
+
email:
|
64
|
+
- wojtowicz.norbert@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- Guardfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lego.gemspec
|
77
|
+
- lib/lego.rb
|
78
|
+
- lib/lego/either.rb
|
79
|
+
- lib/lego/model.rb
|
80
|
+
- lib/lego/value.rb
|
81
|
+
- lib/lego/value/base.rb
|
82
|
+
- lib/lego/value/boolean.rb
|
83
|
+
- lib/lego/value/date.rb
|
84
|
+
- lib/lego/value/float.rb
|
85
|
+
- lib/lego/value/integer.rb
|
86
|
+
- lib/lego/value/set.rb
|
87
|
+
- lib/lego/value/string.rb
|
88
|
+
- spec/lego/either/fail_spec.rb
|
89
|
+
- spec/lego/either/just_spec.rb
|
90
|
+
- spec/lego/either/none_spec.rb
|
91
|
+
- spec/lego/model_spec.rb
|
92
|
+
- spec/lego/value/base_spec.rb
|
93
|
+
- spec/lego/value/boolean_spec.rb
|
94
|
+
- spec/lego/value/date_spec.rb
|
95
|
+
- spec/lego/value/float_spec.rb
|
96
|
+
- spec/lego/value/integer_spec.rb
|
97
|
+
- spec/lego/value/set_spec.rb
|
98
|
+
- spec/lego/value/string_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: https://github.com/pithyless/lego
|
101
|
+
licenses: []
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
hash: 179225649257381862
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: 179225649257381862
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.23
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: Data building blocks for you app.
|
130
|
+
test_files:
|
131
|
+
- spec/lego/either/fail_spec.rb
|
132
|
+
- spec/lego/either/just_spec.rb
|
133
|
+
- spec/lego/either/none_spec.rb
|
134
|
+
- spec/lego/model_spec.rb
|
135
|
+
- spec/lego/value/base_spec.rb
|
136
|
+
- spec/lego/value/boolean_spec.rb
|
137
|
+
- spec/lego/value/date_spec.rb
|
138
|
+
- spec/lego/value/float_spec.rb
|
139
|
+
- spec/lego/value/integer_spec.rb
|
140
|
+
- spec/lego/value/set_spec.rb
|
141
|
+
- spec/lego/value/string_spec.rb
|
142
|
+
- spec/spec_helper.rb
|