mixture 0.2.0 → 0.3.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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/Gemfile +5 -1
  4. data/Gemfile.lock +3 -1
  5. data/README.md +25 -2
  6. data/lib/mixture/attribute.rb +6 -3
  7. data/lib/mixture/coerce/array.rb +20 -5
  8. data/lib/mixture/coerce/base.rb +62 -26
  9. data/lib/mixture/coerce/class.rb +12 -0
  10. data/lib/mixture/coerce/date.rb +10 -10
  11. data/lib/mixture/coerce/datetime.rb +10 -10
  12. data/lib/mixture/coerce/float.rb +9 -9
  13. data/lib/mixture/coerce/hash.rb +10 -4
  14. data/lib/mixture/coerce/integer.rb +9 -9
  15. data/lib/mixture/coerce/nil.rb +10 -10
  16. data/lib/mixture/coerce/object.rb +13 -13
  17. data/lib/mixture/coerce/rational.rb +9 -9
  18. data/lib/mixture/coerce/set.rb +12 -4
  19. data/lib/mixture/coerce/string.rb +9 -9
  20. data/lib/mixture/coerce/symbol.rb +4 -4
  21. data/lib/mixture/coerce/time.rb +10 -9
  22. data/lib/mixture/coerce.rb +28 -8
  23. data/lib/mixture/extensions/coercable.rb +2 -10
  24. data/lib/mixture/extensions.rb +15 -9
  25. data/lib/mixture/types/access.rb +45 -0
  26. data/lib/mixture/types/array.rb +13 -0
  27. data/lib/mixture/types/boolean.rb +20 -0
  28. data/lib/mixture/types/class.rb +16 -0
  29. data/lib/mixture/types/collection.rb +14 -0
  30. data/lib/mixture/types/date.rb +13 -0
  31. data/lib/mixture/types/datetime.rb +13 -0
  32. data/lib/mixture/types/enumerable.rb +14 -0
  33. data/lib/mixture/types/float.rb +12 -0
  34. data/lib/mixture/types/hash.rb +15 -0
  35. data/lib/mixture/types/integer.rb +12 -0
  36. data/lib/mixture/types/nil.rb +11 -0
  37. data/lib/mixture/types/numeric.rb +12 -0
  38. data/lib/mixture/types/object.rb +24 -0
  39. data/lib/mixture/types/rational.rb +13 -0
  40. data/lib/mixture/types/set.rb +16 -0
  41. data/lib/mixture/types/string.rb +12 -0
  42. data/lib/mixture/types/symbol.rb +14 -0
  43. data/lib/mixture/types/time.rb +13 -0
  44. data/lib/mixture/types/type.rb +171 -0
  45. data/lib/mixture/types.rb +110 -0
  46. data/lib/mixture/version.rb +1 -1
  47. data/lib/mixture.rb +22 -2
  48. data/mixture.gemspec +2 -3
  49. metadata +31 -37
  50. data/lib/mixture/type.rb +0 -188
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e1746012422c7e9f856706e2820d0423f404784
4
- data.tar.gz: 0591ce1acb37f9805c37b06298bf7f4c10c4f3f7
3
+ metadata.gz: 0ecfd79715637290b6e55e9bfdf26de13054e50a
4
+ data.tar.gz: 78d922593da25b673897de662496788600790516
5
5
  SHA512:
6
- metadata.gz: 6a2b4634e7d03ef8ed5e8c4730895e497bd1c98307f87f012f27ab4d4a1d401912076c39b366194d52cdeaf1f9beb2dcf673c57729b6f8f679a0656bb39280f1
7
- data.tar.gz: a15206cb105329873a6890deeb46d8aa797385835315568a427c3e98272a8d3862f3af158777a9afbe077c546621b3af42b89ac7427a1a2e5bc073558407669e
6
+ metadata.gz: 74ead02c239ae1add84099d0a2d0f688519a67871e31766550ef72ba219ef5a1c2f1d01dad6df95127cf73216e59539f2fe69b60d12ca4c7bca79105f3e3950e
7
+ data.tar.gz: f1e0670bb6a7b59daf8647b8cbf8fe88db17bd1a41a53b9944825852488a71d2f000212a9b5a0fb6cea0f944d019f857193851a45cb856ea4dd3c1d48ecd21d7
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ -m markdown
data/Gemfile CHANGED
@@ -2,5 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- # Specify your gem's dependencies in mixture.gemspec
5
+ # These are in here for reasons.
6
+ gem "pry"
7
+ gem "rubocop"
8
+ gem "coveralls"
9
+
6
10
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mixture (0.2.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 Mixture::Type[MyLibrary::MyObject]
114
+ type MyLibrary::Types::MyObject
109
115
 
110
- coerce_to(Mixture::Type::Array) do |value|
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
@@ -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
@@ -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 Type::Array
7
+ type Types::Array
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::Array, Itself)
11
- coerce_to(Type::Set, :to_set)
12
- coerce_to(Type::Hash) { |value| ::Hash[value] }
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
@@ -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 coerce
46
- # when coercion needs to happen. Note that the block is not
47
- # used as the body of the method - the method returns the
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 the value
59
- # for coercion. This block is returned by the defined
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
- def self.coerce_to(to, value = Undefined, &block)
64
- fail ArgumentError, "Expected Mixture::Type, got #{to.class}" unless
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 = case
68
- when value != Undefined
69
- value.to_proc
70
- when block_given?
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
- coercions[to] = to.method_name
77
- define_method(to.method_name) { body }
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
- "Undefined coercion of #{self.class.type} => #{type}"
129
+ fail CoercionError, "Undefined coercion #{self.class.type} " \
130
+ "=> #{type}" unless method_name
95
131
  end
96
132
 
97
133
  public_send(method_name)
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # A class coercion.
6
+ class Class < Base
7
+ type Types::Class
8
+
9
+ coerce_to(Types::Object, Itself)
10
+ end
11
+ end
12
+ end
@@ -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 Type::Date
7
+ type Types::Date
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String, :to_s)
11
- coerce_to(Type::Time, :to_time)
12
- coerce_to(Type::Date, :to_date)
13
- coerce_to(Type::DateTime, :to_datetime)
14
- coerce_to(Type::Array) { |value| value.to_time.to_a }
15
- coerce_to(Type::Float) { |value| value.to_time.to_f }
16
- coerce_to(Type::Integer) { |value| value.to_time.to_i }
17
- coerce_to(Type::Rational) { |value| value.to_time.to_r }
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 Type::DateTime
7
+ type Types::DateTime
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String, :to_s)
11
- coerce_to(Type::Time, :to_time)
12
- coerce_to(Type::Date, :to_date)
13
- coerce_to(Type::DateTime, :to_datetime)
14
- coerce_to(Type::Array) { |value| value.to_time.to_a }
15
- coerce_to(Type::Float) { |value| value.to_time.to_f }
16
- coerce_to(Type::Integer) { |value| value.to_time.to_i }
17
- coerce_to(Type::Rational) { |value| value.to_time.to_r }
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,16 +4,16 @@ module Mixture
4
4
  module Coerce
5
5
  # Handles coercion of the Float class.
6
6
  class Float < Base
