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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/lib/necromancer.rb +4 -3
  4. data/lib/necromancer/context.rb +4 -4
  5. data/lib/necromancer/conversion_target.rb +5 -5
  6. data/lib/necromancer/conversions.rb +10 -10
  7. data/lib/necromancer/converter.rb +1 -1
  8. data/lib/necromancer/converters/array.rb +6 -6
  9. data/lib/necromancer/converters/boolean.rb +2 -2
  10. data/lib/necromancer/converters/date_time.rb +4 -4
  11. data/lib/necromancer/converters/numeric.rb +7 -7
  12. data/lib/necromancer/converters/range.rb +6 -6
  13. data/lib/necromancer/null_converter.rb +1 -1
  14. data/lib/necromancer/version.rb +1 -1
  15. metadata +9 -77
  16. data/Rakefile +0 -8
  17. data/necromancer.gemspec +0 -34
  18. data/spec/spec_helper.rb +0 -53
  19. data/spec/unit/can_spec.rb +0 -9
  20. data/spec/unit/config_spec.rb +0 -30
  21. data/spec/unit/configuration/new_spec.rb +0 -28
  22. data/spec/unit/conversions/fetch_spec.rb +0 -16
  23. data/spec/unit/conversions/register_spec.rb +0 -58
  24. data/spec/unit/conversions/to_hash_spec.rb +0 -37
  25. data/spec/unit/convert_spec.rb +0 -130
  26. data/spec/unit/converters/array/array_to_boolean_spec.rb +0 -20
  27. data/spec/unit/converters/array/array_to_numeric_spec.rb +0 -20
  28. data/spec/unit/converters/array/array_to_set_spec.rb +0 -16
  29. data/spec/unit/converters/array/object_to_array_spec.rb +0 -19
  30. data/spec/unit/converters/array/string_to_array_spec.rb +0 -31
  31. data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +0 -24
  32. data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +0 -20
  33. data/spec/unit/converters/boolean/string_to_boolean_spec.rb +0 -34
  34. data/spec/unit/converters/date_time/string_to_date_spec.rb +0 -24
  35. data/spec/unit/converters/date_time/string_to_datetime_spec.rb +0 -30
  36. data/spec/unit/converters/date_time/string_to_time_spec.rb +0 -28
  37. data/spec/unit/converters/numeric/string_to_float_spec.rb +0 -46
  38. data/spec/unit/converters/numeric/string_to_integer_spec.rb +0 -60
  39. data/spec/unit/converters/numeric/string_to_numeric_spec.rb +0 -30
  40. data/spec/unit/converters/range/string_to_range_spec.rb +0 -33
  41. data/spec/unit/inspect_spec.rb +0 -14
  42. data/spec/unit/new_spec.rb +0 -10
  43. data/spec/unit/register_spec.rb +0 -15
  44. data/tasks/console.rake +0 -11
  45. data/tasks/coverage.rake +0 -11
  46. data/tasks/spec.rake +0 -29
