necromancer 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +14 -6
  3. data/CHANGELOG.md +35 -3
  4. data/CODE_OF_CONDUCT.md +49 -0
  5. data/Gemfile +3 -4
  6. data/README.md +116 -61
  7. data/lib/necromancer.rb +16 -18
  8. data/lib/necromancer/context.rb +14 -1
  9. data/lib/necromancer/conversion_target.rb +27 -10
  10. data/lib/necromancer/conversions.rb +33 -13
  11. data/lib/necromancer/converter.rb +3 -1
  12. data/lib/necromancer/converters/array.rb +5 -1
  13. data/lib/necromancer/converters/boolean.rb +4 -1
  14. data/lib/necromancer/converters/date_time.rb +45 -1
  15. data/lib/necromancer/converters/numeric.rb +4 -1
  16. data/lib/necromancer/converters/range.rb +4 -1
  17. data/lib/necromancer/null_converter.rb +3 -1
  18. data/lib/necromancer/version.rb +2 -2
  19. data/necromancer.gemspec +3 -1
  20. data/spec/unit/can_spec.rb +1 -3
  21. data/spec/unit/config_spec.rb +1 -3
  22. data/spec/unit/configuration/new_spec.rb +1 -3
  23. data/spec/unit/conversions/fetch_spec.rb +16 -0
  24. data/spec/unit/conversions/register_spec.rb +12 -3
  25. data/spec/unit/conversions/to_hash_spec.rb +37 -0
  26. data/spec/unit/convert_spec.rb +29 -3
  27. data/spec/unit/converters/array/array_to_boolean_spec.rb +1 -3
  28. data/spec/unit/converters/array/array_to_numeric_spec.rb +1 -3
  29. data/spec/unit/converters/array/array_to_set_spec.rb +1 -3
  30. data/spec/unit/converters/array/object_to_array_spec.rb +1 -3
  31. data/spec/unit/converters/array/string_to_array_spec.rb +1 -3
  32. data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +1 -3
  33. data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +1 -3
  34. data/spec/unit/converters/boolean/string_to_boolean_spec.rb +1 -3
  35. data/spec/unit/converters/date_time/string_to_date_spec.rb +5 -3
  36. data/spec/unit/converters/date_time/string_to_datetime_spec.rb +1 -3
  37. data/spec/unit/converters/date_time/string_to_time_spec.rb +28 -0
  38. data/spec/unit/converters/numeric/string_to_float_spec.rb +1 -3
  39. data/spec/unit/converters/numeric/string_to_integer_spec.rb +1 -3
  40. data/spec/unit/converters/numeric/string_to_numeric_spec.rb +1 -3
  41. data/spec/unit/converters/range/string_to_range_spec.rb +1 -3
  42. data/spec/unit/inspect_spec.rb +14 -0
  43. data/spec/unit/new_spec.rb +1 -3
  44. data/spec/unit/register_spec.rb +1 -3
  45. metadata +48 -12
  46. data/.ruby-version +0 -1
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe Necromancer::Conversions, '#to_hash' do
4
+ it 'exports default conversions to hash' do
5
+ conversions = Necromancer::Conversions.new
6
+ expect(conversions.to_hash).to eq({})
7
+
8
+ conversions.load
9
+
10
+ expect(conversions.to_hash.keys.sort).to eq([
11
+ 'array->array',
12
+ 'array->boolean',
13
+ 'array->numeric',
14
+ 'boolean->boolean',
15
+ 'boolean->integer',
16
+ 'date->date',
17
+ 'datetime->datetime',
18
+ 'float->float',
19
+ 'hash->array',
20
+ 'integer->boolean',
21
+ 'integer->integer',
22
+ 'integer->string',
23
+ 'object->array',
24
+ 'range->range',
25
+ 'string->array',
26
+ 'string->boolean',
27
+ 'string->date',
28
+ 'string->datetime',
29
+ 'string->float',
30
+ 'string->integer',
31
+ 'string->numeric',
32
+ 'string->range',
33
+ 'string->time',
34
+ 'time->time'
35
+ ])
36
+ end
37
+ end
@@ -1,11 +1,32 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer, '.convert' do
6
4
 
