necromancer 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/necromancer.rb +4 -3
- data/lib/necromancer/context.rb +4 -4
- data/lib/necromancer/conversion_target.rb +5 -5
- data/lib/necromancer/conversions.rb +10 -10
- data/lib/necromancer/converter.rb +1 -1
- data/lib/necromancer/converters/array.rb +6 -6
- data/lib/necromancer/converters/boolean.rb +2 -2
- data/lib/necromancer/converters/date_time.rb +4 -4
- data/lib/necromancer/converters/numeric.rb +7 -7
- data/lib/necromancer/converters/range.rb +6 -6
- data/lib/necromancer/null_converter.rb +1 -1
- data/lib/necromancer/version.rb +1 -1
- metadata +9 -77
- data/Rakefile +0 -8
- data/necromancer.gemspec +0 -34
- data/spec/spec_helper.rb +0 -53
- data/spec/unit/can_spec.rb +0 -9
- data/spec/unit/config_spec.rb +0 -30
- data/spec/unit/configuration/new_spec.rb +0 -28
- data/spec/unit/conversions/fetch_spec.rb +0 -16
- data/spec/unit/conversions/register_spec.rb +0 -58
- data/spec/unit/conversions/to_hash_spec.rb +0 -37
- data/spec/unit/convert_spec.rb +0 -130
- data/spec/unit/converters/array/array_to_boolean_spec.rb +0 -20
- data/spec/unit/converters/array/array_to_numeric_spec.rb +0 -20
- data/spec/unit/converters/array/array_to_set_spec.rb +0 -16
- data/spec/unit/converters/array/object_to_array_spec.rb +0 -19
- data/spec/unit/converters/array/string_to_array_spec.rb +0 -31
- data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +0 -24
- data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +0 -20
- data/spec/unit/converters/boolean/string_to_boolean_spec.rb +0 -34
- data/spec/unit/converters/date_time/string_to_date_spec.rb +0 -24
- data/spec/unit/converters/date_time/string_to_datetime_spec.rb +0 -30
- data/spec/unit/converters/date_time/string_to_time_spec.rb +0 -28
- data/spec/unit/converters/numeric/string_to_float_spec.rb +0 -46
- data/spec/unit/converters/numeric/string_to_integer_spec.rb +0 -60
- data/spec/unit/converters/numeric/string_to_numeric_spec.rb +0 -30
- data/spec/unit/converters/range/string_to_range_spec.rb +0 -33
- data/spec/unit/inspect_spec.rb +0 -14
- data/spec/unit/new_spec.rb +0 -10
- data/spec/unit/register_spec.rb +0 -15
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::ArrayConverters::ArrayToSetConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:array, :set) }
|
6
|
-
|
7
|
-
it "converts `[:x,:y,:x,1,2,1]` to set" do
|
8
|
-
expect(converter.call([:x,:y,:x,1,2,1])).to eql(Set[:x,:y,1,2])
|
9
|
-
end
|
10
|
-
|
11
|
-
it "fails to convert `1` to set" do
|
12
|
-
expect {
|
13
|
-
converter.call(1, strict: true)
|
14
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
15
|
-
end
|
16
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::ArrayConverters::ObjectToArrayConverter, '.call' do
|
4
|
-
subject(:converter) { described_class.new(:object, :array) }
|
5
|
-
|
6
|
-
it "converts nil to array" do
|
7
|
-
expect(converter.call(nil)).to eq([])
|
8
|
-
end
|
9
|
-
|
10
|
-
it "converts custom object to array" do
|
11
|
-
Custom = Class.new do
|
12
|
-
def to_ary
|
13
|
-
[:x, :y]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
custom = Custom.new
|
17
|
-
expect(converter.call(custom)).to eq([:x, :y])
|
18
|
-
end
|
19
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::ArrayConverters::StringToArrayConverter, '.call' do
|
4
|
-
subject(:converter) { described_class.new(:string, :array) }
|
5
|
-
|
6
|
-
it "converts empty string to array" do
|
7
|
-
expect(converter.call('', strict: false)).to eq([''])
|
8
|
-
end
|
9
|
-
|
10
|
-
it "fails to convert empty string to array in strict mode" do
|
11
|
-
expect {
|
12
|
-
converter.call('', strict: true)
|
13
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "converts `1,2,3` to array" do
|
17
|
-
expect(converter.call('1,2,3')).to eq([1,2,3])
|
18
|
-
end
|
19
|
-
|
20
|
-
it "converts `a,b,c` to array" do
|
21
|
-
expect(converter.call('a,b,c')).to eq(['a','b','c'])
|
22
|
-
end
|
23
|
-
|
24
|
-
it "converts '1-2-3' to array" do
|
25
|
-
expect(converter.call('1-2-3')).to eq([1,2,3])
|
26
|
-
end
|
27
|
-
|
28
|
-
it "converts ' 1 - 2 - 3 ' to array" do
|
29
|
-
expect(converter.call(' 1 - 2 - 3 ')).to eq([1,2,3])
|
30
|
-
end
|
31
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::BooleanConverters::BooleanToIntegerConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new }
|
6
|
-
|
7
|
-
it "converts true to 1 value" do
|
8
|
-
expect(converter.call(true)).to eq(1)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "converts false to 0 value" do
|
12
|
-
expect(converter.call(false)).to eq(0)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "fails to convert in strict mode" do
|
16
|
-
expect {
|
17
|
-
converter.call('unknown', strict: true)
|
18
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "returns value in non-strict mode" do
|
22
|
-
expect(converter.call('unknown', strict: false)).to eq('unknown')
|
23
|
-
end
|
24
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::BooleanConverters::IntegerToBooleanConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new }
|
6
|
-
|
7
|
-
it "converts 1 to true value" do
|
8
|
-
expect(converter.call(1)).to eq(true)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "converts 0 to false value" do
|
12
|
-
expect(converter.call(0)).to eq(false)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "fails to convert in strict mode" do
|
16
|
-
expect {
|
17
|
-
converter.call('1', strict: true)
|
18
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
19
|
-
end
|
20
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::BooleanConverters::StringToBooleanConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :boolean) }
|
6
|
-
|
7
|
-
it "raises error for empty string strict mode" do
|
8
|
-
expect {
|
9
|
-
converter.call('', strict: true)
|
10
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "fails to convert unkonwn value FOO" do
|
14
|
-
expect {
|
15
|
-
converter.call('FOO', strict: true)
|
16
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "passes through boolean value" do
|
20
|
-
expect(converter.call(true)).to eq(true)
|
21
|
-
end
|
22
|
-
|
23
|
-
%w[true TRUE t T 1 y Y YES yes on ON].each do |value|
|
24
|
-
it "converts '#{value}' to true value" do
|
25
|
-
expect(converter.call(value)).to eq(true)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
%w[false FALSE f F 0 n N NO No no off OFF].each do |value|
|
30
|
-
it "converts '#{value}' to false value" do
|
31
|
-
expect(converter.call(value)).to eq(false)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::DateTimeConverters::StringToDateConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :date) }
|
6
|
-
|
7
|
-
it "converts '1-1-2015' to date value" do
|
8
|
-
expect(converter.call('1-1-2015')).to eq(Date.parse('2015/01/01'))
|
9
|
-
end
|
10
|
-
|
11
|
-
it "converts '2014/12/07' to date value" do
|
12
|
-
expect(converter.call('2014/12/07')).to eq(Date.parse('2014/12/07'))
|
13
|
-
end
|
14
|
-
|
15
|
-
it "converts '2014-12-07' to date value" do
|
16
|
-
expect(converter.call('2014-12-07')).to eq(Date.parse('2014/12/07'))
|
17
|
-
end
|
18
|
-
|
19
|
-
it "fails to convert in strict mode" do
|
20
|
-
expect {
|
21
|
-
converter.call('2014 - 12 - 07', strict: true)
|
22
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
23
|
-
end
|
24
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::DateTimeConverters::StringToDateTimeConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :datetime) }
|
6
|
-
|
7
|
-
it "converts '2014/12/07' to date value" do
|
8
|
-
expect(converter.call('2014/12/07')).to eq(DateTime.parse('2014/12/07'))
|
9
|
-
end
|
10
|
-
|
11
|
-
it "converts '2014-12-07' to date value" do
|
12
|
-
expect(converter.call('2014-12-07')).to eq(DateTime.parse('2014-12-07'))
|
13
|
-
end
|
14
|
-
|
15
|
-
it "converts '7th December 2014' to datetime value" do
|
16
|
-
expect(converter.call('7th December 2014')).
|
17
|
-
to eq(DateTime.parse('2014-12-07'))
|
18
|
-
end
|
19
|
-
|
20
|
-
it "converts '7th December 2014 17:19:44' to datetime value" do
|
21
|
-
expect(converter.call('7th December 2014 17:19:44')).
|
22
|
-
to eq(DateTime.parse('2014-12-07 17:19:44'))
|
23
|
-
end
|
24
|
-
|
25
|
-
it "fails to convert in strict mode" do
|
26
|
-
expect {
|
27
|
-
converter.call('2014 - 12 - 07', strict: true)
|
28
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
29
|
-
end
|
30
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::DateTimeConverters::StringToTimeConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :time) }
|
6
|
-
|
7
|
-
it "converts to time instance" do
|
8
|
-
expect(converter.call('01/01/2015')).to be_a(Time)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "converts '01/01/2015' to time value" do
|
12
|
-
expect(converter.call('01/01/2015')).to eq(Time.parse('01/01/2015'))
|
13
|
-
end
|
14
|
-
|
15
|
-
it "converts '01/01/2015 08:35' to time value" do
|
16
|
-
expect(converter.call('01/01/2015 08:35')).to eq(Time.parse('01/01/2015 08:35'))
|
17
|
-
end
|
18
|
-
|
19
|
-
it "converts '12:35' to time value" do
|
20
|
-
expect(converter.call('12:35')).to eq(Time.parse('12:35'))
|
21
|
-
end
|
22
|
-
|
23
|
-
it "fails to convert in strict mode" do
|
24
|
-
expect {
|
25
|
-
converter.call('11-13-2015', strict: true)
|
26
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
27
|
-
end
|
28
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::NumericConverters::StringToFloatConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :float) }
|
6
|
-
|
7
|
-
it "raises error for empty string in strict mode" do
|
8
|
-
expect {
|
9
|
-
converter.call('', strict: true)
|
10
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
11
|
-
end
|
12
|
-
|
13
|
-
{
|
14
|
-
'1' => 1.0,
|
15
|
-
'+1' => 1.0,
|
16
|
-
'-1' => -1.0,
|
17
|
-
'1e1' => 10.0,
|
18
|
-
'1e-1' => 0.1,
|
19
|
-
'-1e1' => -10.0,
|
20
|
-
'-1e-1' => -0.1,
|
21
|
-
'1.0' => 1.0,
|
22
|
-
'1.0e+1' => 10.0,
|
23
|
-
'1.0e-1' => 0.1,
|
24
|
-
'-1.0e+1' => -10.0,
|
25
|
-
'-1.0e-1' => -0.1,
|
26
|
-
'.1' => 0.1,
|
27
|
-
'.1e+1' => 1.0,
|
28
|
-
'.1e-1' => 0.01,
|
29
|
-
'-.1e+1' => -1.0,
|
30
|
-
'-.1e-1' => -0.01
|
31
|
-
}.each do |actual, expected|
|
32
|
-
it "converts '#{actual}' to float value" do
|
33
|
-
expect(converter.call(actual)).to eql(expected)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
it "failse to convert '1.2a' in strict mode" do
|
38
|
-
expect {
|
39
|
-
converter.call('1.2a', strict: true)
|
40
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "converts '1.2a' in non-strict mode" do
|
44
|
-
expect(converter.call('1.2a', strict: false)).to eq(1.2)
|
45
|
-
end
|
46
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::NumericConverters::StringToIntegerConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :integer) }
|
6
|
-
|
7
|
-
{
|
8
|
-
'1' => 1,
|
9
|
-
'+1' => 1,
|
10
|
-
'-1' => -1,
|
11
|
-
'1e+1' => 1,
|
12
|
-
'+1e-1' => 1,
|
13
|
-
'-1e1' => -1,
|
14
|
-
'-1e-1' => -1,
|
15
|
-
'1.0' => 1,
|
16
|
-
'1.0e+1' => 1,
|
17
|
-
'1.0e-1' => 1,
|
18
|
-
'-1.0e+1' => -1,
|
19
|
-
'-1.0e-1' => -1,
|
20
|
-
'.1' => 0,
|
21
|
-
'.1e+1' => 0,
|
22
|
-
'.1e-1' => 0,
|
23
|
-
'-.1e+1' => 0,
|
24
|
-
'-.1e-1' => 0
|
25
|
-
}.each do |actual, expected|
|
26
|
-
it "converts '#{actual}' to float value" do
|
27
|
-
expect(converter.call(actual)).to eql(expected)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
it "raises error for empty string in strict mode" do
|
32
|
-
expect {
|
33
|
-
converter.call('', strict: true)
|
34
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "converts empty string to 0 in non-strict mode" do
|
38
|
-
expect(converter.call('', strict: false)).to eq(0)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "raises error for float in strict mode" do
|
42
|
-
expect {
|
43
|
-
converter.call('1.2', strict: true)
|
44
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
45
|
-
end
|
46
|
-
|
47
|
-
it "converts float to integer in non-strict mode" do
|
48
|
-
expect(converter.call(1.2)).to eq(1)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "converts mixed string to integer in non-strict mode" do
|
52
|
-
expect(converter.call('1abc')).to eq(1)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "raises error for mixed string in strict mode" do
|
56
|
-
expect {
|
57
|
-
converter.call('1abc', strict: true)
|
58
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
59
|
-
end
|
60
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::NumericConverters::StringToNumericConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new(:string, :numeric) }
|
6
|
-
|
7
|
-
{
|
8
|
-
'1' => 1,
|
9
|
-
'+1' => 1,
|
10
|
-
'-1' => -1,
|
11
|
-
'1e1' => 10.0,
|
12
|
-
'1e-1' => 0.1,
|
13
|
-
'-1e1' => -10.0,
|
14
|
-
'-1e-1' => -0.1,
|
15
|
-
'1.0' => 1.0,
|
16
|
-
'1.0e+1' => 10.0,
|
17
|
-
'1.0e-1' => 0.1,
|
18
|
-
'-1.0e+1' => -10.0,
|
19
|
-
'-1.0e-1' => -0.1,
|
20
|
-
'.1' => 0.1,
|
21
|
-
'.1e+1' => 1.0,
|
22
|
-
'.1e-1' => 0.01,
|
23
|
-
'-.1e+1' => -1.0,
|
24
|
-
'-.1e-1' => -0.01
|
25
|
-
}.each do |actual, expected|
|
26
|
-
it "converts '#{actual}' to '#{expected}'" do
|
27
|
-
expect(converter.call(actual)).to eql(expected)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer::RangeConverters::StringToRangeConverter, '.call' do
|
4
|
-
|
5
|
-
subject(:converter) { described_class.new }
|
6
|
-
|
7
|
-
it "raises error for empty string in strict mode" do
|
8
|
-
expect {
|
9
|
-
converter.call('', strict: true)
|
10
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "returns value in non-strict mode" do
|
14
|
-
expect(converter.call('', strict: false)).to eq('')
|
15
|
-
end
|
16
|
-
|
17
|
-
{
|
18
|
-
'1' => 1..1,
|
19
|
-
'1..10' => 1..10,
|
20
|
-
'1-10' => 1..10,
|
21
|
-
'1,10' => 1..10,
|
22
|
-
'1...10' => 1...10,
|
23
|
-
'-1..10' => -1..10,
|
24
|
-
'1..-10' => 1..-10,
|
25
|
-
'a..z' => 'a'..'z',
|
26
|
-
'a-z' => 'a'..'z',
|
27
|
-
'A-Z' => 'A'..'Z'
|
28
|
-
}.each do |actual, expected|
|
29
|
-
it "converts '#{actual}' to range type" do
|
30
|
-
expect(converter.call(actual)).to eql(expected)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/spec/unit/inspect_spec.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer, '.inspect' do
|
4
|
-
subject(:converter) { described_class.new }
|
5
|
-
|
6
|
-
it "inspects converter instance" do
|
7
|
-
expect(converter.inspect).to eq("#<Necromancer::Context@#{converter.object_id} @config=#{converter.configuration}>")
|
8
|
-
end
|
9
|
-
|
10
|
-
it "inspects conversion target" do
|
11
|
-
conversion = converter.convert(11)
|
12
|
-
expect(conversion.inspect).to eq("#<Necromancer::ConversionTarget@#{conversion.object_id} @object=11, @source=integer>")
|
13
|
-
end
|
14
|
-
end
|
data/spec/unit/new_spec.rb
DELETED
data/spec/unit/register_spec.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Necromancer, '.register' do
|
4
|
-
it "allows ro register converter" do
|
5
|
-
converter = described_class.new
|
6
|
-
UpcaseConverter = Struct.new(:source, :target) do
|
7
|
-
def call(value, **options)
|
8
|
-
value.to_s.upcase
|
9
|
-
end
|
10
|
-
end
|
11
|
-
upcase_converter = UpcaseConverter.new(:string, :upcase)
|
12
|
-
expect(converter.register(upcase_converter)).to eq(true)
|
13
|
-
expect(converter.convert('magic').to(:upcase)).to eq('MAGIC')
|
14
|
-
end
|
15
|
-
end
|