mixture 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/mixture/types/array.rb +1 -0
- data/lib/mixture/types/boolean.rb +1 -0
- data/lib/mixture/types/class.rb +10 -0
- data/lib/mixture/types/date.rb +1 -0
- data/lib/mixture/types/datetime.rb +1 -0
- data/lib/mixture/types/float.rb +1 -0
- data/lib/mixture/types/hash.rb +1 -0
- data/lib/mixture/types/integer.rb +1 -0
- data/lib/mixture/types/nil.rb +1 -0
- data/lib/mixture/types/object.rb +15 -0
- data/lib/mixture/types/range.rb +1 -0
- data/lib/mixture/types/rational.rb +1 -0
- data/lib/mixture/types/set.rb +1 -0
- data/lib/mixture/types/string.rb +1 -0
- data/lib/mixture/types/symbol.rb +1 -0
- data/lib/mixture/types/time.rb +1 -0
- data/lib/mixture/types/type.rb +8 -1
- data/lib/mixture/types.rb +4 -1
- data/lib/mixture/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfc27126b02c841b844840b39021ef62be841b1e
|
4
|
+
data.tar.gz: 29d1ec9b68e85b1d6968552626d5b66c26256753
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88f9b2ad44278155c2f528e144f5486686099a75a5a9ce0ce040cbae473e0d2835133f3bb843da0486190b6db0023a76b17505277fed06310cfdec651c406b46
|
7
|
+
data.tar.gz: a0e1457884b2a9bd58a29dc20f4b5e9381dd55f0df52df1c8931b06dd19e3ca09e0b7bbe76a768ccccfd012847e20fb20b0fe5077531cfad6dce839e558dac99
|
data/Gemfile.lock
CHANGED
data/lib/mixture/types/array.rb
CHANGED
data/lib/mixture/types/class.rb
CHANGED
@@ -5,12 +5,22 @@ module Mixture
|
|
5
5
|
# A class type. This can be subtyped, and is subtyped for
|
6
6
|
# non-primitive classes.
|
7
7
|
class Class < Object
|
8
|
+
register
|
8
9
|
options[:primitive] = ::Class
|
9
10
|
options[:noinfer] = true
|
10
11
|
options[:members] = [Object]
|
11
12
|
options[:method] = :to_class
|
12
13
|
options[:types] = ThreadSafe::Cache.new
|
13
14
|
extend Access
|
15
|
+
|
16
|
+
constraints.clear
|
17
|
+
constraint do |value|
|
18
|
+
if value.is_a?(::Class)
|
19
|
+
options.fetch(:members).first == value
|
20
|
+
else
|
21
|
+
options.fetch(:members).first === value
|
22
|
+
end
|
23
|
+
end
|
14
24
|
end
|
15
25
|
end
|
16
26
|
end
|
data/lib/mixture/types/date.rb
CHANGED
data/lib/mixture/types/float.rb
CHANGED
data/lib/mixture/types/hash.rb
CHANGED
data/lib/mixture/types/nil.rb
CHANGED
data/lib/mixture/types/object.rb
CHANGED
@@ -6,6 +6,7 @@ module Mixture
|
|
6
6
|
# inherit from Object) have; i.e., it must be an object, and
|
7
7
|
# it must be the type's primitive.
|
8
8
|
class Object < Type
|
9
|
+
register
|
9
10
|
options[:primitive] = ::Object
|
10
11
|
options[:method] = :to_object
|
11
12
|
as :object
|
@@ -33,6 +34,20 @@ module Mixture
|
|
33
34
|
::Object === value
|
34
35
|
# rubocop:enable Style/CaseEquality
|
35
36
|
end
|
37
|
+
|
38
|
+
# We can't match anything as an object that isn't an object.
|
39
|
+
constraint do |value|
|
40
|
+
# The first constraint is that the class cannot be an object.
|
41
|
+
# This is the same as removing this constraint on inherited
|
42
|
+
# classes.
|
43
|
+
# The second constraint is that if the class is an object,
|
44
|
+
# then the value's class must be object.
|
45
|
+
# This is so we can properly infer objects, since it's likely
|
46
|
+
# _someone_'s gonna throw us an object.
|
47
|
+
self != Object ||
|
48
|
+
(self == Object && value.class == ::Object)
|
49
|
+
end
|
50
|
+
|
36
51
|
constraint { |value| value.is_a?(options[:primitive]) }
|
37
52
|
end
|
38
53
|
end
|
data/lib/mixture/types/range.rb
CHANGED
data/lib/mixture/types/set.rb
CHANGED
data/lib/mixture/types/string.rb
CHANGED
data/lib/mixture/types/symbol.rb
CHANGED
data/lib/mixture/types/time.rb
CHANGED
data/lib/mixture/types/type.rb
CHANGED
@@ -49,11 +49,18 @@ module Mixture
|
|
49
49
|
# @param sub [Class] The new subclass.
|
50
50
|
# @return [void]
|
51
51
|
def self.inherited(sub)
|
52
|
-
Types.types << sub unless sub.anonymous?
|
53
52
|
sub.options.merge!(options)
|
54
53
|
sub.constraints.concat(constraints)
|
55
54
|
end
|
56
55
|
|
56
|
+
# Registers the type. This shouldn't be called on anonymous
|
57
|
+
# classes.
|
58
|
+
#
|
59
|
+
# @return [void]
|
60
|
+
def self.register
|
61
|
+
Types.types << self
|
62
|
+
end
|
63
|
+
|
57
64
|
# Checks if the given value passes all of the constraints
|
58
65
|
# defined on this type. Each constraint is executed within the
|
59
66
|
# context of the class, to provide access to {.options}.
|
data/lib/mixture/types.rb
CHANGED
@@ -82,7 +82,10 @@ module Mixture
|
|
82
82
|
case object
|
83
83
|
when ::Array then Array[object.first]
|
84
84
|
when ::Set then Set[object.first]
|
85
|
-
|
85
|
+
when ::Hash then Hash[object.keys.first => object.values.first]
|
86
|
+
else
|
87
|
+
types.find { |type| type.matches?(object) } ||
|
88
|
+
infer_class(object.class)
|
86
89
|
end
|
87
90
|
end
|
88
91
|
end
|
data/lib/mixture/version.rb
CHANGED