coercible 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +4 -0
- data/config/flay.yml +1 -1
- data/lib/coercible/coercer.rb +3 -13
- data/lib/coercible/coercer/configurable.rb +7 -2
- data/lib/coercible/coercer/object.rb +10 -1
- data/lib/coercible/version.rb +1 -1
- data/lib/support/options.rb +5 -0
- data/spec/unit/coercible/coercer/configurable/config_name_spec.rb +18 -0
- data/spec/unit/coercible/coercer/configurable/config_spec.rb +25 -0
- data/spec/unit/coercible/coercer/{element_reference_spec.rb → element_reader_spec.rb} +0 -0
- data/spec/unit/coercible/coercer/float/to_datetime_spec.rb +3 -1
- data/spec/unit/coercible/coercer/integer/to_datetime_spec.rb +3 -1
- data/spec/unit/coercible/coercer/numeric/to_decimal_spec.rb +13 -4
- data/spec/unit/coercible/coercer/object/inspect_spec.rb +9 -0
- data/spec/unit/coercible/coercer/string/class_methods/config_spec.rb +7 -0
- data/spec/unit/coercible/coercer/time_coercions/to_date_spec.rb +4 -0
- data/spec/unit/coercible/coercer/time_coercions/to_datetime_spec.rb +4 -0
- data/spec/unit/coercible/coercer/time_coercions/to_string_spec.rb +4 -0
- data/spec/unit/coercible/coercer/time_coercions/to_time_spec.rb +4 -0
- metadata +12 -4
data/Changelog.md
CHANGED
data/config/flay.yml
CHANGED
data/lib/coercible/coercer.rb
CHANGED
@@ -47,7 +47,7 @@ module Coercible
|
|
47
47
|
|
48
48
|
yield(configuration) if block_given?
|
49
49
|
|
50
|
-
super(
|
50
|
+
super(configuration)
|
51
51
|
end
|
52
52
|
|
53
53
|
# Return configuration keys for Coercer instance
|
@@ -81,10 +81,9 @@ module Coercible
|
|
81
81
|
# @return [undefined]
|
82
82
|
#
|
83
83
|
# @api private
|
84
|
-
def initialize(coercers = {}
|
84
|
+
def initialize(config, coercers = {})
|
85
85
|
@coercers = coercers
|
86
86
|
@config = config
|
87
|
-
initialize_default_coercer
|
88
87
|
end
|
89
88
|
|
90
89
|
# Access a specific coercer object for the given type
|
@@ -105,7 +104,7 @@ module Coercible
|
|
105
104
|
|
106
105
|
private
|
107
106
|
|
108
|
-
# Initialize a new coercer instance for the
|
107
|
+
# Initialize a new coercer instance for the given type
|
109
108
|
#
|
110
109
|
# If a coercer class supports configuration it will receive it from the
|
111
110
|
# global configuration object
|
@@ -123,15 +122,6 @@ module Coercible
|
|
123
122
|
end
|
124
123
|
end
|
125
124
|
|
126
|
-
# Initialize default coercer object
|
127
|
-
#
|
128
|
-
# @return [Coercer::Object]
|
129
|
-
#
|
130
|
-
# @api private
|
131
|
-
def initialize_default_coercer
|
132
|
-
coercers[::Object] = Coercer::Object.new(self)
|
133
|
-
end
|
134
|
-
|
135
125
|
# Find configuration for the given coercer type
|
136
126
|
#
|
137
127
|
# @return [Configuration]
|
@@ -34,7 +34,7 @@ module Coercible
|
|
34
34
|
#
|
35
35
|
# @api public
|
36
36
|
def config(&block)
|
37
|
-
configuration =
|
37
|
+
configuration = configuration_class.build(config_keys)
|
38
38
|
yield configuration
|
39
39
|
configuration
|
40
40
|
end
|
@@ -45,7 +45,12 @@ module Coercible
|
|
45
45
|
#
|
46
46
|
# @api private
|
47
47
|
def config_name
|
48
|
-
|
48
|
+
name.downcase.split('::').last.to_sym
|
49
|
+
end
|
50
|
+
|
51
|
+
# @api private
|
52
|
+
def configuration_class
|
53
|
+
Configuration
|
49
54
|
end
|
50
55
|
|
51
56
|
end # module Configurable
|
@@ -3,7 +3,7 @@ module Coercible
|
|
3
3
|
|
4
4
|
# Coerce Object values
|
5
5
|
class Object
|
6
|
-
extend
|
6
|
+
extend TypeLookup, Options
|
7
7
|
|
8
8
|
accept_options :primitive
|
9
9
|
|
@@ -29,6 +29,15 @@ module Coercible
|
|
29
29
|
@coercers = coercers
|
30
30
|
end
|
31
31
|
|
32
|
+
# Inspect the coercer object
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
#
|
36
|
+
# @api public
|
37
|
+
def inspect
|
38
|
+
"#<#{self.class} primitive=#{self.class.primitive}>"
|
39
|
+
end
|
40
|
+
|
32
41
|
# Create an Array from any Object
|
33
42
|
#
|
34
43
|
# @example with an object that does not respond to #to_a or #to_ary
|
data/lib/coercible/version.rb
CHANGED
data/lib/support/options.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Coercer::Configurable, '.config_name' do
|
4
|
+
subject { object.config_name }
|
5
|
+
|
6
|
+
let(:object) {
|
7
|
+
Class.new {
|
8
|
+
extend Coercer::Configurable, Options
|
9
|
+
config_keys [ :one, :two ]
|
10
|
+
|
11
|
+
def self.name
|
12
|
+
"Some::Class::Test"
|
13
|
+
end
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
it { should be(:test) }
|
18
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Coercer::Configurable, '.config' do
|
4
|
+
subject { object.config(&block) }
|
5
|
+
|
6
|
+
let(:object) {
|
7
|
+
Class.new {
|
8
|
+
extend Coercer::Configurable, Options
|
9
|
+
config_keys [ :one, :two ]
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
let(:block) { Proc.new { |config| config.test } }
|
14
|
+
let(:configuration) { mock('configuration') }
|
15
|
+
let(:configuration_class) { mock('configuration_class') }
|
16
|
+
|
17
|
+
before do
|
18
|
+
object.stub!(:configuration_class => configuration_class)
|
19
|
+
configuration_class.should_receive(:build).with(object.config_keys).
|
20
|
+
and_return(configuration)
|
21
|
+
configuration.should_receive(:test)
|
22
|
+
end
|
23
|
+
|
24
|
+
it { should be(configuration) }
|
25
|
+
end
|
File without changes
|
@@ -6,5 +6,7 @@ describe Coercer::Integer, '#to_datetime' do
|
|
6
6
|
let(:object) { described_class.new }
|
7
7
|
let(:value) { 1361036672 }
|
8
8
|
|
9
|
-
|
9
|
+
specify do
|
10
|
+
expect(subject.strftime('%Y-%m-%d %H:%M:%S.%L')).to eql('2013-02-16 17:44:32.000')
|
11
|
+
end
|
10
12
|
end
|
@@ -1,10 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Coercer::Numeric, '.to_decimal' do
|
4
|
-
subject { object.to_decimal(
|
4
|
+
subject { object.to_decimal(value) }
|
5
5
|
|
6
|
-
let(:object)
|
7
|
-
let(:numeric) { Rational(2, 2) }
|
6
|
+
let(:object) { described_class.new }
|
8
7
|
|
9
|
-
|
8
|
+
context "with an object responding to #to_d" do
|
9
|
+
let(:value) { Rational(2, 2) }
|
10
|
+
|
11
|
+
it { should eql(BigDecimal('1.0')) }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with an object not responding to #to_d" do
|
15
|
+
let(:value) { Class.new { def to_s; '1'; end }.new }
|
16
|
+
|
17
|
+
it { should eql(BigDecimal('1.0')) }
|
18
|
+
end
|
10
19
|
end
|
@@ -7,6 +7,10 @@ describe Coercer::TimeCoercions, '.to_date' do
|
|
7
7
|
let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } }
|
8
8
|
let(:value) { mock('value') }
|
9
9
|
|
10
|
+
after do
|
11
|
+
Coercer::Object.descendants.delete(coercer)
|
12
|
+
end
|
13
|
+
|
10
14
|
context 'when the value responds to #to_date' do
|
11
15
|
before do
|
12
16
|
value.should_receive(:to_date).and_return(Date.new(2011, 1, 1))
|
@@ -7,6 +7,10 @@ describe Coercer::TimeCoercions, '.to_datetime' do
|
|
7
7
|
let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } }
|
8
8
|
let(:value) { mock('value') }
|
9
9
|
|
10
|
+
after do
|
11
|
+
Coercer::Object.descendants.delete(coercer)
|
12
|
+
end
|
13
|
+
|
10
14
|
context 'when the value responds to #to_datetime' do
|
11
15
|
before do
|
12
16
|
object.extend Coercer::TimeCoercions
|
@@ -7,6 +7,10 @@ describe Coercer::TimeCoercions, '.to_string' do
|
|
7
7
|
let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } }
|
8
8
|
let(:value) { mock('value') }
|
9
9
|
|
10
|
+
after do
|
11
|
+
Coercer::Object.descendants.delete(coercer)
|
12
|
+
end
|
13
|
+
|
10
14
|
before do
|
11
15
|
object.extend Coercer::TimeCoercions
|
12
16
|
|
@@ -7,6 +7,10 @@ describe Coercer::TimeCoercions, '.to_time' do
|
|
7
7
|
let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } }
|
8
8
|
let(:value) { mock('value') }
|
9
9
|
|
10
|
+
after do
|
11
|
+
Coercer::Object.descendants.delete(coercer)
|
12
|
+
end
|
13
|
+
|
10
14
|
context 'when the value responds to #to_time' do
|
11
15
|
before do
|
12
16
|
object.extend Coercer::TimeCoercions
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coercible
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: backports
|
@@ -96,6 +96,8 @@ files:
|
|
96
96
|
- spec/unit/coercible/coercer/array/coerced_predicate_spec.rb
|
97
97
|
- spec/unit/coercible/coercer/array/to_set_spec.rb
|
98
98
|
- spec/unit/coercible/coercer/class_methods/new_spec.rb
|
99
|
+
- spec/unit/coercible/coercer/configurable/config_name_spec.rb
|
100
|
+
- spec/unit/coercible/coercer/configurable/config_spec.rb
|
99
101
|
- spec/unit/coercible/coercer/date/coerced_predicate_spec.rb
|
100
102
|
- spec/unit/coercible/coercer/date/to_date_spec.rb
|
101
103
|
- spec/unit/coercible/coercer/date/to_datetime_spec.rb
|
@@ -111,7 +113,7 @@ files:
|
|
111
113
|
- spec/unit/coercible/coercer/decimal/to_float_spec.rb
|
112
114
|
- spec/unit/coercible/coercer/decimal/to_integer_spec.rb
|
113
115
|
- spec/unit/coercible/coercer/decimal/to_string_spec.rb
|
114
|
-
- spec/unit/coercible/coercer/
|
116
|
+
- spec/unit/coercible/coercer/element_reader_spec.rb
|
115
117
|
- spec/unit/coercible/coercer/false_class/coerced_predicate_spec.rb
|
116
118
|
- spec/unit/coercible/coercer/false_class/to_string_spec.rb
|
117
119
|
- spec/unit/coercible/coercer/float/coerced_predicate_spec.rb
|
@@ -139,11 +141,13 @@ files:
|
|
139
141
|
- spec/unit/coercible/coercer/numeric/to_integer_spec.rb
|
140
142
|
- spec/unit/coercible/coercer/numeric/to_string_spec.rb
|
141
143
|
- spec/unit/coercible/coercer/object/coerced_predicate_spec.rb
|
144
|
+
- spec/unit/coercible/coercer/object/inspect_spec.rb
|
142
145
|
- spec/unit/coercible/coercer/object/method_missing_spec.rb
|
143
146
|
- spec/unit/coercible/coercer/object/to_array_spec.rb
|
144
147
|
- spec/unit/coercible/coercer/object/to_hash_spec.rb
|
145
148
|
- spec/unit/coercible/coercer/object/to_integer_spec.rb
|
146
149
|
- spec/unit/coercible/coercer/object/to_string_spec.rb
|
150
|
+
- spec/unit/coercible/coercer/string/class_methods/config_spec.rb
|
147
151
|
- spec/unit/coercible/coercer/string/coerced_predicate_spec.rb
|
148
152
|
- spec/unit/coercible/coercer/string/to_boolean_spec.rb
|
149
153
|
- spec/unit/coercible/coercer/string/to_constant_spec.rb
|
@@ -199,6 +203,8 @@ test_files:
|
|
199
203
|
- spec/unit/coercible/coercer/array/coerced_predicate_spec.rb
|
200
204
|
- spec/unit/coercible/coercer/array/to_set_spec.rb
|
201
205
|
- spec/unit/coercible/coercer/class_methods/new_spec.rb
|
206
|
+
- spec/unit/coercible/coercer/configurable/config_name_spec.rb
|
207
|
+
- spec/unit/coercible/coercer/configurable/config_spec.rb
|
202
208
|
- spec/unit/coercible/coercer/date/coerced_predicate_spec.rb
|
203
209
|
- spec/unit/coercible/coercer/date/to_date_spec.rb
|
204
210
|
- spec/unit/coercible/coercer/date/to_datetime_spec.rb
|
@@ -214,7 +220,7 @@ test_files:
|
|
214
220
|
- spec/unit/coercible/coercer/decimal/to_float_spec.rb
|
215
221
|
- spec/unit/coercible/coercer/decimal/to_integer_spec.rb
|
216
222
|
- spec/unit/coercible/coercer/decimal/to_string_spec.rb
|
217
|
-
- spec/unit/coercible/coercer/
|
223
|
+
- spec/unit/coercible/coercer/element_reader_spec.rb
|
218
224
|
- spec/unit/coercible/coercer/false_class/coerced_predicate_spec.rb
|
219
225
|
- spec/unit/coercible/coercer/false_class/to_string_spec.rb
|
220
226
|
- spec/unit/coercible/coercer/float/coerced_predicate_spec.rb
|
@@ -242,11 +248,13 @@ test_files:
|
|
242
248
|
- spec/unit/coercible/coercer/numeric/to_integer_spec.rb
|
243
249
|
- spec/unit/coercible/coercer/numeric/to_string_spec.rb
|
244
250
|
- spec/unit/coercible/coercer/object/coerced_predicate_spec.rb
|
251
|
+
- spec/unit/coercible/coercer/object/inspect_spec.rb
|
245
252
|
- spec/unit/coercible/coercer/object/method_missing_spec.rb
|
246
253
|
- spec/unit/coercible/coercer/object/to_array_spec.rb
|
247
254
|
- spec/unit/coercible/coercer/object/to_hash_spec.rb
|
248
255
|
- spec/unit/coercible/coercer/object/to_integer_spec.rb
|
249
256
|
- spec/unit/coercible/coercer/object/to_string_spec.rb
|
257
|
+
- spec/unit/coercible/coercer/string/class_methods/config_spec.rb
|
250
258
|
- spec/unit/coercible/coercer/string/coerced_predicate_spec.rb
|
251
259
|
- spec/unit/coercible/coercer/string/to_boolean_spec.rb
|
252
260
|
- spec/unit/coercible/coercer/string/to_constant_spec.rb
|