necromancer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +19 -0
  6. data/Gemfile +16 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +141 -0
  9. data/Rakefile +8 -0
  10. data/lib/necromancer.rb +27 -0
  11. data/lib/necromancer/context.rb +45 -0
  12. data/lib/necromancer/conversion_target.rb +82 -0
  13. data/lib/necromancer/conversions.rb +75 -0
  14. data/lib/necromancer/converter.rb +37 -0
  15. data/lib/necromancer/converters/array.rb +50 -0
  16. data/lib/necromancer/converters/boolean.rb +58 -0
  17. data/lib/necromancer/converters/float.rb +28 -0
  18. data/lib/necromancer/converters/integer.rb +48 -0
  19. data/lib/necromancer/converters/range.rb +38 -0
  20. data/lib/necromancer/null_converter.rb +10 -0
  21. data/lib/necromancer/version.rb +5 -0
  22. data/necromancer.gemspec +21 -0
  23. data/spec/spec_helper.rb +45 -0
  24. data/spec/unit/can_spec.rb +11 -0
  25. data/spec/unit/conversions/register_spec.rb +50 -0
  26. data/spec/unit/convert_spec.rb +66 -0
  27. data/spec/unit/converters/array/array_to_numeric_spec.rb +22 -0
  28. data/spec/unit/converters/array/string_to_array_spec.rb +15 -0
  29. data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +16 -0
  30. data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +16 -0
  31. data/spec/unit/converters/boolean/string_to_boolean_spec.rb +96 -0
  32. data/spec/unit/converters/float/string_to_float_spec.rb +28 -0
  33. data/spec/unit/converters/integer/string_to_integer_spec.rb +38 -0
  34. data/spec/unit/converters/range/string_to_range_spec.rb +52 -0
  35. data/spec/unit/new_spec.rb +12 -0
  36. data/tasks/console.rake +10 -0
  37. data/tasks/coverage.rake +11 -0
  38. data/tasks/spec.rake +29 -0
  39. metadata +109 -0