@@ -1,34 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'necromancer/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'necromancer'
7
- spec.version = Necromancer::VERSION
8
- spec.authors = ['Piotr Murach']
9
- spec.email = ['me@piotrmurach.com']
10
- spec.summary = %q{Conversion from one object type to another with a bit of black magic.}
11
- spec.description = %q{Conversion from one object type to another with a bit of black magic.}
12
- spec.homepage = 'https://github.com/piotrmurach/necromancer'
13
- spec.license = 'MIT'
14
-
15
- if spec.respond_to?(:metadata=)
16
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
- spec.metadata["changelog_uri"] = "https://github.com/piotrmurach/necromancer/blob/master/CHANGELOG.md"
18
- spec.metadata["documentation_uri"] = "https://www.rubydoc.info/gems/necromancer"
19
- spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/piotrmurach/necromancer"
21
- end
22
-
23
- spec.files = Dir['{lib,spec}/**/*.rb']
24
- spec.files += Dir['tasks/*', 'necromancer.gemspec']
25
- spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
26
- spec.test_files = spec.files.grep(%r{^(spec)/})
27
- spec.require_paths = ["lib"]
28
-
29
- spec.required_ruby_version = '>= 2.0.0'
30
-
31
- spec.add_development_dependency 'bundler', '>= 1.6'
32
- spec.add_development_dependency 'rake'
33
- spec.add_development_dependency 'rspec', '~> 3.0'
34
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if ENV['COVERAGE'] || ENV['TRAVIS']
4
- require 'simplecov'
5
- require 'coveralls'
6
-
7
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
8
- SimpleCov::Formatter::HTMLFormatter,
9
- Coveralls::SimpleCov::Formatter
10
- ])
11
-
12
- SimpleCov.start do
13
- command_name 'spec'
14
- add_filter 'spec'
15
- end
16
- end
17
-
18
- require 'necromancer'
19
-
20
- RSpec.configure do |config|
21
- config.expect_with :rspec do |expectations|
22
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
23
- end
24
-
25
- config.mock_with :rspec do |mocks|
26
- mocks.verify_partial_doubles = true
27
- end
28
-
29
- # Limits the available syntax to the non-monkey patched syntax that is recommended.
30
- config.disable_monkey_patching!
31
-
32
- # This setting enables warnings. It's recommended, but in some cases may
33
- # be too noisy due to issues in dependencies.
34
- config.warnings = true
35
-
36
- if config.files_to_run.one?
37
- config.default_formatter = 'doc'
38
- end
39
-
40
- config.profile_examples = 2
41
-
42
- config.order = :random
43
-
44
- Kernel.srand config.seed
45
-
46
- config.before :each do
47
- [:UpcaseConverter, :Custom].each do |class_name|
48
- if Object.const_defined?(class_name)
49
- Object.send(:remove_const, class_name)
50
- end
51
- end
52
- end
53
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer, 'can?' do
4
- it "checks if conversion is possible" do
5
- converter = described_class.new
6
- expect(converter.can?(:string, :integer)).to eq(true)
7
- expect(converter.can?(:unknown, :integer)).to eq(false)
8
- end
9
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer, 'config' do
4
- it "configures global settings per instance" do
5
- converter = described_class.new
6
-
7
- converter.configure do |config|
8
- config.strict false
9
- end
10
- expect(converter.convert("1.2.3").to(:array)).to eq(["1.2.3"])
11
-
12
- converter.configure do |config|
13
- config.strict true
14
- end
15
- expect {
16
- converter.convert("1.2.3").to(:array)
17
- }.to raise_error(Necromancer::ConversionTypeError)
18
- end
19
-
20
- it "configures global settings through instance block" do
21
- converter = described_class.new do |config|
22
- config.strict true
23
- end
24
- expect(converter.configuration.strict).to eq(true)
25
-
26
- expect {
27
- converter.convert("1.2.3").to(:array)
28
- }.to raise_error(Necromancer::ConversionTypeError)
29
- end
30
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer::Configuration, '.new' do
4
-
5
- subject(:config) { described_class.new }
6
-
7
- it { is_expected.to respond_to(:strict=) }
8
-
9
- it { is_expected.to respond_to(:copy=) }
10
-
11
- it "is in non-strict mode by default" do
12
- expect(config.strict).to eq(false)
13
- end
14
-
15
- it "is in copy mode by default" do
16
- expect(config.copy).to eq(true)
17
- end
18
-
19
- it "allows to set strict through method" do
20
- config.strict true
21
- expect(config.strict).to eq(true)
22
- end
23
-
24
- it "allows to set copy mode through method" do
25
- config.copy false
26
- expect(config.strict).to eq(false)
27
- end
28
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer::Conversions, '#fetch' do
4
- it "retrieves conversion given source & target" do
5
- converter = double(:converter)
6
- conversions = described_class.new nil, {'string->array' => converter}
7
- expect(conversions['string', 'array']).to eq(converter)
8
- end
9
-
10
- it "fails to find conversion" do
11
- conversions = described_class.new
12
- expect {
13
- conversions['string', 'array']
14
- }.to raise_error(Necromancer::NoTypeConversionAvailableError)
15
- end
16
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer::Conversions, '.register' do
4
- it "allows to register converter" do
5
- context = described_class.new
6
- converter = double(:converter, {source: :string, target: :numeric})
7
- expect(context.register(converter)).to eq(true)
8
- expect(context[:string, :numeric]).to eq(converter)
9
- end
10
-
11
- it "allows to register converter with no source" do
12
- context = described_class.new
13
- converter = double(:converter, {source: nil, target: :numeric})
14
- expect(context.register(converter)).to eq(true)
15
- expect(context[:none, :numeric]).to eq(converter)
16
- end
17
-
18
- it "allows to register converter with no target" do
19
- context = described_class.new
20
- converter = double(:converter, {source: :string, target: nil})
21
- expect(context.register(converter)).to eq(true)
22
- expect(context[:string, :none]).to eq(converter)
23
- end
24
-
25
- it "allows to register anonymous converter" do
26
- conversions = described_class.new
27
-
28
- conversions.register do |c|
29
- c.source= :string
30
- c.target= :upcase
31
- c.convert = proc { |value| value.to_s.upcase }
32
- end
33
- expect(conversions[:string, :upcase].call('magic')).to eq('MAGIC')
34
- end
35
-
36
- it "allows to register anonymous converter with class names" do
37
- conversions = described_class.new
38
-
39
- conversions.register do |c|
40
- c.source= String
41
- c.target= Array
42
- c.convert = proc { |value| Array(value) }
43
- end
44
- expect(conversions[String, Array].call('magic')).to eq(['magic'])
45
- end
46
-
47
- it "allows to register custom converter" do
48
- conversions = described_class.new
49
- UpcaseConverter = Struct.new(:source, :target) do
50
- def call(value)
51
- value.to_s.upcase
52
- end
53
- end
54
- upcase_converter = UpcaseConverter.new(:string, :upcase)
55
- expect(conversions.register(upcase_converter)).to be(true)
56
- expect(conversions[:string, :upcase].call('magic')).to eq('MAGIC')
57
- end
58
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
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,130 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer, '.convert' do
4
-
5
- subject(:converter) { described_class.new }
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
-
30
- context 'when array' do
31
- it "converts string to array" do
32
- expect(converter.convert("1,2,3").to(:array)).to eq([1,2,3])
33
- end
34
-
35
- it "converts array to numeric " do
36
- expect(converter.convert(['1','2.3','3.0']).to(:numeric)).to eq([1,2.3,3.0])
37
- end
38
-
39
- it "converts array to boolean" do
40
- expect(converter.convert(['t', 'no']).to(:boolean)).to eq([true, false])
41
- end
42
-
43
- it "converts object to array" do
44
- expect(converter.convert({x: 1}).to(:array)).to eq([[:x, 1]])
45
- end
46
-
47
- it "fails to convert in strict mode" do
48
- expect {
49
- converter.convert(['1', '2.3', false]).to(:numeric, strict: true)
50
- }.to raise_error(Necromancer::ConversionTypeError)
51
- end
52
- end
53
-
54
- context 'when numeric' do
55
- it "converts string to integer" do
56
- expect(converter.convert('1').to(:integer)).to eq(1)
57
- end
58
-
59
- it "allows for block for conversion method" do
60
- expect(converter.convert { '1' }.to(:integer)).to eq(1)
61
- end
62
-
63
- it "convers integer to string" do
64
- expect(converter.convert(1).to(:string)).to eq('1')
65
- end
66
-
67
- it "allows for null type conversion" do
68
- expect(converter.convert(1).to(:integer)).to eq(1)
69
- end
70
-
71
- it "raises error when in strict mode" do
72
- expect {
73
- converter.convert('1a').to(:integer, strict: true)
74
- }.to raise_error(Necromancer::ConversionTypeError)
75
- end
76
-
77
- it "doesn't raise error when in non-strict mode" do
78
- expect(converter.convert('1').to(:integer, strict: false)).to eql(1)
79
- end
80
-
81
- it "converts string to float" do
82
- expect(converter.convert('1.0').to(:float)).to eql(1.0)
83
- end
84
-
85
- it "converts string to numeric" do
86
- expect(converter.convert('1.0').to(:numeric)).to eql(1.0)
87
- end
88
- end
89
-
90
- context 'when boolean' do
91
- it "converts boolean to boolean" do
92
- expect(converter.convert(true).to(:boolean)).to eq(true)
93
- end
94
-
95
- it "converts string to boolean" do
96
- expect(converter.convert('yes').to(:boolean)).to eq(true)
97
- end
98
-
99
- it "converts integer to boolean" do
100
- expect(converter.convert(0).to(:boolean)).to eq(false)
101
- end
102
-
103
- it "converts boolean to integer" do
104
- expect(converter.convert(true).to(:integer)).to eq(1)
105
- end
106
- end
107
-
108
- context 'when range' do
109
- it "converts string to range" do
110
- expect(converter.convert('1-10').to(:range)).to eq(1..10)
111
- end
112
- end
113
-
114
- context 'when datetime' do
115
- it "converts string to date" do
116
- expect(converter.convert('2014-12-07').to(:date)).
117
- to eq(Date.parse('2014-12-07'))
118
- end
119
-
120
- it "converts string to datetime" do
121
- expect(converter.convert('2014-12-07 17:35:44').to(:datetime)).
122
- to eq(DateTime.parse('2014-12-07 17:35:44'))
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
129
- end
130
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer::ArrayConverters::ArrayToBooleanConverter, '.call' do
4
-
5
- subject(:converter) { described_class.new(:array, :boolean) }
6
-
7
- it "converts `['t', 'f', 'yes', 'no']` to boolean array" do
8
- expect(converter.call(['t', 'f', 'yes', 'no'])).to eq([true, false, true, false])
9
- end
10
-
11
- it "fails to convert `['t', 'no', 5]` to boolean array in strict mode" do
12
- expect {
13
- converter.call(['t', 'no', 5], strict: true)
14
- }.to raise_error(Necromancer::ConversionTypeError)
15
- end
16
-
17
- it "converts `['t', 'no', 5]` to boolean array in non-strict mode" do
18
- expect(converter.call(['t', 'no', 5], strict: false)).to eql([true, false, 5])
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Necromancer::ArrayConverters::ArrayToNumericConverter, '.call' do
4
-
5
- subject(:converter) { described_class.new(:array, :numeric) }
6
-
7
- it "converts `['1','2.3','3.0']` to numeric array" do
8
- expect(converter.call(['1', '2.3', '3.0'])).to eq([1, 2.3, 3.0])
9
- end
10
-
11
- it "fails to convert `['1','2.3',false]` to numeric array in strict mode" do
12
- expect {
13
- converter.call(['1', '2.3', false], strict: true)
14
- }.to raise_error(Necromancer::ConversionTypeError)
15
- end
16
-
17
- it "converts `['1','2.3',false]` to numeric array in non-strict mode" do
18
- expect(converter.call(['1', '2.3', false], strict: false)).to eq([1, 2.3, false])
19
- end
20
- end