literal 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/literal.rb CHANGED
@@ -3,33 +3,114 @@
3
3
  module Literal
4
4
  TYPE_CHECKS_DISABLED = ENV["LITERAL_TYPE_CHECKS"] == "false"
5
5
 
6
+ autoload :Array, "literal/array"
6
7
  autoload :Data, "literal/data"
7
8
  autoload :DataProperty, "literal/data_property"
8
9
  autoload :DataStructure, "literal/data_structure"
9
10
  autoload :Enum, "literal/enum"
11
+ autoload :Flags, "literal/flags"
12
+ autoload :Flags16, "literal/flags"
13
+ autoload :Flags32, "literal/flags"
14
+ autoload :Flags64, "literal/flags"
15
+ autoload :Flags8, "literal/flags"
16
+ autoload :Hash, "literal/hash"
10
17
  autoload :Null, "literal/null"
11
18
  autoload :Object, "literal/object"
12
19
  autoload :Properties, "literal/properties"
13
20
  autoload :Property, "literal/property"
21
+ autoload :Set, "literal/set"
14
22
  autoload :Struct, "literal/struct"
23
+ autoload :Type, "literal/type"
15
24
  autoload :Types, "literal/types"
16
- autoload :Flags, "literal/flags"
17
- autoload :Flags8, "literal/flags"
18
- autoload :Flags16, "literal/flags"
19
- autoload :Flags32, "literal/flags"
20
- autoload :Flags64, "literal/flags"
21
25
 
22
26
  # Errors
23
27
  autoload :Error, "literal/errors/error"
24
28
  autoload :TypeError, "literal/errors/type_error"
25
29
  autoload :ArgumentError, "literal/errors/argument_error"
26
30
 
31
+ TRANSFORMS = {
32
+ Integer => {
33
+ abs: Integer,
34
+ ceil: Integer,
35
+ chr: String,
36
+ denominator: Integer,
37
+ even?: Types::BooleanType::Instance,
38
+ floor: Integer,
39
+ hash: Integer,
40
+ inspect: String,
41
+ integer?: true,
42
+ magnitude: Integer,
43
+ negative?: Types::BooleanType::Instance,
44
+ next: Integer,
45
+ nonzero?: Types::BooleanType::Instance,
46
+ numerator: Integer,
47
+ odd?: Types::BooleanType::Instance,
48
+ ord: Integer,
49
+ positive?: Types::BooleanType::Instance,
50
+ pred: Integer,
51
+ round: Integer,
52
+ size: Integer,
53
+ succ: Integer,
54
+ to_f: Float,
55
+ to_i: Integer,
56
+ to_int: Integer,
57
+ to_r: Rational,
58
+ to_s: String,
59
+ truncate: Integer,
60
+ zero?: Types::BooleanType::Instance,
61
+ },
62
+ String => {
63
+ ascii_only?: Types::BooleanType::Instance,
64
+ bytesize: Integer,
65
+ capitalize: String,
66
+ chomp: String,
67
+ chop: String,
68
+ downcase: String,
69
+ dump: String,
70
+ empty?: Types::BooleanType::Instance,
71
+ hash: Integer,
72
+ inspect: String,
73
+ length: Integer,
74
+ lstrip: String,
75
+ ord: Integer,
76
+ reverse: String,
77
+ rstrip: String,
78
+ scrub: String,
79
+ size: Integer,
80
+ strip: String,
81
+ swapcase: String,
82
+ to_str: String,
83
+ upcase: String,
84
+ valid_encoding?: Types::BooleanType::Instance,
85
+ },
86
+ Array => {
87
+ size: Integer,
88
+ length: Integer,
89
+ empty?: Types::BooleanType::Instance,
90
+ sort: Array,
91
+ to_a: Array,
92
+ to_ary: Array,
93
+ },
94
+ }.transform_values! { |it| it.transform_keys(&:to_proc) }.freeze
95
+
27
96
  def self.Enum(type)
28
97
  Class.new(Literal::Enum) do
29
98
  prop :value, type, :positional
30
99
  end
31
100
  end
32
101
 
102
+ def self.Array(type)
103
+ Literal::Array::Generic.new(type)
104
+ end
105
+
106
+ def self.Set(type)
107
+ Literal::Set::Generic.new(type)
108
+ end
109
+
110
+ def self.Hash(key_type, value_type)
111
+ Literal::Hash::Generic.new(key_type, value_type)
112
+ end
113
+
33
114
  def self.check(actual:, expected:)
34
115
  if expected === actual
35
116
  true
@@ -40,6 +121,17 @@ module Literal
40
121
  raise Literal::TypeError.new(context:)
41
122
  end
42
123
  end
124
+
125
+ def self.subtype?(type, of:)
126
+ (of == type) || case of
127
+ when Literal::Type, Module
128
+ of >= type
129
+ when Range
130
+ of.cover?(type)
131
+ else
132
+ false
133
+ end
134
+ end
43
135
  end
44
136
 
45
137
  require_relative "literal/rails" if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: literal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-07 00:00:00.000000000 Z
11
+ date: 2024-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A literal Ruby gem.
14
14
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - LICENSE.txt
21
21
  - README.md
22
22
  - lib/literal.rb
23
+ - lib/literal/array.rb
23
24
  - lib/literal/data.rb
24
25
  - lib/literal/data_property.rb
25
26
  - lib/literal/data_structure.rb
@@ -28,6 +29,7 @@ files:
28
29
  - lib/literal/errors/error.rb
29
30
  - lib/literal/errors/type_error.rb
30
31
  - lib/literal/flags.rb
32
+ - lib/literal/hash.rb
31
33
  - lib/literal/null.rb
32
34
  - lib/literal/object.rb
33
35
  - lib/literal/properties.rb
@@ -37,9 +39,12 @@ files:
37
39
  - lib/literal/rails.rb
38
40
  - lib/literal/rails/enum_serializer.rb
39
41
  - lib/literal/rails/enum_type.rb
42
+ - lib/literal/rails/flags_type.rb
40
43
  - lib/literal/rails/patches/active_record.rb
41
44
  - lib/literal/railtie.rb
45
+ - lib/literal/set.rb
42
46
  - lib/literal/struct.rb
47
+ - lib/literal/type.rb
43
48
  - lib/literal/types.rb
44
49
  - lib/literal/types/any_type.rb
45
50
  - lib/literal/types/array_type.rb