necromancer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +229 -19
- data/lib/necromancer.rb +13 -2
- data/lib/necromancer/context.rb +4 -0
- data/lib/necromancer/conversions.rb +2 -1
- data/lib/necromancer/converter.rb +11 -0
- data/lib/necromancer/converters/array.rb +29 -21
- data/lib/necromancer/converters/boolean.rb +40 -10
- data/lib/necromancer/converters/date_time.rb +33 -0
- data/lib/necromancer/converters/numeric.rb +88 -0
- data/lib/necromancer/converters/range.rb +13 -6
- data/lib/necromancer/version.rb +1 -1
- data/spec/spec_helper.rb +8 -0
- data/spec/unit/convert_spec.rb +31 -1
- data/spec/unit/converters/array/string_to_array_spec.rb +19 -1
- data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +10 -0
- data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +6 -0
- data/spec/unit/converters/boolean/string_to_boolean_spec.rb +19 -79
- data/spec/unit/converters/date_time/string_to_date_spec.rb +22 -0
- data/spec/unit/converters/date_time/string_to_datetime_spec.rb +32 -0
- data/spec/unit/converters/numeric/string_to_float_spec.rb +48 -0
- data/spec/unit/converters/{integer → numeric}/string_to_integer_spec.rb +28 -4
- data/spec/unit/converters/numeric/string_to_numeric_spec.rb +32 -0
- data/spec/unit/converters/range/string_to_range_spec.rb +25 -42
- data/spec/unit/register_spec.rb +17 -0
- metadata +17 -8
- data/lib/necromancer/converters/float.rb +0 -28
- data/lib/necromancer/converters/integer.rb +0 -48
- data/spec/unit/converters/float/string_to_float_spec.rb +0 -28
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,32 @@
|
|
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
|
@@ -0,0 +1,48 @@
|
|
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
|
@@ -2,9 +2,33 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
RSpec.describe Necromancer::
|
6
|
-
|
7
|
-
subject(:converter) { described_class.new }
|
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
|
8
32
|
|
9
33
|
it "raises error for empty string in strict mode" do
|
10
34
|
expect {
|
@@ -18,7 +42,7 @@ RSpec.describe Necromancer::IntegerConverters::StringToIntegerConverter, '.call'
|
|
18
42
|
|
19
43
|
it "raises error for float in strict mode" do
|
20
44
|
expect {
|
21
|
-
converter.call(1.2, strict: true)
|
45
|
+
converter.call('1.2', strict: true)
|
22
46
|
}.to raise_error(Necromancer::ConversionTypeError)
|
23
47
|
end
|
24
48
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Necromancer::NumericConverters::StringToNumericConverter, '.call' do
|
6
|
+
|
7
|
+
subject(:converter) { described_class.new(:string, :numeric) }
|
8
|
+
|
9
|
+
{
|
10
|
+
'1' => 1,
|
11
|
+
'+1' => 1,
|
12
|
+
'-1' => -1,
|
13
|
+
'1e1' => 10.0,
|
14
|
+
'1e-1' => 0.1,
|
15
|
+
'-1e1' => -10.0,
|
16
|
+
'-1e-1' => -0.1,
|
17
|
+
'1.0' => 1.0,
|
18
|
+
'1.0e+1' => 10.0,
|
19
|
+
'1.0e-1' => 0.1,
|
20
|
+
'-1.0e+1' => -10.0,
|
21
|
+
'-1.0e-1' => -0.1,
|
22
|
+
'.1' => 0.1,
|
23
|
+
'.1e+1' => 1.0,
|
24
|
+
'.1e-1' => 0.01,
|
25
|
+
'-.1e+1' => -1.0,
|
26
|
+
'-.1e-1' => -0.01
|
27
|
+
}.each do |actual, expected|
|
28
|
+
it "converts '#{actual}' to '#{expected}'" do
|
29
|
+
expect(converter.call(actual)).to eql(expected)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -6,47 +6,30 @@ RSpec.describe Necromancer::RangeConverters::StringToRangeConverter, '.call' do
|
|
6
6
|
|
7
7
|
subject(:converter) { described_class.new }
|
8
8
|
|
9
|
-
it "raises error for empty string" do
|
10
|
-
expect {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
expect(converter.call('-1..10')).to eq(-1..10)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "converts '1..-10' to range value" do
|
38
|
-
expect(converter.call('1..-10')).to eq(1..-10)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "converts 'a..z' to range value" do
|
42
|
-
expect(converter.call('a..z')).to eq('a'..'z')
|
43
|
-
end
|
44
|
-
|
45
|
-
it "converts 'a-z' to range value" do
|
46
|
-
expect(converter.call('a-z')).to eq('a'..'z')
|
47
|
-
end
|
48
|
-
|
49
|
-
it "converts 'A-Z' to range value" do
|
50
|
-
expect(converter.call('A-Z')).to eq('A'..'Z')
|
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
|
+
it "returns value in non-strict mode" do
|
16
|
+
expect(converter.call('', strict: false)).to eq('')
|
17
|
+
end
|
18
|
+
|
19
|
+
{
|
20
|
+
'1' => 1..1,
|
21
|
+
'1..10' => 1..10,
|
22
|
+
'1-10' => 1..10,
|
23
|
+
'1,10' => 1..10,
|
24
|
+
'1...10' => 1...10,
|
25
|
+
'-1..10' => -1..10,
|
26
|
+
'1..-10' => 1..-10,
|
27
|
+
'a..z' => 'a'..'z',
|
28
|
+
'a-z' => 'a'..'z',
|
29
|
+
'A-Z' => 'A'..'Z'
|
30
|
+
}.each do |actual, expected|
|
31
|
+
it "converts '#{actual}' to range type" do
|
32
|
+
expect(converter.call(actual)).to eql(expected)
|
33
|
+
end
|
51
34
|
end
|
52
35
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Necromancer, '.register' do
|
6
|
+
it "allows ro register converter" do
|
7
|
+
converter = described_class.new
|
8
|
+
UpcaseConverter = Struct.new(:source, :target) do
|
9
|
+
def call(value, options)
|
10
|
+
value.to_s.upcase
|
11
|
+
end
|
12
|
+
end
|
13
|
+
upcase_converter = UpcaseConverter.new(:string, :upcase)
|
14
|
+
expect(converter.register(upcase_converter)).to eq(true)
|
15
|
+
expect(converter.convert('magic').to(:upcase)).to eq('MAGIC')
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: necromancer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- .rspec
|
36
36
|
- .ruby-version
|
37
37
|
- .travis.yml
|
38
|
+
- CHANGELOG.md
|
38
39
|
- Gemfile
|
39
40
|
- LICENSE.txt
|
40
41
|
- README.md
|
@@ -46,8 +47,8 @@ files:
|
|
46
47
|
- lib/necromancer/converter.rb
|
47
48
|
- lib/necromancer/converters/array.rb
|
48
49
|
- lib/necromancer/converters/boolean.rb
|
49
|
-
- lib/necromancer/converters/
|
50
|
-
- lib/necromancer/converters/
|
50
|
+
- lib/necromancer/converters/date_time.rb
|
51
|
+
- lib/necromancer/converters/numeric.rb
|
51
52
|
- lib/necromancer/converters/range.rb
|
52
53
|
- lib/necromancer/null_converter.rb
|
53
54
|
- lib/necromancer/version.rb
|
@@ -61,10 +62,14 @@ files:
|
|
61
62
|
- spec/unit/converters/boolean/boolean_to_integer_spec.rb
|
62
63
|
- spec/unit/converters/boolean/integer_to_boolean_spec.rb
|
63
64
|
- spec/unit/converters/boolean/string_to_boolean_spec.rb
|
64
|
-
- spec/unit/converters/
|
65
|
-
- spec/unit/converters/
|
65
|
+
- spec/unit/converters/date_time/string_to_date_spec.rb
|
66
|
+
- spec/unit/converters/date_time/string_to_datetime_spec.rb
|
67
|
+
- spec/unit/converters/numeric/string_to_float_spec.rb
|
68
|
+
- spec/unit/converters/numeric/string_to_integer_spec.rb
|
69
|
+
- spec/unit/converters/numeric/string_to_numeric_spec.rb
|
66
70
|
- spec/unit/converters/range/string_to_range_spec.rb
|
67
71
|
- spec/unit/new_spec.rb
|
72
|
+
- spec/unit/register_spec.rb
|
68
73
|
- tasks/console.rake
|
69
74
|
- tasks/coverage.rake
|
70
75
|
- tasks/spec.rake
|
@@ -102,8 +107,12 @@ test_files:
|
|
102
107
|
- spec/unit/converters/boolean/boolean_to_integer_spec.rb
|
103
108
|
- spec/unit/converters/boolean/integer_to_boolean_spec.rb
|
104
109
|
- spec/unit/converters/boolean/string_to_boolean_spec.rb
|
105
|
-
- spec/unit/converters/
|
106
|
-
- spec/unit/converters/
|
110
|
+
- spec/unit/converters/date_time/string_to_date_spec.rb
|
111
|
+
- spec/unit/converters/date_time/string_to_datetime_spec.rb
|
112
|
+
- spec/unit/converters/numeric/string_to_float_spec.rb
|
113
|
+
- spec/unit/converters/numeric/string_to_integer_spec.rb
|
114
|
+
- spec/unit/converters/numeric/string_to_numeric_spec.rb
|
107
115
|
- spec/unit/converters/range/string_to_range_spec.rb
|
108
116
|
- spec/unit/new_spec.rb
|
117
|
+
- spec/unit/register_spec.rb
|
109
118
|
has_rdoc:
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Necromancer
|
4
|
-
module FloatConverters
|
5
|
-
class StringToFloatConverter < Converter
|
6
|
-
# Convert string to float value
|
7
|
-
#
|
8
|
-
# @example
|
9
|
-
# converter.call('1.2') # => 1.2
|
10
|
-
#
|
11
|
-
# @api public
|
12
|
-
def call(value, options = {})
|
13
|
-
strict = options.fetch(:strict, false)
|
14
|
-
Float(value.to_s)
|
15
|
-
rescue
|
16
|
-
if strict
|
17
|
-
raise ConversionTypeError, "#{value} could not be converted from " \
|
18
|
-
"`#{source}` into `#{target}`"
|
19
|
-
else
|
20
|
-
value.to_f
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.load(conversions)
|
26
|
-
end
|
27
|
-
end # Conversion
|
28
|
-
end # Necromancer
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Necromancer
|
4
|
-
# Container for Integer converter classes
|
5
|
-
module IntegerConverters
|
6
|
-
# An object that converts a String to an Integer
|
7
|
-
class StringToIntegerConverter < Converter
|
8
|
-
# Convert string value to integer
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# converter.call('1abc') # => 1
|
12
|
-
#
|
13
|
-
# @api public
|
14
|
-
def call(value, options = {})
|
15
|
-
strict = options.fetch(:strict, false)
|
16
|
-
Integer(value.to_s)
|
17
|
-
rescue
|
18
|
-
if strict
|
19
|
-
raise ConversionTypeError, "#{value} could not be converted from " \
|
20
|
-
"`#{source}` into `#{target}`"
|
21
|
-
else
|
22
|
-
value.to_i
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# An object that converts an Integer to a String
|
28
|
-
class IntegerToStringConverter < Converter
|
29
|
-
# Convert integer value to string
|
30
|
-
#
|
31
|
-
# @example
|
32
|
-
# converter.call(1) # => '1'
|
33
|
-
#
|
34
|
-
# @api public
|
35
|
-
def call(value, _)
|
36
|
-
value.to_s
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.load(conversions)
|
41
|
-
conversions.register StringToIntegerConverter.new(:string, :integer)
|
42
|
-
conversions.register IntegerToStringConverter.new(:integer, :string)
|
43
|
-
conversions.register IntegerToStringConverter.new(:fixnum, :string)
|
44
|
-
conversions.register NullConverter.new(:integer, :integer)
|
45
|
-
conversions.register NullConverter.new(:fixnum, :integer)
|
46
|
-
end
|
47
|
-
end # IntegerConverters
|
48
|
-
end # Necromancer
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
RSpec.describe Necromancer::FloatConverters::StringToFloatConverter, '.call' do
|
6
|
-
|
7
|
-
subject(:converter) { described_class.new }
|
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
|
-
it "converts '1' to float value" do
|
16
|
-
expect(converter.call('1')).to eq(1.0)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "converts '1.2a' to float value in non-strict mode" do
|
20
|
-
expect(converter.call('1.2')).to eq(1.2)
|
21
|
-
end
|
22
|
-
|
23
|
-
it "failse to convert '1.2a' in strict mode" do
|
24
|
-
expect {
|
25
|
-
converter.call('1.2a', strict: true)
|
26
|
-
}.to raise_error(Necromancer::ConversionTypeError)
|
27
|
-
end
|
28
|
-
end
|