7
5
  subject(:converter) { described_class.new }
8
6
 
7
+ it "indicates inability to perform the requested conversion" do
8
+ expect {
9
+ converter.convert(:foo).to(:float)
10
+ }.to raise_error(Necromancer::NoTypeConversionAvailableError,
11
+ /Conversion 'symbol->float' unavailable/)
12
+ end
13
+
14
+ it "allows for module level convert call" do
15
+ expect(Necromancer.convert('1,2,3').to(:array)).to eq([1,2,3])
16
+ end
17
+
18
+ it "allows replacing #to with #>> call" do
19
+ expect(converter.convert('1,2,3') >> :array).to eq([1,2,3])
20
+ end
21
+
22
+ it "allows to specify object as conversion target" do
23
+ expect(converter.convert('1,2,3') >> []).to eq([1,2,3])
24
+ end
25
+
26
+ it "allows to specify class as conversion target" do
27
+ expect(converter.convert('1,2,3') >> Array).to eq([1,2,3])
28
+ end
29
+
9
30
  context 'when array' do
10
31
  it "converts string to array" do
11
32
  expect(converter.convert("1,2,3").to(:array)).to eq([1,2,3])
@@ -100,5 +121,10 @@ RSpec.describe Necromancer, '.convert' do
100
121
  expect(converter.convert('2014-12-07 17:35:44').to(:datetime)).
101
122
  to eq(DateTime.parse('2014-12-07 17:35:44'))
102
123
  end
124
+
125
+ it "converts string to time" do
126
+ expect(converter.convert('12:30').to(:time)).
127
+ to eq(Time.parse('12:30'))
128
+ end
103
129
  end
104
130
  end
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::ArrayConverters::ArrayToBooleanConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::ArrayConverters::ArrayToNumericConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::ArrayConverters::ArrayToSetConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::ArrayConverters::ObjectToArrayConverter, '.call' do
6
4
  subject(:converter) { described_class.new(:object, :array) }
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::ArrayConverters::StringToArrayConverter, '.call' do
6
4
  subject(:converter) { described_class.new(:string, :array) }
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::BooleanConverters::BooleanToIntegerConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::BooleanConverters::IntegerToBooleanConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::BooleanConverters::StringToBooleanConverter, '.call' do
6
4
 
@@ -1,11 +1,13 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::DateTimeConverters::StringToDateConverter, '.call' do
6
4
 
7
5
  subject(:converter) { described_class.new(:string, :date) }
8
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
+
9
11
  it "converts '2014/12/07' to date value" do
10
12
  expect(converter.call('2014/12/07')).to eq(Date.parse('2014/12/07'))
11
13
  end
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::DateTimeConverters::StringToDateTimeConverter, '.call' do
6
4
 
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
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,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::NumericConverters::StringToFloatConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::NumericConverters::StringToIntegerConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::NumericConverters::StringToNumericConverter, '.call' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer::RangeConverters::StringToRangeConverter, '.call' do
6
4
 
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
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
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer, '#new' do
6
4
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
1
+ # encoding: utf-8
4
2
 
5
3
  RSpec.describe Necromancer, '.register' do
6
4
  it "allows ro register converter" do
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: necromancer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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-12-14 00:00:00.000000000 Z
11
+ date: 2017-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.5.0
27
55
  description: Conversion from one object type to another with a bit of black magic.
28
56
  email:
29
57
  - ''
@@ -31,11 +59,11 @@ executables: []
31
59
  extensions: []
32
60
  extra_rdoc_files: []
33
61
  files:
34
- - .gitignore
35
- - .rspec
36
- - .ruby-version
37
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
38
65
  - CHANGELOG.md
66
+ - CODE_OF_CONDUCT.md
39
67
  - Gemfile
40
68
  - LICENSE.txt
41
69
  - README.md
@@ -58,7 +86,9 @@ files:
58
86
  - spec/unit/can_spec.rb
