coercible 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +0 -4
- data/Changelog.md +4 -0
- data/Gemfile +2 -2
- data/Gemfile.devtools +0 -1
- data/coercible.gemspec +1 -1
- data/lib/coercible.rb +9 -1
- data/lib/coercible/coercer/configurable.rb +4 -0
- data/lib/coercible/coercer/object.rb +14 -5
- data/lib/coercible/coercer/string.rb +8 -3
- data/lib/coercible/coercer/time_coercions.rb +3 -4
- data/lib/coercible/version.rb +1 -1
- data/lib/support/options.rb +6 -0
- data/spec/unit/coercible/coercer/object/to_hash_spec.rb +1 -1
- data/spec/unit/coercible/coercer/object/to_integer_spec.rb +1 -1
- data/spec/unit/coercible/coercer/object/to_string_spec.rb +1 -1
- data/spec/unit/coercible/coercer/string/to_decimal_spec.rb +1 -1
- data/spec/unit/coercible/coercer/string/to_float_spec.rb +2 -3
- data/spec/unit/coercible/coercer/string/to_integer_spec.rb +2 -3
- metadata +9 -35
- data/config/yardstick.yml +0 -2
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 709edbe22e3e38bb3e2c6408e6dd2c7edd441a3b
|
4
|
+
data.tar.gz: dfc9e7750e14888a69cfa6657fe795e0e7b9298d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e45ec911eb3e4d22aab74081091b9e2d80911d0875be1b0bdd4b661700b52b54690f08ccb5af80fd84dc9953d17d8eb08e5b2e3b3e8cbd57c00bb6114413067
|
7
|
+
data.tar.gz: 652a0700da4b6999628b39c9278316aeaa38d668a8e53bcab7d0e76b2e511d12912d3f12e8607e62d4568337e63d12cbc76308ef8ad1be581fbeae205c63e980
|
data/.travis.yml
CHANGED
@@ -3,15 +3,11 @@ before_install: gem install bundler
|
|
3
3
|
bundler_args: --without guard metrics
|
4
4
|
script: "bundle exec rake spec"
|
5
5
|
rvm:
|
6
|
-
- 1.8.7
|
7
6
|
- 1.9.2
|
8
7
|
- 1.9.3
|
9
8
|
- 2.0.0
|
10
|
-
- jruby-18mode
|
11
9
|
- jruby-19mode
|
12
|
-
- rbx-18mode
|
13
10
|
- rbx-19mode
|
14
|
-
- ree
|
15
11
|
- jruby-head
|
16
12
|
|
17
13
|
notifications:
|
data/Changelog.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.devtools
CHANGED
data/coercible.gemspec
CHANGED
@@ -11,12 +11,12 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.description = %q{Powerful, flexible and configurable coercion library. And nothing more.}
|
12
12
|
gem.summary = gem.description
|
13
13
|
gem.homepage = "https://github.com/solnic/coercible"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
19
|
gem.require_paths = ["lib"]
|
19
20
|
|
20
|
-
gem.add_dependency 'backports', [ '~> 3.0', '>= 3.1.0' ]
|
21
21
|
gem.add_dependency 'descendants_tracker', '~> 0.0.1'
|
22
22
|
end
|
data/lib/coercible.rb
CHANGED
@@ -3,6 +3,15 @@ module Coercible
|
|
3
3
|
|
4
4
|
UnsupportedCoercion = Class.new(StandardError)
|
5
5
|
|
6
|
+
# Test for rubinius platform
|
7
|
+
#
|
8
|
+
# @return [true]
|
9
|
+
# if running under rubinius
|
10
|
+
#
|
11
|
+
# @return [false]
|
12
|
+
# otherwise
|
13
|
+
#
|
14
|
+
# @api private
|
6
15
|
def self.rbx?
|
7
16
|
@is_rbx ||= defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
|
8
17
|
end
|
@@ -13,7 +22,6 @@ require 'time'
|
|
13
22
|
require 'bigdecimal'
|
14
23
|
require 'bigdecimal/util'
|
15
24
|
require 'set'
|
16
|
-
require 'backports'
|
17
25
|
|
18
26
|
require 'descendants_tracker'
|
19
27
|
require 'support/options'
|
@@ -31,6 +31,9 @@ module Coercible
|
|
31
31
|
|
32
32
|
# Inspect the coercer object
|
33
33
|
#
|
34
|
+
# @example
|
35
|
+
# coercer[Object].inspect # => "<Coercer::Object primitive=Object>"
|
36
|
+
#
|
34
37
|
# @return [String]
|
35
38
|
#
|
36
39
|
# @api public
|
@@ -76,7 +79,7 @@ module Coercible
|
|
76
79
|
#
|
77
80
|
# @api public
|
78
81
|
def to_hash(value)
|
79
|
-
coerce_with_method(value, :to_hash)
|
82
|
+
coerce_with_method(value, :to_hash, __method__)
|
80
83
|
end
|
81
84
|
|
82
85
|
# Create a String from the Object if possible
|
@@ -96,7 +99,7 @@ module Coercible
|
|
96
99
|
#
|
97
100
|
# @api public
|
98
101
|
def to_string(value)
|
99
|
-
coerce_with_method(value, :to_str)
|
102
|
+
coerce_with_method(value, :to_str, __method__)
|
100
103
|
end
|
101
104
|
|
102
105
|
# Create an Integer from the Object if possible
|
@@ -116,11 +119,17 @@ module Coercible
|
|
116
119
|
#
|
117
120
|
# @api public
|
118
121
|
def to_integer(value)
|
119
|
-
coerce_with_method(value, :to_int)
|
122
|
+
coerce_with_method(value, :to_int, __method__)
|
120
123
|
end
|
121
124
|
|
122
125
|
# Return if the value was successfuly coerced
|
123
126
|
#
|
127
|
+
# @example when coercion was successful
|
128
|
+
# coercer[String].coerced?(1) # => true
|
129
|
+
#
|
130
|
+
# @example when coercion was NOT successful
|
131
|
+
# coercer[String].coerced?("foo") # => false
|
132
|
+
#
|
124
133
|
# @return [TrueClass,FalseClass]
|
125
134
|
#
|
126
135
|
# @api public
|
@@ -168,8 +177,8 @@ module Coercible
|
|
168
177
|
# @return [Object]
|
169
178
|
#
|
170
179
|
# @api private
|
171
|
-
def coerce_with_method(value, method)
|
172
|
-
value.respond_to?(method) ? value.public_send(method) : value
|
180
|
+
def coerce_with_method(value, method, ref_method)
|
181
|
+
value.respond_to?(method) ? value.public_send(method) : raise_unsupported_coercion(value, ref_method)
|
173
182
|
end
|
174
183
|
|
175
184
|
end # class Object
|
@@ -159,9 +159,10 @@ module Coercible
|
|
159
159
|
else
|
160
160
|
# coerce to a Float first to evaluate scientific notation (if any)
|
161
161
|
# that may change the integer part, then convert to an integer
|
162
|
-
|
163
|
-
::Float === coerced ? coerced.to_i : coerced
|
162
|
+
to_float(value).to_i
|
164
163
|
end
|
164
|
+
rescue UnsupportedCoercion
|
165
|
+
raise_unsupported_coercion(value, __method__)
|
165
166
|
end
|
166
167
|
|
167
168
|
# Coerce value to float
|
@@ -176,6 +177,8 @@ module Coercible
|
|
176
177
|
# @api public
|
177
178
|
def to_float(value)
|
178
179
|
to_numeric(value, :to_f)
|
180
|
+
rescue UnsupportedCoercion
|
181
|
+
raise_unsupported_coercion(value, __method__)
|
179
182
|
end
|
180
183
|
|
181
184
|
# Coerce value to decimal
|
@@ -190,6 +193,8 @@ module Coercible
|
|
190
193
|
# @api public
|
191
194
|
def to_decimal(value)
|
192
195
|
to_numeric(value, :to_d)
|
196
|
+
rescue UnsupportedCoercion
|
197
|
+
raise_unsupported_coercion(value, __method__)
|
193
198
|
end
|
194
199
|
|
195
200
|
private
|
@@ -226,7 +231,7 @@ module Coercible
|
|
226
231
|
if value =~ NUMERIC_REGEXP
|
227
232
|
$1.public_send(method)
|
228
233
|
else
|
229
|
-
value
|
234
|
+
raise_unsupported_coercion(value, method)
|
230
235
|
end
|
231
236
|
end
|
232
237
|
|
@@ -73,11 +73,10 @@ module Coercible
|
|
73
73
|
#
|
74
74
|
# @api private
|
75
75
|
def coerce_with_method(value, method)
|
76
|
-
|
77
|
-
|
78
|
-
coercers[::String].public_send(method, to_string(value))
|
76
|
+
if value.respond_to?(method)
|
77
|
+
value.public_send(method)
|
79
78
|
else
|
80
|
-
|
79
|
+
coercers[::String].public_send(method, to_string(value))
|
81
80
|
end
|
82
81
|
end
|
83
82
|
|
data/lib/coercible/version.rb
CHANGED
data/lib/support/options.rb
CHANGED
@@ -4,6 +4,12 @@ module Coercible
|
|
4
4
|
module Options
|
5
5
|
Undefined = Class.new.freeze
|
6
6
|
|
7
|
+
# Hook called when descendant was extended
|
8
|
+
#
|
9
|
+
# @param [Class,Module] descendant
|
10
|
+
#
|
11
|
+
# @return [undefined]
|
12
|
+
#
|
7
13
|
# @api private
|
8
14
|
def self.extended(descendant)
|
9
15
|
descendant.extend(DescendantsTracker)
|
@@ -45,13 +45,12 @@ describe Coercer::String, '.to_float' do
|
|
45
45
|
context 'with an invalid float string' do
|
46
46
|
let(:string) { 'non-float' }
|
47
47
|
|
48
|
-
|
48
|
+
specify { expect { subject }.to raise_error(UnsupportedCoercion) }
|
49
49
|
end
|
50
50
|
|
51
51
|
context 'string starts with e' do
|
52
52
|
let(:string) { 'e1' }
|
53
53
|
|
54
|
-
|
55
|
-
it { should == 'e1' }
|
54
|
+
specify { expect { subject }.to raise_error(UnsupportedCoercion) }
|
56
55
|
end
|
57
56
|
end
|
@@ -50,7 +50,7 @@ describe Coercer::String, '.to_integer' do
|
|
50
50
|
context 'with an invalid integer string' do
|
51
51
|
let(:string) { 'non-integer' }
|
52
52
|
|
53
|
-
|
53
|
+
specify { expect { subject }.to raise_error(UnsupportedCoercion) }
|
54
54
|
end
|
55
55
|
|
56
56
|
context 'when integer string is big' do
|
@@ -62,7 +62,6 @@ describe Coercer::String, '.to_integer' do
|
|
62
62
|
context 'string starts with e' do
|
63
63
|
let(:string) { 'e1' }
|
64
64
|
|
65
|
-
|
66
|
-
it { should == 'e1' }
|
65
|
+
specify { expect { subject }.to raise_error(UnsupportedCoercion) }
|
67
66
|
end
|
68
67
|
end
|
metadata
CHANGED
@@ -1,42 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coercible
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Piotr Solnica
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-12-10 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: backports
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '3.0'
|
22
|
-
- - ! '>='
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version: 3.1.0
|
25
|
-
type: :runtime
|
26
|
-
prerelease: false
|
27
|
-
version_requirements: !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ~>
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '3.0'
|
33
|
-
- - ! '>='
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
version: 3.1.0
|
36
13
|
- !ruby/object:Gem::Dependency
|
37
14
|
name: descendants_tracker
|
38
15
|
requirement: !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
16
|
requirements:
|
41
17
|
- - ~>
|
42
18
|
- !ruby/object:Gem::Version
|
@@ -44,7 +20,6 @@ dependencies:
|
|
44
20
|
type: :runtime
|
45
21
|
prerelease: false
|
46
22
|
version_requirements: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
23
|
requirements:
|
49
24
|
- - ~>
|
50
25
|
- !ruby/object:Gem::Version
|
@@ -72,7 +47,6 @@ files:
|
|
72
47
|
- config/mutant.yml
|
73
48
|
- config/roodi.yml
|
74
49
|
- config/site.reek
|
75
|
-
- config/yardstick.yml
|
76
50
|
- lib/coercible.rb
|
77
51
|
- lib/coercible/coercer.rb
|
78
52
|
- lib/coercible/coercer/array.rb
|
@@ -178,28 +152,28 @@ files:
|
|
178
152
|
- spec/unit/coercible/coercer/true_class/to_string_spec.rb
|
179
153
|
- spec/unit/coercible/configuration/class_methods/build_spec.rb
|
180
154
|
homepage: https://github.com/solnic/coercible
|
181
|
-
licenses:
|
155
|
+
licenses:
|
156
|
+
- MIT
|
157
|
+
metadata: {}
|
182
158
|
post_install_message:
|
183
159
|
rdoc_options: []
|
184
160
|
require_paths:
|
185
161
|
- lib
|
186
162
|
required_ruby_version: !ruby/object:Gem::Requirement
|
187
|
-
none: false
|
188
163
|
requirements:
|
189
|
-
- -
|
164
|
+
- - '>='
|
190
165
|
- !ruby/object:Gem::Version
|
191
166
|
version: '0'
|
192
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
193
|
-
none: false
|
194
168
|
requirements:
|
195
|
-
- -
|
169
|
+
- - '>='
|
196
170
|
- !ruby/object:Gem::Version
|
197
171
|
version: '0'
|
198
172
|
requirements: []
|
199
173
|
rubyforge_project:
|
200
|
-
rubygems_version:
|
174
|
+
rubygems_version: 2.0.3
|
201
175
|
signing_key:
|
202
|
-
specification_version:
|
176
|
+
specification_version: 4
|
203
177
|
summary: Powerful, flexible and configurable coercion library. And nothing more.
|
204
178
|
test_files:
|
205
179
|
- spec/integration/configuring_coercers_spec.rb
|
data/config/yardstick.yml
DELETED