mixture 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/Gemfile +5 -1
- data/Gemfile.lock +3 -1
- data/README.md +25 -2
- data/lib/mixture/attribute.rb +6 -3
- data/lib/mixture/coerce/array.rb +20 -5
- data/lib/mixture/coerce/base.rb +62 -26
- data/lib/mixture/coerce/class.rb +12 -0
- data/lib/mixture/coerce/date.rb +10 -10
- data/lib/mixture/coerce/datetime.rb +10 -10
- data/lib/mixture/coerce/float.rb +9 -9
- data/lib/mixture/coerce/hash.rb +10 -4
- data/lib/mixture/coerce/integer.rb +9 -9
- data/lib/mixture/coerce/nil.rb +10 -10
- data/lib/mixture/coerce/object.rb +13 -13
- data/lib/mixture/coerce/rational.rb +9 -9
- data/lib/mixture/coerce/set.rb +12 -4
- data/lib/mixture/coerce/string.rb +9 -9
- data/lib/mixture/coerce/symbol.rb +4 -4
- data/lib/mixture/coerce/time.rb +10 -9
- data/lib/mixture/coerce.rb +28 -8
- data/lib/mixture/extensions/coercable.rb +2 -10
- data/lib/mixture/extensions.rb +15 -9
- data/lib/mixture/types/access.rb +45 -0
- data/lib/mixture/types/array.rb +13 -0
- data/lib/mixture/types/boolean.rb +20 -0
- data/lib/mixture/types/class.rb +16 -0
- data/lib/mixture/types/collection.rb +14 -0
- data/lib/mixture/types/date.rb +13 -0
- data/lib/mixture/types/datetime.rb +13 -0
- data/lib/mixture/types/enumerable.rb +14 -0
- data/lib/mixture/types/float.rb +12 -0
- data/lib/mixture/types/hash.rb +15 -0
- data/lib/mixture/types/integer.rb +12 -0
- data/lib/mixture/types/nil.rb +11 -0
- data/lib/mixture/types/numeric.rb +12 -0
- data/lib/mixture/types/object.rb +24 -0
- data/lib/mixture/types/rational.rb +13 -0
- data/lib/mixture/types/set.rb +16 -0
- data/lib/mixture/types/string.rb +12 -0
- data/lib/mixture/types/symbol.rb +14 -0
- data/lib/mixture/types/time.rb +13 -0
- data/lib/mixture/types/type.rb +171 -0
- data/lib/mixture/types.rb +110 -0
- data/lib/mixture/version.rb +1 -1
- data/lib/mixture.rb +22 -2
- data/mixture.gemspec +2 -3
- metadata +31 -37
- data/lib/mixture/type.rb +0 -188
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ecfd79715637290b6e55e9bfdf26de13054e50a
|
4
|
+
data.tar.gz: 78d922593da25b673897de662496788600790516
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74ead02c239ae1add84099d0a2d0f688519a67871e31766550ef72ba219ef5a1c2f1d01dad6df95127cf73216e59539f2fe69b60d12ca4c7bca79105f3e3950e
|
7
|
+
data.tar.gz: f1e0670bb6a7b59daf8647b8cbf8fe88db17bd1a41a53b9944825852488a71d2f000212a9b5a0fb6cea0f944d019f857193851a45cb856ea4dd3c1d48ecd21d7
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-m markdown
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mixture (0.
|
4
|
+
mixture (0.3.0)
|
5
|
+
thread_safe (~> 0.3)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
@@ -68,6 +69,7 @@ GEM
|
|
68
69
|
term-ansicolor (1.3.2)
|
69
70
|
tins (~> 1.0)
|
70
71
|
thor (0.19.1)
|
72
|
+
thread_safe (0.3.5)
|
71
73
|
tins (1.5.4)
|
72
74
|
unf (0.1.4)
|
73
75
|
unf_ext
|
data/README.md
CHANGED
@@ -103,11 +103,17 @@ Adding a coercer is also simple:
|
|
103
103
|
|
104
104
|
```ruby
|
105
105
|
module MyLibrary
|
106
|
+
module Types
|
107
|
+
class MyObject < Mixture::Types::Object
|
108
|
+
options[:primitive] = nil
|
109
|
+
constraint { |value| ... }
|
110
|
+
end
|
111
|
+
end
|
106
112
|
module Coerce
|
107
113
|
class MyObject < Mixture::Coerce::Base
|
108
|
-
type
|
114
|
+
type MyLibrary::Types::MyObject
|
109
115
|
|
110
|
-
coerce_to(Mixture::
|
116
|
+
coerce_to(Mixture::Types::Array) do |value|
|
111
117
|
value.to_a
|
112
118
|
end
|
113
119
|
end
|
@@ -115,6 +121,23 @@ module MyLibrary
|
|
115
121
|
end
|
116
122
|
```
|
117
123
|
|
124
|
+
Although the builtin coercers should do well.
|
125
|
+
|
126
|
+
A more complex example:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
module MyLibrary
|
130
|
+
class Something
|
131
|
+
include Mixture::Model
|
132
|
+
|
133
|
+
attribute :name, type: String
|
134
|
+
attribute :files, type: Array[String]
|
135
|
+
attribute :authors, type: Set[Author]
|
136
|
+
attribute :dependencies, type: Set[Dependency]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
118
141
|
## Contributing
|
119
142
|
|
120
143
|
Bug reports and pull requests are welcome on GitHub at
|
data/lib/mixture/attribute.rb
CHANGED
@@ -36,27 +36,30 @@ module Mixture
|
|
36
36
|
@list.callbacks[:update].inject(value) { |a, e| e.call(self, a) }
|
37
37
|
end
|
38
38
|
|
39
|
+
# @!attribute [r] ivar
|
39
40
|
# The instance variable for this attribute.
|
40
41
|
#
|
41
|
-
# @example
|
42
|
+
# @example Retrieve the instance variable.
|
42
43
|
# attribute.ivar # => :@name
|
43
44
|
# @return [Symbol]
|
44
45
|
def ivar
|
45
46
|
@_ivar ||= :"@#{@name}"
|
46
47
|
end
|
47
48
|
|
49
|
+
# @!attribute [r] getter
|
48
50
|
# The getter method for this attribute.
|
49
51
|
#
|
50
|
-
# @example
|
52
|
+
# @example Retrieve the getter.
|
51
53
|
# attribute.getter # => :name
|
52
54
|
# @return [Symbol]
|
53
55
|
def getter
|
54
56
|
@name
|
55
57
|
end
|
56
58
|
|
59
|
+
# @!attribute [r] setter
|
57
60
|
# The setter method for this attribute.
|
58
61
|
#
|
59
|
-
# @example
|
62
|
+
# @example Retrieve the setter.
|
60
63
|
# attribute.setter # :name=
|
61
64
|
# @return [Symbol]
|
62
65
|
def setter
|
data/lib/mixture/coerce/array.rb
CHANGED
@@ -4,12 +4,27 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Array class.
|
6
6
|
class Array < Base
|
7
|
-
type
|
7
|
+
type Types::Array
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
|
11
|
-
coerce_to(
|
12
|
-
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
|
11
|
+
coerce_to(Types::Array) do |value, type|
|
12
|
+
member = type.options.fetch(:members).first
|
13
|
+
value.map { |e| Coerce.perform(member, e) }
|
14
|
+
end
|
15
|
+
|
16
|
+
coerce_to(Types::Set) do |value, type|
|
17
|
+
member = type.options.fetch(:members).first
|
18
|
+
value.map { |e| Coerce.perform(member, e) }
|
19
|
+
end
|
20
|
+
|
21
|
+
coerce_to(Types::Hash) do |value, type|
|
22
|
+
member = type.options.fetch(:members)
|
23
|
+
::Hash[value.map do |element|
|
24
|
+
[Coerce.perform(member[0], element[0]),
|
25
|
+
Coerce.perform(member[1], element[1])]
|
26
|
+
end]
|
27
|
+
end
|
13
28
|
end
|
14
29
|
end
|
15
30
|
end
|
data/lib/mixture/coerce/base.rb
CHANGED
@@ -31,7 +31,26 @@ module Mixture
|
|
31
31
|
#
|
32
32
|
# @return [Hash{Mixture::Type => Symbol}]
|
33
33
|
def self.coercions
|
34
|
-
@_coercions ||=
|
34
|
+
@_coercions ||= ThreadSafe::Hash.new
|
35
|
+
end
|
36
|
+
|
37
|
+
# This is a method that's called by ruby interally. We're going
|
38
|
+
# to use it to hook into the coercions, to allow a class
|
39
|
+
# coercion.
|
40
|
+
#
|
41
|
+
# @param base [Class] A subclass.
|
42
|
+
# @return [void]
|
43
|
+
def self.inherited(base)
|
44
|
+
super # for Singleton
|
45
|
+
base.coerce_to(Types::Class) do |value, type|
|
46
|
+
member = type.options.fetch(:members).first
|
47
|
+
if member.respond_to?(:coerce) then member.coerce(value)
|
48
|
+
elsif member.respond_to?(:new) then member.new(value)
|
49
|
+
else
|
50
|
+
fail CoercionError, "Expected #{member} to " \
|
51
|
+
"respond to #coerce, #new"
|
52
|
+
end
|
53
|
+
end
|
35
54
|
end
|
36
55
|
|
37
56
|
# This is a DSL for the class itself. It essentially defines a
|
@@ -41,12 +60,13 @@ module Mixture
|
|
41
60
|
# This is a DSL for the class itself. It essentially defines
|
42
61
|
# a method to perform the coercion of the given type.
|
43
62
|
#
|
44
|
-
# @param to [Mixture::Type] The type to coerce to.
|
45
|
-
# @yield [value] The block is called with the value to
|
46
|
-
# when coercion needs to happen. Note that the
|
47
|
-
# used as the body of the method - the method
|
48
|
-
# block.
|
63
|
+
# @param to [Mixture::Types::Type] The type to coerce to.
|
64
|
+
# @yield [value, type] The block is called with the value to
|
65
|
+
# coerce when coercion needs to happen. Note that the
|
66
|
+
# block is not used as the body of the method - the method
|
67
|
+
# returns the block.
|
49
68
|
# @yieldparam value [Object] The object to coerce.
|
69
|
+
# @yieldparam type [Mixture::Types::Type] The destination type.
|
50
70
|
# @yieldreturn [Object] The coerced value.
|
51
71
|
# @return [void]
|
52
72
|
#
|
@@ -54,27 +74,43 @@ module Mixture
|
|
54
74
|
# This is a DSL for the class itself. It essentially defines
|
55
75
|
# a method to perform the coercion of the given type.
|
56
76
|
#
|
57
|
-
# @param to [Mixture::Type] The type to coerce to.
|
58
|
-
# @param value [Proc] The block that is called with
|
59
|
-
# for coercion. This block is returned by
|
60
|
-
# coercion method.
|
77
|
+
# @param to [Mixture::Types::Type] The type to coerce to.
|
78
|
+
# @param value [Proc, Symbol] The block that is called with
|
79
|
+
# the value for coercion. This block is returned by
|
80
|
+
# the defined coercion method. If it's a symbol, it's
|
81
|
+
# turned into a block. Note that this doesn't use
|
82
|
+
# Symbol#to_proc; it uses a similar block that ignores
|
83
|
+
# the excess paramters.
|
61
84
|
# @return [void]
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
to.is_a?(Mixture::Type)
|
85
|
+
def self.coerce_to(to, data = Undefined, &block)
|
86
|
+
fail ArgumentError, "Expected Mixture::Types::Type, got #{to}" unless
|
87
|
+
to <= Mixture::Types::Type
|
66
88
|
|
67
|
-
body =
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
block
|
72
|
-
else
|
73
|
-
fail ArgumentError, "Expected a block"
|
74
|
-
end
|
89
|
+
body = data_block(data, &block)
|
90
|
+
coercions[to] = to.options[:method]
|
91
|
+
define_method(to.options[:method]) { body }
|
92
|
+
end
|
75
93
|
|
76
|
-
|
77
|
-
|
94
|
+
# Turns a data/block given to {.coerce_to} into a block worthy
|
95
|
+
# of a body for a method.
|
96
|
+
#
|
97
|
+
# @param data [Proc, Symbol] A proc/symbol to be used for a
|
98
|
+
# method.
|
99
|
+
# @yield (see .coerce_to)
|
100
|
+
# @yieldparam (see .coerce_to)
|
101
|
+
# @yieldreturn (see .coerce_to)
|
102
|
+
# @return [void]
|
103
|
+
def self.data_block(data, &block)
|
104
|
+
case
|
105
|
+
when data.is_a?(::Symbol)
|
106
|
+
proc { |value| value.public_send(data) }
|
107
|
+
when data.is_a?(::Proc)
|
108
|
+
data
|
109
|
+
when block_given?
|
110
|
+
block
|
111
|
+
else
|
112
|
+
fail ArgumentError, "Expected a block, got #{data.inspect}"
|
113
|
+
end
|
78
114
|
end
|
79
115
|
|
80
116
|
# (see #to)
|
@@ -90,8 +126,8 @@ module Mixture
|
|
90
126
|
# @return [Proc{(Object) => Object}]
|
91
127
|
def to(type)
|
92
128
|
method_name = self.class.coercions.fetch(type) do
|
93
|
-
fail CoercionError,
|
94
|
-
|
129
|
+
fail CoercionError, "Undefined coercion #{self.class.type} " \
|
130
|
+
"=> #{type}" unless method_name
|
95
131
|
end
|
96
132
|
|
97
133
|
public_send(method_name)
|
data/lib/mixture/coerce/date.rb
CHANGED
@@ -4,17 +4,17 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Date class.
|
6
6
|
class Date < Base
|
7
|
-
type
|
7
|
+
type Types::Date
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
17
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :to_s)
|
11
|
+
coerce_to(Types::Time, :to_time)
|
12
|
+
coerce_to(Types::Date, :to_date)
|
13
|
+
coerce_to(Types::DateTime, :to_datetime)
|
14
|
+
coerce_to(Types::Array) { |value| value.to_time.to_a }
|
15
|
+
coerce_to(Types::Float) { |value| value.to_time.to_f }
|
16
|
+
coerce_to(Types::Integer) { |value| value.to_time.to_i }
|
17
|
+
coerce_to(Types::Rational) { |value| value.to_time.to_r }
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -4,17 +4,17 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the DateTime class.
|
6
6
|
class DateTime < Base
|
7
|
-
type
|
7
|
+
type Types::DateTime
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
17
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :to_s)
|
11
|
+
coerce_to(Types::Time, :to_time)
|
12
|
+
coerce_to(Types::Date, :to_date)
|
13
|
+
coerce_to(Types::DateTime, :to_datetime)
|
14
|
+
coerce_to(Types::Array) { |value| value.to_time.to_a }
|
15
|
+
coerce_to(Types::Float) { |value| value.to_time.to_f }
|
16
|
+
coerce_to(Types::Integer) { |value| value.to_time.to_i }
|
17
|
+
coerce_to(Types::Rational) { |value| value.to_time.to_r }
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/mixture/coerce/float.rb
CHANGED
@@ -4,16 +4,16 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Float class.
|
6
6
|
class Float < Base
|
7
|
-
type
|
7
|
+
type Types::Float
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :to_s)
|
11
|
+
coerce_to(Types::Float, Itself)
|
12
|
+
coerce_to(Types::Integer, :to_i)
|
13
|
+
coerce_to(Types::Rational, :to_r)
|
14
|
+
coerce_to(Types::Time) { |value| ::Time.at(value) }
|
15
|
+
coerce_to(Types::Date) { |value| ::Time.at(value).to_date }
|
16
|
+
coerce_to(Types::DateTime) { |value| ::Time.at(value).to_datetime }
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/mixture/coerce/hash.rb
CHANGED
@@ -4,11 +4,17 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Hash class.
|
6
6
|
class Hash < Base
|
7
|
-
type
|
7
|
+
type Types::Hash
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
|
11
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
|
11
|
+
coerce_to(Types::Hash) do |value, type|
|
12
|
+
members = type.options.fetch(:members)
|
13
|
+
::Hash[value.map do |k, v|
|
14
|
+
[Coerce.perform(members[0], k),
|
15
|
+
Coerce.perform(members[1], v)]
|
16
|
+
end]
|
17
|
+
end
|
12
18
|
end
|
13
19
|
end
|
14
20
|
end
|
@@ -4,16 +4,16 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Integer class.
|
6
6
|
class Integer < Base
|
7
|
-
type
|
7
|
+
type Types::Integer
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :to_s)
|
11
|
+
coerce_to(Types::Float, :to_f)
|
12
|
+
coerce_to(Types::Rational, :to_r)
|
13
|
+
coerce_to(Types::Integer, Itself)
|
14
|
+
coerce_to(Types::Time) { |value| ::Time.at(value) }
|
15
|
+
coerce_to(Types::Date) { |value| ::Time.at(value).to_date }
|
16
|
+
coerce_to(Types::DateTime) { |value| ::Time.at(value).to_datetime }
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/mixture/coerce/nil.rb
CHANGED
@@ -4,17 +4,17 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Nil class.
|
6
6
|
class Nil < Base
|
7
|
-
type
|
7
|
+
type Types::Nil
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
17
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String) { "" }
|
11
|
+
coerce_to(Types::Array) { [] }
|
12
|
+
coerce_to(Types::Float) { 0.0 }
|
13
|
+
coerce_to(Types::Hash) { {} }
|
14
|
+
coerce_to(Types::Integer) { 0 }
|
15
|
+
coerce_to(Types::Rational) { Rational(0, 1) }
|
16
|
+
coerce_to(Types::Set) { Set.new }
|
17
|
+
coerce_to(Types::Symbol) { :"" }
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -4,7 +4,7 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Object class.
|
6
6
|
class Object < Base
|
7
|
-
type
|
7
|
+
type Types::Object
|
8
8
|
|
9
9
|
# Tries a set of methods on the object, before failing with a
|
10
10
|
# coercion error.
|
@@ -21,18 +21,18 @@ module Mixture
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
coerce_to(
|
25
|
-
coerce_to(
|
26
|
-
coerce_to(
|
27
|
-
coerce_to(
|
28
|
-
coerce_to(
|
29
|
-
coerce_to(
|
30
|
-
coerce_to(
|
31
|
-
coerce_to(
|
32
|
-
coerce_to(
|
33
|
-
coerce_to(
|
34
|
-
coerce_to(
|
35
|
-
coerce_to(
|
24
|
+
coerce_to(Types::Object, Itself)
|
25
|
+
coerce_to(Types::Array, TryMethods[:to_a, :to_ary, :to_array])
|
26
|
+
coerce_to(Types::Date, TryMethods[:to_date])
|
27
|
+
coerce_to(Types::DateTime, TryMethods[:to_datetime])
|
28
|
+
coerce_to(Types::Float, TryMethods[:to_f, :to_float])
|
29
|
+
coerce_to(Types::Hash, TryMethods[:to_h, :to_hash])
|
30
|
+
coerce_to(Types::Integer, TryMethods[:to_i, :to_integer])
|
31
|
+
coerce_to(Types::Rational, TryMethods[:to_r, :to_rational])
|
32
|
+
coerce_to(Types::Set, TryMethods[:to_set])
|
33
|
+
coerce_to(Types::String, TryMethods[:to_s, :to_str, :to_string])
|
34
|
+
coerce_to(Types::Symbol, TryMethods[:to_sym, :intern, :to_symbol])
|
35
|
+
coerce_to(Types::Time, TryMethods[:to_time])
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -4,16 +4,16 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Rational class.
|
6
6
|
class Rational < Base
|
7
|
-
type
|
7
|
+
type Types::Rational
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::Rational, Itself)
|
11
|
+
coerce_to(Types::Time) { |value| ::Time.at(value) }
|
12
|
+
coerce_to(Types::Date) { |value| ::Time.at(value).to_date }
|
13
|
+
coerce_to(Types::DateTime) { |value| ::Time.at(value).to_datetime }
|
14
|
+
coerce_to(Types::Float, :to_f)
|
15
|
+
coerce_to(Types::Integer, :to_i)
|
16
|
+
coerce_to(Types::String, :to_s)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/mixture/coerce/set.rb
CHANGED
@@ -4,11 +4,19 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Set class.
|
6
6
|
class Set < Base
|
7
|
-
type
|
7
|
+
type Types::Set
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
|
11
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
|
11
|
+
coerce_to(Types::Set) do |value, type|
|
12
|
+
member = type.options.fetch(:members).first
|
13
|
+
value.map { |e| Coerce.perform(member, e) }.to_set
|
14
|
+
end
|
15
|
+
|
16
|
+
coerce_to(Types::Array) do |value, type|
|
17
|
+
member = type.options.fetch(:members).first
|
18
|
+
value.map { |e| Coerce.perform(member, e) }.to_a
|
19
|
+
end
|
12
20
|
end
|
13
21
|
end
|
14
22
|
end
|
@@ -4,16 +4,16 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the String class.
|
6
6
|
class String < Base
|
7
|
-
type
|
7
|
+
type Types::String
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :dup)
|
11
|
+
coerce_to(Types::Symbol, :to_sym)
|
12
|
+
coerce_to(Types::Integer, :to_i)
|
13
|
+
coerce_to(Types::Float, :to_f)
|
14
|
+
coerce_to(Types::Time) { |value| ::Time.parse(value) }
|
15
|
+
coerce_to(Types::Date) { |value| ::Date.parse(value) }
|
16
|
+
coerce_to(Types::DateTime) { |value| ::DateTime.parse(value) }
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -4,11 +4,11 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Numeric class.
|
6
6
|
class Symbol < Base
|
7
|
-
type
|
7
|
+
type Types::Symbol
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :to_s)
|
11
|
+
coerce_to(Types::Symbol, Itself)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
data/lib/mixture/coerce/time.rb
CHANGED
@@ -4,16 +4,17 @@ module Mixture
|
|
4
4
|
module Coerce
|
5
5
|
# Handles coercion of the Time class.
|
6
6
|
class Time < Base
|
7
|
-
type
|
7
|
+
type Types::Time
|
8
8
|
|
9
|
-
coerce_to(
|
10
|
-
coerce_to(
|
11
|
-
coerce_to(
|
12
|
-
coerce_to(
|
13
|
-
coerce_to(
|
14
|
-
coerce_to(
|
15
|
-
coerce_to(
|
16
|
-
coerce_to(
|
9
|
+
coerce_to(Types::Object, Itself)
|
10
|
+
coerce_to(Types::String, :to_s)
|
11
|
+
coerce_to(Types::Integer, :to_i)
|
12
|
+
coerce_to(Types::Float, :to_f)
|
13
|
+
coerce_to(Types::Rational, :to_r)
|
14
|
+
coerce_to(Types::Array, :to_a)
|
15
|
+
coerce_to(Types::Time, Itself)
|
16
|
+
coerce_to(Types::Date, :to_date)
|
17
|
+
coerce_to(Types::DateTime, :to_datetime)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|