necromancer 0.3.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +70 -3
  3. data/README.md +204 -86
  4. data/lib/necromancer.rb +17 -18
  5. data/lib/necromancer/configuration.rb +1 -1
  6. data/lib/necromancer/context.rb +16 -3
  7. data/lib/necromancer/conversion_target.rb +31 -14
  8. data/lib/necromancer/conversions.rb +39 -16
  9. data/lib/necromancer/converter.rb +10 -8
  10. data/lib/necromancer/converters/array.rb +143 -45
  11. data/lib/necromancer/converters/boolean.rb +21 -19
  12. data/lib/necromancer/converters/date_time.rb +58 -13
  13. data/lib/necromancer/converters/hash.rb +119 -0
  14. data/lib/necromancer/converters/numeric.rb +32 -28
  15. data/lib/necromancer/converters/range.rb +44 -18
  16. data/lib/necromancer/null_converter.rb +4 -2
  17. data/lib/necromancer/version.rb +2 -2
  18. metadata +39 -72
  19. data/.gitignore +0 -14
  20. data/.rspec +0 -3
  21. data/.ruby-version +0 -1
  22. data/.travis.yml +0 -19
  23. data/Gemfile +0 -16
  24. data/Rakefile +0 -8
  25. data/necromancer.gemspec +0 -21
  26. data/spec/spec_helper.rb +0 -53
  27. data/spec/unit/can_spec.rb +0 -11
  28. data/spec/unit/config_spec.rb +0 -32
  29. data/spec/unit/configuration/new_spec.rb +0 -30
  30. data/spec/unit/conversions/register_spec.rb +0 -49
  31. data/spec/unit/convert_spec.rb +0 -104
  32. data/spec/unit/converters/array/array_to_boolean_spec.rb +0 -22
  33. data/spec/unit/converters/array/array_to_numeric_spec.rb +0 -22
  34. data/spec/unit/converters/array/array_to_set_spec.rb +0 -18
  35. data/spec/unit/converters/array/object_to_array_spec.rb +0 -21
  36. data/spec/unit/converters/array/string_to_array_spec.rb +0 -33
  37. data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +0 -26
  38. data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +0 -22
  39. data/spec/unit/converters/boolean/string_to_boolean_spec.rb +0 -36
  40. data/spec/unit/converters/date_time/string_to_date_spec.rb +0 -22
  41. data/spec/unit/converters/date_time/string_to_datetime_spec.rb +0 -32
  42. data/spec/unit/converters/numeric/string_to_float_spec.rb +0 -48
  43. data/spec/unit/converters/numeric/string_to_integer_spec.rb +0 -62
  44. data/spec/unit/converters/numeric/string_to_numeric_spec.rb +0 -32
  45. data/spec/unit/converters/range/string_to_range_spec.rb +0 -35
  46. data/spec/unit/new_spec.rb +0 -12
  47. data/spec/unit/register_spec.rb +0 -17
  48. data/tasks/console.rake +0 -10
  49. data/tasks/coverage.rake +0 -11
  50. data/tasks/spec.rake +0 -29