7
- type Type::Float
7
+ type Types::Float
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String, :to_s)
11
- coerce_to(Type::Float, Itself)
12
- coerce_to(Type::Integer, :to_i)
13
- coerce_to(Type::Rational, :to_r)
14
- coerce_to(Type::Time) { |value| ::Time.at(value) }
15
- coerce_to(Type::Date) { |value| ::Time.at(value).to_date }
16
- coerce_to(Type::DateTime) { |value| ::Time.at(value).to_datetime }
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
@@ -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 Type::Hash
7
+ type Types::Hash
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::Hash, Itself)
11
- coerce_to(Type::Array, :to_a)
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 Type::Integer
7
+ type Types::Integer
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String, :to_s)
11
- coerce_to(Type::Float, :to_f)
12
- coerce_to(Type::Rational, :to_r)
13
- coerce_to(Type::Integer, Itself)
14
- coerce_to(Type::Time) { |value| ::Time.at(value) }
15
- coerce_to(Type::Date) { |value| ::Time.at(value).to_date }
16
- coerce_to(Type::DateTime) { |value| ::Time.at(value).to_datetime }
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
@@ -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 Type::Nil
7
+ type Types::Nil
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String) { "" }
11
- coerce_to(Type::Array) { [] }
12
- coerce_to(Type::Float) { 0.0 }
13
- coerce_to(Type::Hash) { {} }
14
- coerce_to(Type::Integer) { 0 }
15
- coerce_to(Type::Rational) { Rational(0, 1) }
16
- coerce_to(Type::Set) { Set.new }
17
- coerce_to(Type::Symbol) { :"" }
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 Type::Object
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(Type::Array, TryMethods[:to_a, :to_ary, :to_array])
25
- coerce_to(Type::Date, TryMethods[:to_date])
26
- coerce_to(Type::DateTime, TryMethods[:to_datetime])
27
- coerce_to(Type::Float, TryMethods[:to_f, :to_float])
28
- coerce_to(Type::Hash, TryMethods[:to_h, :to_hash])
29
- coerce_to(Type::Integer, TryMethods[:to_i, :to_integer])
30
- coerce_to(Type::Object, :dup)
31
- coerce_to(Type::Rational, TryMethods[:to_r, :to_rational])
32
- coerce_to(Type::Set, TryMethods[:to_set])
33
- coerce_to(Type::String, TryMethods[:to_s, :to_str, :to_string])
34
- coerce_to(Type::Symbol, TryMethods[:to_sym, :intern, :to_symbol])
35
- coerce_to(Type::Time, TryMethods[:to_time])
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 Type::Rational
7
+ type Types::Rational
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::Rational, Itself)
11
- coerce_to(Type::Time) { |value| ::Time.at(value) }
12
- coerce_to(Type::Date) { |value| ::Time.at(value).to_date }
13
- coerce_to(Type::DateTime) { |value| ::Time.at(value).to_datetime }
14
- coerce_to(Type::Float, :to_f)
15
- coerce_to(Type::Integer, :to_i)
16
- coerce_to(Type::String, :to_s)
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
@@ -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 Type::Set
7
+ type Types::Set
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::Set, :dup)
11
- coerce_to(Type::Array, :to_a)
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 Type::String
7
+ type Types::String
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String, :dup)
11
- coerce_to(Type::Symbol, :to_sym)
12
- coerce_to(Type::Integer, :to_i)
13
- coerce_to(Type::Float, :to_f)
14
- coerce_to(Type::Time) { |value| ::Time.parse(value) }
15
- coerce_to(Type::Date) { |value| ::Date.parse(value) }
16
- coerce_to(Type::DateTime) { |value| ::DateTime.parse(value) }
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 Type::Symbol
7
+ type Types::Symbol
8
8
 
9
- coerce_to(Type::Object, Itself)
10
- coerce_to(Type::String, :to_s)
11
- coerce_to(Type::Symbol, Itself)
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
@@ -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 Type::Time
7
+ type Types::Time
8
8
 
9
- coerce_to(Type::String, :to_s)
10
- coerce_to(Type::Integer, :to_i)
11
- coerce_to(Type::Float, :to_f)
12
- coerce_to(Type::Rational, :to_r)
13
- coerce_to(Type::Array, :to_a)
14
- coerce_to(Type::Time, Itself)
15
- coerce_to(Type::Date, :to_date)
16
- coerce_to(Type::DateTime, :to_datetime)
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