59
87
  - spec/unit/config_spec.rb
60
88
  - spec/unit/configuration/new_spec.rb
89
+ - spec/unit/conversions/fetch_spec.rb
61
90
  - spec/unit/conversions/register_spec.rb
91
+ - spec/unit/conversions/to_hash_spec.rb
62
92
  - spec/unit/convert_spec.rb
63
93
  - spec/unit/converters/array/array_to_boolean_spec.rb
64
94
  - spec/unit/converters/array/array_to_numeric_spec.rb
@@ -70,16 +100,18 @@ files:
70
100
  - spec/unit/converters/boolean/string_to_boolean_spec.rb
71
101
  - spec/unit/converters/date_time/string_to_date_spec.rb
72
102
  - spec/unit/converters/date_time/string_to_datetime_spec.rb
103
+ - spec/unit/converters/date_time/string_to_time_spec.rb
73
104
  - spec/unit/converters/numeric/string_to_float_spec.rb
74
105
  - spec/unit/converters/numeric/string_to_integer_spec.rb
75
106
  - spec/unit/converters/numeric/string_to_numeric_spec.rb
76
107
  - spec/unit/converters/range/string_to_range_spec.rb
108
+ - spec/unit/inspect_spec.rb
77
109
  - spec/unit/new_spec.rb
78
110
  - spec/unit/register_spec.rb
79
111
  - tasks/console.rake
80
112
  - tasks/coverage.rake
81
113
  - tasks/spec.rake
82
- homepage: https://github.com/peter-murach/necromancer
114
+ homepage: https://github.com/piotrmurach/necromancer
83
115
  licenses:
84
116
  - MIT
85
117
  metadata: {}
@@ -89,17 +121,17 @@ require_paths:
89
121
  - lib
90
122
  required_ruby_version: !ruby/object:Gem::Requirement
91
123
  requirements:
92
- - - '>='
124
+ - - ">="
93
125
  - !ruby/object:Gem::Version
94
126
  version: '0'
95
127
  required_rubygems_version: !ruby/object:Gem::Requirement
96
128
  requirements:
97
- - - '>='
129
+ - - ">="
98
130
  - !ruby/object:Gem::Version
99
131
  version: '0'
100
132
  requirements: []
101
133
  rubyforge_project:
102
- rubygems_version: 2.0.3
134
+ rubygems_version: 2.5.1
103
135
  signing_key:
104
136
  specification_version: 4
105
137
  summary: Conversion from one object type to another with a bit of black magic.
@@ -108,7 +140,9 @@ test_files:
108
140
  - spec/unit/can_spec.rb
109
141
  - spec/unit/config_spec.rb
110
142
  - spec/unit/configuration/new_spec.rb
143
+ - spec/unit/conversions/fetch_spec.rb
111
144
  - spec/unit/conversions/register_spec.rb
145
+ - spec/unit/conversions/to_hash_spec.rb
112
146
  - spec/unit/convert_spec.rb
113
147
  - spec/unit/converters/array/array_to_boolean_spec.rb
114
148
  - spec/unit/converters/array/array_to_numeric_spec.rb
@@ -120,10 +154,12 @@ test_files:
120
154
  - spec/unit/converters/boolean/string_to_boolean_spec.rb
121
155
  - spec/unit/converters/date_time/string_to_date_spec.rb
122
156
  - spec/unit/converters/date_time/string_to_datetime_spec.rb
157
+ - spec/unit/converters/date_time/string_to_time_spec.rb
123
158
  - spec/unit/converters/numeric/string_to_float_spec.rb
124
159
  - spec/unit/converters/numeric/string_to_integer_spec.rb
125
160
  - spec/unit/converters/numeric/string_to_numeric_spec.rb
126
161
  - spec/unit/converters/range/string_to_range_spec.rb
162
+ - spec/unit/inspect_spec.rb
127
163
  - spec/unit/new_spec.rb
128
164
  - spec/unit/register_spec.rb
129
165
  has_rdoc:
@@ -1 +0,0 @@
1
- 2.0.0