@@ -1,104 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer, '.convert' do
6
-
7
- subject(:converter) { described_class.new }
8
-
9
- context 'when array' do
10
- it "converts string to array" do
11
- expect(converter.convert("1,2,3").to(:array)).to eq([1,2,3])
12
- end
13
-
14
- it "converts array to numeric " do
15
- expect(converter.convert(['1','2.3','3.0']).to(:numeric)).to eq([1,2.3,3.0])
16
- end
17
-
18
- it "converts array to boolean" do
19
- expect(converter.convert(['t', 'no']).to(:boolean)).to eq([true, false])
20
- end
21
-
22
- it "converts object to array" do
23
- expect(converter.convert({x: 1}).to(:array)).to eq([[:x, 1]])
24
- end
25
-
26
- it "fails to convert in strict mode" do
27
- expect {
28
- converter.convert(['1', '2.3', false]).to(:numeric, strict: true)
29
- }.to raise_error(Necromancer::ConversionTypeError)
30
- end
31
- end
32
-
33
- context 'when numeric' do
34
- it "converts string to integer" do
35
- expect(converter.convert('1').to(:integer)).to eq(1)
36
- end
37
-
38
- it "allows for block for conversion method" do
39
- expect(converter.convert { '1' }.to(:integer)).to eq(1)
40
- end
41
-
42
- it "convers integer to string" do
43
- expect(converter.convert(1).to(:string)).to eq('1')
44
- end
45
-
46
- it "allows for null type conversion" do
47
- expect(converter.convert(1).to(:integer)).to eq(1)
48
- end
49
-
50
- it "raises error when in strict mode" do
51
- expect {
52
- converter.convert('1a').to(:integer, strict: true)
53
- }.to raise_error(Necromancer::ConversionTypeError)
54
- end
55
-
56
- it "doesn't raise error when in non-strict mode" do
57
- expect(converter.convert('1').to(:integer, strict: false)).to eql(1)
58
- end
59
-
60
- it "converts string to float" do
61
- expect(converter.convert('1.0').to(:float)).to eql(1.0)
62
- end
63
-
64
- it "converts string to numeric" do
65
- expect(converter.convert('1.0').to(:numeric)).to eql(1.0)
66
- end
67
- end
68
-
69
- context 'when boolean' do
70
- it "converts boolean to boolean" do
71
- expect(converter.convert(true).to(:boolean)).to eq(true)
72
- end
73
-
74
- it "converts string to boolean" do
75
- expect(converter.convert('yes').to(:boolean)).to eq(true)
76
- end
77
-
78
- it "converts integer to boolean" do
79
- expect(converter.convert(0).to(:boolean)).to eq(false)
80
- end
81
-
82
- it "converts boolean to integer" do
83
- expect(converter.convert(true).to(:integer)).to eq(1)
84
- end
85
- end
86
-
87
- context 'when range' do
88
- it "converts string to range" do
89
- expect(converter.convert('1-10').to(:range)).to eq(1..10)
90
- end
91
- end
92
-
93
- context 'when datetime' do
94
- it "converts string to date" do
95
- expect(converter.convert('2014-12-07').to(:date)).
96
- to eq(Date.parse('2014-12-07'))
97
- end
98
-
99
- it "converts string to datetime" do
100
- expect(converter.convert('2014-12-07 17:35:44').to(:datetime)).
101
- to eq(DateTime.parse('2014-12-07 17:35:44'))
102
- end
103
- end
104
- end
@@ -1,22 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::ArrayConverters::ArrayToBooleanConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:array, :boolean) }
8
-
9
- it "converts `['t', 'f', 'yes', 'no']` to boolean array" do
10
- expect(converter.call(['t', 'f', 'yes', 'no'])).to eq([true, false, true, false])
11
- end
12
-
13
- it "fails to convert `['t', 'no', 5]` to boolean array in strict mode" do
14
- expect {
15
- converter.call(['t', 'no', 5], strict: true)
16
- }.to raise_error(Necromancer::ConversionTypeError)
17
- end
18
-
19
- it "converts `['t', 'no', 5]` to boolean array in non-strict mode" do
20
- expect(converter.call(['t', 'no', 5], strict: false)).to eql([true, false, 5])
21
- end
22
- end
@@ -1,22 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::ArrayConverters::ArrayToNumericConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:array, :numeric) }
8
-
9
- it "converts `['1','2.3','3.0']` to numeric array" do
10
- expect(converter.call(['1', '2.3', '3.0'])).to eq([1, 2.3, 3.0])
11
- end
12
-
13
- it "fails to convert `['1','2.3',false]` to numeric array in strict mode" do
14
- expect {
15
- converter.call(['1', '2.3', false], strict: true)
16
- }.to raise_error(Necromancer::ConversionTypeError)
17
- end
18
-
19
- it "converts `['1','2.3',false]` to numeric array in non-strict mode" do
20
- expect(converter.call(['1', '2.3', false], strict: false)).to eq([1, 2.3, false])
21
- end
22
- end
@@ -1,18 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::ArrayConverters::ArrayToSetConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:array, :set) }
8
-
9
- it "converts `[:x,:y,:x,1,2,1]` to set" do
10
- expect(converter.call([:x,:y,:x,1,2,1])).to eql(Set[:x,:y,1,2])
11
- end
12
-
13
- it "fails to convert `1` to set" do
14
- expect {
15
- converter.call(1, strict: true)
16
- }.to raise_error(Necromancer::ConversionTypeError)
17
- end
18
- end
@@ -1,21 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::ArrayConverters::ObjectToArrayConverter, '.call' do
6
- subject(:converter) { described_class.new(:object, :array) }
7
-
8
- it "converts nil to array" do
9
- expect(converter.call(nil)).to eq([])
10
- end
11
-
12
- it "converts custom object to array" do
13
- Custom = Class.new do
14
- def to_ary
15
- [:x, :y]
16
- end
17
- end
18
- custom = Custom.new
19
- expect(converter.call(custom)).to eq([:x, :y])
20
- end
21
- end
@@ -1,33 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::ArrayConverters::StringToArrayConverter, '.call' do
6
- subject(:converter) { described_class.new(:string, :array) }
7
-
8
- it "converts empty string to array" do
9
- expect(converter.call('', strict: false)).to eq([''])
10
- end
11
-
12
- it "fails to convert empty string to array in strict mode" do
13
- expect {
14
- converter.call('', strict: true)
15
- }.to raise_error(Necromancer::ConversionTypeError)
16
- end
17
-
18
- it "converts `1,2,3` to array" do
19
- expect(converter.call('1,2,3')).to eq([1,2,3])
20
- end
21
-
22
- it "converts `a,b,c` to array" do
23
- expect(converter.call('a,b,c')).to eq(['a','b','c'])
24
- end
25
-
26
- it "converts '1-2-3' to array" do
27
- expect(converter.call('1-2-3')).to eq([1,2,3])
28
- end
29
-
30
- it "converts ' 1 - 2 - 3 ' to array" do
31
- expect(converter.call(' 1 - 2 - 3 ')).to eq([1,2,3])
32
- end
33
- end
@@ -1,26 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::BooleanConverters::BooleanToIntegerConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new }
8
-
9
- it "converts true to 1 value" do
10
- expect(converter.call(true)).to eq(1)
11
- end
12
-
13
- it "converts false to 0 value" do
14
- expect(converter.call(false)).to eq(0)
15
- end
16
-
17
- it "fails to convert in strict mode" do
18
- expect {
19
- converter.call('unknown', strict: true)
20
- }.to raise_error(Necromancer::ConversionTypeError)
21
- end
22
-
23
- it "returns value in non-strict mode" do
24
- expect(converter.call('unknown', strict: false)).to eq('unknown')
25
- end
26
- end
@@ -1,22 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::BooleanConverters::IntegerToBooleanConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new }
8
-
9
- it "converts 1 to true value" do
10
- expect(converter.call(1)).to eq(true)
11
- end
12
-
13
- it "converts 0 to false value" do
14
- expect(converter.call(0)).to eq(false)
15
- end
16
-
17
- it "fails to convert in strict mode" do
18
- expect {
19
- converter.call('1', strict: true)
20
- }.to raise_error(Necromancer::ConversionTypeError)
21
- end
22
- end
@@ -1,36 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::BooleanConverters::StringToBooleanConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:string, :boolean) }
8
-
9
- it "raises error for empty string strict mode" do
10
- expect {
11
- converter.call('', strict: true)
12
- }.to raise_error(Necromancer::ConversionTypeError)
13
- end
14
-
15
- it "fails to convert unkonwn value FOO" do
16
- expect {
17
- converter.call('FOO', strict: true)
18
- }.to raise_error(Necromancer::ConversionTypeError)
19
- end
20
-
21
- it "passes through boolean value" do
22
- expect(converter.call(true)).to eq(true)
23
- end
24
-
25
- %w[true TRUE t T 1 y Y YES yes on ON].each do |value|
26
- it "converts '#{value}' to true value" do
27
- expect(converter.call(value)).to eq(true)
28
- end
29
- end
30
-
31
- %w[false FALSE f F 0 n N NO No no off OFF].each do |value|
32
- it "converts '#{value}' to false value" do
33
- expect(converter.call(value)).to eq(false)
34
- end
35
- end
36
- end
@@ -1,22 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::DateTimeConverters::StringToDateConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:string, :date) }
8
-
9
- it "converts '2014/12/07' to date value" do
10
- expect(converter.call('2014/12/07')).to eq(Date.parse('2014/12/07'))
11
- end
12
-
13
- it "converts '2014-12-07' to date value" do
14
- expect(converter.call('2014-12-07')).to eq(Date.parse('2014/12/07'))
15
- end
16
-
17
- it "fails to convert in strict mode" do
18
- expect {
19
- converter.call('2014 - 12 - 07', strict: true)
20
- }.to raise_error(Necromancer::ConversionTypeError)
21
- end
22
- end
@@ -1,32 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::DateTimeConverters::StringToDateTimeConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:string, :datetime) }
8
-
9
- it "converts '2014/12/07' to date value" do
10
- expect(converter.call('2014/12/07')).to eq(DateTime.parse('2014/12/07'))
11
- end
12
-
13
- it "converts '2014-12-07' to date value" do
14
- expect(converter.call('2014-12-07')).to eq(DateTime.parse('2014-12-07'))
15
- end
16
-
17
- it "converts '7th December 2014' to datetime value" do
18
- expect(converter.call('7th December 2014')).
19
- to eq(DateTime.parse('2014-12-07'))
20
- end
21
-
22
- it "converts '7th December 2014 17:19:44' to datetime value" do
23
- expect(converter.call('7th December 2014 17:19:44')).
24
- to eq(DateTime.parse('2014-12-07 17:19:44'))
25
- end
26
-
27
- it "fails to convert in strict mode" do
28
- expect {
29
- converter.call('2014 - 12 - 07', strict: true)
30
- }.to raise_error(Necromancer::ConversionTypeError)
31
- end
32
- end
@@ -1,48 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::NumericConverters::StringToFloatConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:string, :float) }
8
-
9
- it "raises error for empty string in strict mode" do
10
- expect {
11
- converter.call('', strict: true)
12
- }.to raise_error(Necromancer::ConversionTypeError)
13
- end
14
-
15
- {
16
- '1' => 1.0,
17
- '+1' => 1.0,
18
- '-1' => -1.0,
19
- '1e1' => 10.0,
20
- '1e-1' => 0.1,
21
- '-1e1' => -10.0,
22
- '-1e-1' => -0.1,
23
- '1.0' => 1.0,
24
- '1.0e+1' => 10.0,
25
- '1.0e-1' => 0.1,
26
- '-1.0e+1' => -10.0,
27
- '-1.0e-1' => -0.1,
28
- '.1' => 0.1,
29
- '.1e+1' => 1.0,
30
- '.1e-1' => 0.01,
31
- '-.1e+1' => -1.0,
32
- '-.1e-1' => -0.01
33
- }.each do |actual, expected|
34
- it "converts '#{actual}' to float value" do
35
- expect(converter.call(actual)).to eql(expected)
36
- end
37
- end
38
-
39
- it "failse to convert '1.2a' in strict mode" do
40
- expect {
41
- converter.call('1.2a', strict: true)
42
- }.to raise_error(Necromancer::ConversionTypeError)
43
- end
44
-
45
- it "converts '1.2a' in non-strict mode" do
46
- expect(converter.call('1.2a', strict: false)).to eq(1.2)
47
- end
48
- end
@@ -1,62 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Necromancer::NumericConverters::StringToIntegerConverter, '.call' do
6
-
7
- subject(:converter) { described_class.new(:string, :integer) }
8
-
9
- {
10
- '1' => 1,
11
- '+1' => 1,
12
- '-1' => -1,
13
- '1e+1' => 1,
14
- '+1e-1' => 1,
15
- '-1e1' => -1,
16
- '-1e-1' => -1,
17
- '1.0' => 1,
18
- '1.0e+1' => 1,
19
- '1.0e-1' => 1,
20
- '-1.0e+1' => -1,
21
- '-1.0e-1' => -1,
22
- '.1' => 0,
23
- '.1e+1' => 0,
24
- '.1e-1' => 0,
25
- '-.1e+1' => 0,
26
- '-.1e-1' => 0
27
- }.each do |actual, expected|
28
- it "converts '#{actual}' to float value" do
29
- expect(converter.call(actual)).to eql(expected)
30
- end
31
- end
32
-
33
- it "raises error for empty string in strict mode" do
34
- expect {
35
- converter.call('', strict: true)
36
- }.to raise_error(Necromancer::ConversionTypeError)
37
- end
38
-
39
- it "converts empty string to 0 in non-strict mode" do
40
- expect(converter.call('', strict: false)).to eq(0)
41
- end
42
-
43
- it "raises error for float in strict mode" do
44
- expect {
45
- converter.call('1.2', strict: true)
46
- }.to raise_error(Necromancer::ConversionTypeError)
47
- end
48
-
49
- it "converts float to integer in non-strict mode" do
50
- expect(converter.call(1.2)).to eq(1)
51
- end
52
-
53
- it "converts mixed string to integer in non-strict mode" do
54
- expect(converter.call('1abc')).to eq(1)
55
- end
56
-
57
- it "raises error for mixed string in strict mode" do
58
- expect {
59
- converter.call('1abc', strict: true)
60
- }.to raise_error(Necromancer::ConversionTypeError)
61
- end
62
- end