@@ -0,0 +1,22 @@
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 }
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
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Necromancer::ArrayConverters::StringToArrayConverter, '.call' do
6
+ subject(:converter) { described_class.new }
7
+
8
+ it "converts `1,2,3` to array" do
9
+ expect(converter.call('1,2,3')).to eq([1,2,3])
10
+ end
11
+
12
+ it "converts `a,b,c` to array" do
13
+ expect(converter.call('a,b,c')).to eq(['a','b','c'])
14
+ end
15
+ end
@@ -0,0 +1,16 @@
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
+ end
@@ -0,0 +1,16 @@
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
+ end
@@ -0,0 +1,96 @@
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 }
8
+
9
+ it "raises error for empty string" do
10
+ expect { converter.call('') }.to raise_error(ArgumentError)
11
+ end
12
+
13
+ it "passes through boolean value" do
14
+ expect(converter.call(true)).to eq(true)
15
+ end
16
+
17
+ it "converts 'true' to true value" do
18
+ expect(converter.call('true')).to eq(true)
19
+ end
20
+
21
+ it "converts 'TRUE' to true value" do
22
+ expect(converter.call('TRUE')).to eq(true)
23
+ end
24
+
25
+ it "converts TRUE to true value" do
26
+ expect(converter.call(TRUE)).to eq(true)
27
+ end
28
+
29
+ it "converts 't' to true value" do
30
+ expect(converter.call('t')).to eq(true)
31
+ end
32
+
33
+ it "converts 'T' to true value" do
34
+ expect(converter.call('T')).to eq(true)
35
+ end
36
+
37
+ it "converts 1 to true value" do
38
+ expect(converter.call(1)).to eq(true)
39
+ end
40
+
41
+ it "converts '1' to true value" do
42
+ expect(converter.call('1')).to eq(true)
43
+ end
44
+
45
+ it "converts 'y' to true value" do
46
+ expect(converter.call('y')).to eq(true)
47
+ end
48
+
49
+ it "converts 'yes' to true value" do
50
+ expect(converter.call('yes')).to eq(true)
51
+ end
52
+
53
+ it "converts 'on' to true value" do
54
+ expect(converter.call('on')).to eq(true)
55
+ end
56
+
57
+ it "converts 'false' to false value" do
58
+ expect(converter.call('false')).to eq(false)
59
+ end
60
+
61
+ it "converts FALSE to false value" do
62
+ expect(converter.call(FALSE)).to eq(false)
63
+ end
64
+
65
+ it "converts 'FALSE' to false value" do
66
+ expect(converter.call('FALSE')).to eq(false)
67
+ end
68
+
69
+ it "converts 'f' to false value" do
70
+ expect(converter.call('f')).to eq(false)
71
+ end
72
+
73
+ it "converts 'F' to false value" do
74
+ expect(converter.call('F')).to eq(false)
75
+ end
76
+
77
+ it "converts '0' to false value" do
78
+ expect(converter.call('0')).to eq(false)
79
+ end
80
+
81
+ it "converts 'n' to false value" do
82
+ expect(converter.call('n')).to eq(false)
83
+ end
84
+
85
+ it "converts 'no' to false value" do
86
+ expect(converter.call('no')).to eq(false)
87
+ end
88
+
89
+ it "converts 'off' to false value" do
90
+ expect(converter.call('off')).to eq(false)
91
+ end
92
+
93
+ it "fails to convert unkonwn value FOO" do
94
+ expect { converter.call('FOO') }.to raise_error(ArgumentError)
95
+ end
96
+ end
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Necromancer::IntegerConverters::StringToIntegerConverter, '.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 empty string to 0 in non-strict mode" do
16
+ expect(converter.call('', strict: false)).to eq(0)
17
+ end
18
+
19
+ it "raises error for float in strict mode" do
20
+ expect {
21
+ converter.call(1.2, strict: true)
22
+ }.to raise_error(Necromancer::ConversionTypeError)
23
+ end
24
+
25
+ it "converts float to integer in non-strict mode" do
26
+ expect(converter.call(1.2)).to eq(1)
27
+ end
28
+
29
+ it "converts mixed string to integer in non-strict mode" do
30
+ expect(converter.call('1abc')).to eq(1)
31
+ end
32
+
33
+ it "raises error for mixed string in strict mode" do
34
+ expect {
35
+ converter.call('1abc', strict: true)
36
+ }.to raise_error(Necromancer::ConversionTypeError)
37
+ end
38
+ end
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Necromancer::RangeConverters::StringToRangeConverter, '.call' do
6
+
7
+ subject(:converter) { described_class.new }
8
+
9
+ it "raises error for empty string" do
10
+ expect { converter.call('') }.to raise_error(Necromancer::ConversionTypeError)
11
+ end
12
+
13
+ it "converts '1' to range" do
14
+ expect(converter.call('1')).to eq(1..1)
15
+ end
16
+
17
+ it "converts '1..10' to range value" do
18
+ expect(converter.call('1..10')).to eq(1..10)
19
+ end
20
+
21
+ it "converts '1-10' to range value" do
22
+ expect(converter.call('1-10')).to eq(1..10)
23
+ end
24
+
25
+ it "converts '1,10' to range value" do
26
+ expect(converter.call('1,10')).to eq(1..10)
27
+ end
28
+
29
+ it "converts '1...10' to range value" do
30
+ expect(converter.call('1...10')).to eq(1...10)
31
+ end
32
+
33
+ it "converts '-1..10' to range value" do
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')
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Necromancer, '#new' do
6
+
7
+ subject(:converter) { described_class.new }
8
+
9
+ it "creates context" do
10
+ expect(converter).to be_a(Necromancer::Context)
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Load gem inside irb console'
4
+ task :console do
5
+ require 'irb'
6
+ require 'irb/completion'
7
+ require File.join(__FILE__, '../../lib/necromancer')
8
+ ARGV.clear
9
+ IRB.start
10
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Measure code coverage'
4
+ task :coverage do
5
+ begin
6
+ original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
+ Rake::Task['spec'].invoke
8
+ ensure
9
+ ENV['COVERAGE'] = original
10
+ end
11
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
+ end
10
+
11
+ namespace :spec do
12
+ desc 'Run unit specs'
13
+ RSpec::Core::RakeTask.new(:unit) do |task|
14
+ task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
+ end
16
+
17
+ desc 'Run integration specs'
18
+ RSpec::Core::RakeTask.new(:integration) do |task|
19
+ task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
+ end
21
+ end
22
+
23
+ rescue LoadError
24
+ %w[spec spec:unit spec:integration].each do |name|
25
+ task name do
26
+ $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: necromancer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ description: Conversion from one object type to another with a bit of black magic.
28
+ email:
29
+ - ''
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - .rspec
36
+ - .ruby-version
37
+ - .travis.yml
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/necromancer.rb
43
+ - lib/necromancer/context.rb
44
+ - lib/necromancer/conversion_target.rb
45
+ - lib/necromancer/conversions.rb
46
+ - lib/necromancer/converter.rb
47
+ - lib/necromancer/converters/array.rb
48
+ - lib/necromancer/converters/boolean.rb
49
+ - lib/necromancer/converters/float.rb
50
+ - lib/necromancer/converters/integer.rb
51
+ - lib/necromancer/converters/range.rb
52
+ - lib/necromancer/null_converter.rb
53
+ - lib/necromancer/version.rb
54
+ - necromancer.gemspec
55
+ - spec/spec_helper.rb
56
+ - spec/unit/can_spec.rb
57
+ - spec/unit/conversions/register_spec.rb
58
+ - spec/unit/convert_spec.rb
59
+ - spec/unit/converters/array/array_to_numeric_spec.rb
60
+ - spec/unit/converters/array/string_to_array_spec.rb
61
+ - spec/unit/converters/boolean/boolean_to_integer_spec.rb
62
+ - spec/unit/converters/boolean/integer_to_boolean_spec.rb
63
+ - spec/unit/converters/boolean/string_to_boolean_spec.rb
64
+ - spec/unit/converters/float/string_to_float_spec.rb
65
+ - spec/unit/converters/integer/string_to_integer_spec.rb
66
+ - spec/unit/converters/range/string_to_range_spec.rb
67
+ - spec/unit/new_spec.rb
68
+ - tasks/console.rake
69
+ - tasks/coverage.rake
70
+ - tasks/spec.rake
71
+ homepage: https://github.com/peter-murach/necromancer
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Conversion from one object type to another with a bit of black magic.
95
+ test_files:
96
+ - spec/spec_helper.rb
97
+ - spec/unit/can_spec.rb
98
+ - spec/unit/conversions/register_spec.rb
99
+ - spec/unit/convert_spec.rb
100
+ - spec/unit/converters/array/array_to_numeric_spec.rb
101
+ - spec/unit/converters/array/string_to_array_spec.rb
102
+ - spec/unit/converters/boolean/boolean_to_integer_spec.rb
103
+ - spec/unit/converters/boolean/integer_to_boolean_spec.rb
104
+ - spec/unit/converters/boolean/string_to_boolean_spec.rb
105
+ - spec/unit/converters/float/string_to_float_spec.rb
106
+ - spec/unit/converters/integer/string_to_integer_spec.rb
107
+ - spec/unit/converters/range/string_to_range_spec.rb
108
+ - spec/unit/new_spec.rb
109
+ has_rdoc: