mixture 0.4.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdcfd77ffa0853daa936e5a7e7c10d18dcc12bd6
4
- data.tar.gz: acb4173454fe8c014f33d6b43f489f9cba7338f4
3
+ metadata.gz: cfc27126b02c841b844840b39021ef62be841b1e
4
+ data.tar.gz: 29d1ec9b68e85b1d6968552626d5b66c26256753
5
5
  SHA512:
6
- metadata.gz: fb75c08641030b0c5acc22a1a51bc21cdb122fde7a3cdd5d8ffd6b19f1ae2014cf56bddc46cda61239c8c65baeb1e4ea2c2cf2f8c7cf6f5f5ac1ccd7f593ecc4
7
- data.tar.gz: e425fa998b3198e7f9c43123a757700de9635063505c5ca6b588c58aecc6696d41ee1f48bdf70c7b781c47006f55e29d62bf406bf5d2abb0fafa947fb0a7ec75
6
+ metadata.gz: 88f9b2ad44278155c2f528e144f5486686099a75a5a9ce0ce040cbae473e0d2835133f3bb843da0486190b6db0023a76b17505277fed06310cfdec651c406b46
7
+ data.tar.gz: a0e1457884b2a9bd58a29dc20f4b5e9381dd55f0df52df1c8931b06dd19e3ca09e0b7bbe76a768ccccfd012847e20fb20b0fe5077531cfad6dce839e558dac99
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mixture (0.4.0)
4
+ mixture (0.5.0)
5
5
  thread_safe (~> 0.3)
6
6
 
7
7
  GEM
@@ -5,6 +5,7 @@ module Mixture
5
5
  # The array type. This is a collection, and as such, can be used
6
6
  # with the `.[]` accessor.
7
7
  class Array < Collection
8
+ register
8
9
  options[:primitive] = ::Array
9
10
  options[:method] = :to_array
10
11
  as :array
@@ -8,6 +8,7 @@ module Mixture
8
8
  # odd stuff from happening. It can still be matched by other
9
9
  # values.
10
10
  class Boolean < Object
11
+ register
11
12
  options[:primitive] = nil
12
13
  as :bool, :boolean, true, false
13
14
 
@@ -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
@@ -5,6 +5,7 @@ module Mixture
5
5
  # A date type. I don't know why ruby has Date, DateTime, and
6
6
  # Time, but someone thinks we need it.
7
7
  class Date < Object
8
+ register
8
9
  options[:primitive] = ::Date
9
10
  options[:method] = :to_date
10
11
  as :date
@@ -5,6 +5,7 @@ module Mixture
5
5
  # A datetime type. I don't know why Ruby has a Date, DateTime, and
6
6
  # Time type, but someone thinks we needs it.
7
7
  class DateTime < Object
8
+ register
8
9
  options[:primitive] = ::DateTime
9
10
  options[:method] = :to_datetime
10
11
  as :datetime
@@ -4,6 +4,7 @@ module Mixture
4
4
  module Types
5
5
  # A float. Not much to say here.
6
6
  class Float < Numeric
7
+ register
7
8
  options[:primitive] = ::Float
8
9
  options[:method] = :to_float
9
10
  as :float
@@ -5,6 +5,7 @@ module Mixture
5
5
  # A hash. This is also accessable, and expects two member types;
6
6
  # one for the keys, and one for the values.
7
7
  class Hash < Object
8
+ register
8
9
  options[:primitive] = ::Hash
9
10
  options[:members] = [Object, Object]
10
11
  options[:method] = :to_hash
@@ -4,6 +4,7 @@ module Mixture
4
4
  module Types
5
5
  # An integer. Not much to say here.
6
6
  class Integer < Numeric
7
+ register
7
8
  options[:primitive] = ::Integer
8
9
  options[:method] = :to_integer
9
10
  as :int, :integer
@@ -4,6 +4,7 @@ module Mixture
4
4
  module Types
5
5
  # Represents a nil type. This has no coercions.
6
6
  class Nil < Object
7
+ register
7
8
  options[:primitive] = ::NilClass
8
9
  as nil
9
10
  end
@@ -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
@@ -4,6 +4,7 @@ module Mixture
4
4
  module Types
5
5
  # The range type.
6
6
  class Range < Enumerable
7
+ register
7
8
  options[:primitive] = ::Range
8
9
  options[:method] = :to_range
9
10
  as :range
@@ -5,6 +5,7 @@ module Mixture
5
5
  # A rational. I've personally never used this, but I don't see it
6
6
  # as a bad thing.
7
7
  class Rational < Numeric
8
+ register
8
9
  options[:primitive] = ::Rational
9
10
  options[:method] = :to_rational
10
11
  as :rational
@@ -8,6 +8,7 @@ module Mixture
8
8
  # @see Array
9
9
  # @see http://ruby-doc.org/stdlib/libdoc/set/rdoc/Set.html
10
10
  class Set < Collection
11
+ register
11
12
  options[:primitive] = ::Set
12
13
  options[:method] = :to_set
13
14
  as :set
@@ -4,6 +4,7 @@ module Mixture
4
4
  module Types
5
5
  # A string.
6
6
  class String < Object
7
+ register
7
8
  options[:primitive] = ::String
8
9
  options[:method] = :to_string
9
10
  as :str, :string
@@ -6,6 +6,7 @@ module Mixture
6
6
  # they added garbage collection for symbols; however, it is still
7
7
  # not a brilliant idea to turn user input into symbols.
8
8
  class Symbol < Object
9
+ register
9
10
  options[:primitive] = ::Symbol
10
11
  options[:method] = :to_symbol
11
12
  as :symbol
@@ -5,6 +5,7 @@ module Mixture
5
5
  # A time type. I'm not sure why Ruby has a Date, DateTime, and
6
6
  # Time object, but someone thinks we need it.
7
7
  class Time < Object
8
+ register
8
9
  options[:primitive] = ::Time
9
10
  options[:method] = :to_time
10
11
  as :time
@@ -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
- else types.reverse.find { |type| type.matches?(object) }
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
@@ -5,5 +5,5 @@ module Mixture
5
5
  # The current version of Mixture.
6
6
  #
7
7
  # @return [String]
8
- VERSION = "0.4.0"
8
+ VERSION = "0.5.0"
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Rodi