shale-builder 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fcf37277027a3ba39d0df88fe7e7b1b974b02c71b243e33b602fafcd2559198b
4
- data.tar.gz: 79edd9b585d523eb892befd7a0afa7ebb0721a7b614dc24b5d6849119226e8a9
3
+ metadata.gz: 01baf7e6e2a1bb3af915935d777089427a91fb25bf238bb1103fe7be133bea47
4
+ data.tar.gz: 7bb534cd32cf4681d90f25658c4bd659e22f1173d21434f06e39997cfb12188f
5
5
  SHA512:
6
- metadata.gz: 182b5b46d26fa1d9d0a988d3d74207769da752781afa1b3583bc25abc8ae7b3de52d9b921549ee171ed8f666ea45a0d897085919d2c21b40d24c39128e30f00b
7
- data.tar.gz: 32ada87010a6307cc1e3c7749e06624c77594cb10a280d1a4b9c8d05e2799ee291099c62e71817feabfb8de38ac8bdc025791b769aa8b6e1a67aa753c20f55f6
6
+ metadata.gz: 11ea61b420ba7fd7c24412e570344b5e1f57a4385f33884c2c0ad6118f5856d4d45a58903872d0c10c1c28af8f3f5f3d9481875817fb102f0bc135159f635efa
7
+ data.tar.gz: 2fe221a51291db87700f2a0d5e5777924c28d9e93527d3da8f9f0548592a9bfc5d5ae1d91819348c559d8bfcae088b80aa0dd36e9b14627f3df40aaff54c4b30
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shale-builder (0.2.2)
4
+ shale-builder (0.2.3)
5
5
  booleans (>= 0.1)
6
6
  shale (< 2.0)
7
7
  sorbet-runtime (> 0.5)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Shale
4
4
  module Builder
5
- VERSION = '0.2.2'
5
+ VERSION = '0.2.3'
6
6
  end
7
7
  end
@@ -37,33 +37,40 @@ module Tapioca
37
37
  attribute_names = constant.attributes.keys.sort
38
38
  attribute_names.each do |attribute_name|
39
39
  attribute = T.let(constant.attributes[attribute_name], ::Shale::Attribute)
40
- non_nilable_type, nilable_type = shale_type_to_sorbet_return_type(attribute)
41
- type = nilable_type
42
- if attribute.collection?
43
- type = "T.nilable(T::Array[#{non_nilable_type}])"
44
- end
40
+ return_type, nilable = shale_type_to_sorbet_return_type(attribute)
45
41
  comments = T.let([], T::Array[RBI::Comment])
46
42
  if shale_builder_defined? && attribute.doc
47
43
  comments << RBI::Comment.new(T.must(attribute.doc))
48
44
  end
49
45
 
46
+ if attribute.collection?
47
+ getter_without_block_type = "T.nilable(T::Array[#{return_type}])"
48
+ elsif nilable
49
+ getter_without_block_type = "T.nilable(#{return_type})"
50
+ else
51
+ getter_without_block_type = return_type.to_s
52
+ end
53
+
50
54
  if has_shale_builder && attribute.type < ::Shale::Mapper
51
- generate_mapper_getter(mod, attribute.name, type, non_nilable_type, comments)
55
+ generate_mapper_getter(mod, attribute.name, return_type, getter_without_block_type, comments)
52
56
  else
53
- mod.create_method(attribute.name, return_type: type, comments: comments)
57
+ mod.create_method(attribute.name, return_type: getter_without_block_type, comments: comments)
54
58
  end
55
59
 
56
- non_nilable_type, nilable_type = shale_type_to_sorbet_setter_type(attribute)
57
- type = nilable_type
60
+ setter_type, nilable = shale_type_to_sorbet_setter_type(attribute)
58
61
  if attribute.collection?
59
- type = "T.nilable(T::Array[#{non_nilable_type}])"
62
+ setter_type = "T.nilable(T::Array[#{return_type}])"
63
+ elsif nilable
64
+ setter_type = "T.nilable(#{return_type})"
65
+ else
66
+ setter_type = return_type.to_s
60
67
  end
61
68
 
62
69
  # setter
63
70
  mod.create_method(
64
71
  "#{attribute.name}=",
65
- parameters: [create_param('value', type: type)],
66
- return_type: type,
72
+ parameters: [create_param('value', type: setter_type)],
73
+ return_type: setter_type,
67
74
  comments: comments,
68
75
  )
69
76
  end
@@ -75,24 +82,25 @@ module Tapioca
75
82
  params(
76
83
  mod: RBI::Scope,
77
84
  method_name: String,
78
- type: String,
79
- non_nilable_type: String,
85
+ type: Object,
86
+ getter_without_block_type: String,
80
87
  comments: T::Array[RBI::Comment],
81
88
  ).void
82
89
  end
83
- def generate_mapper_getter(mod, method_name, type, non_nilable_type, comments)
90
+ def generate_mapper_getter(mod, method_name, type, getter_without_block_type, comments)
84
91
  if mod.respond_to?(:create_sig)
85
92
  # for tapioca < 0.16.0
86
93
  sigs = T.let([], T::Array[RBI::Sig])
94
+
87
95
  # simple getter
88
96
  sigs << mod.create_sig(
89
97
  parameters: { block: 'NilClass' },
90
- return_type: type,
98
+ return_type: getter_without_block_type,
91
99
  )
92
100
  # getter with block
93
101
  sigs << mod.create_sig(
94
- parameters: { block: "T.proc.params(arg0: #{non_nilable_type}).void" },
95
- return_type: non_nilable_type
102
+ parameters: { block: "T.proc.params(arg0: #{type}).void" },
103
+ return_type: type.to_s
96
104
  )
97
105
  mod.create_method_with_sigs(
98
106
  method_name,
@@ -107,12 +115,12 @@ module Tapioca
107
115
 
108
116
  method.add_sig do |sig|
109
117
  sig.add_param('block', 'NilClass')
110
- sig.return_type = type
118
+ sig.return_type = getter_without_block_type
111
119
  end
112
120
 
113
121
  method.add_sig do |sig|
114
- sig.add_param('block', "T.proc.params(arg0: #{non_nilable_type}).void")
115
- sig.return_type = non_nilable_type
122
+ sig.add_param('block', "T.proc.params(arg0: #{type}).void")
123
+ sig.return_type = type.to_s
116
124
  end
117
125
  end
118
126
  end
@@ -130,7 +138,22 @@ module Tapioca
130
138
  sig { returns(T::Boolean) }
131
139
  def shale_builder_defined? = Boolean(defined?(::Shale::Builder))
132
140
 
133
- SHALE_TYPES_MAP = T.let(
141
+ # Maps Shale return types to Sorbet types
142
+ SHALE_RETURN_TYPES_MAP = T.let(
143
+ {
144
+ ::Shale::Type::Value => Object,
145
+ ::Shale::Type::String => String,
146
+ ::Shale::Type::Float => Float,
147
+ ::Shale::Type::Integer => Integer,
148
+ ::Shale::Type::Time => Time,
149
+ ::Shale::Type::Date => Date,
150
+ ::Shale::Type::Boolean => T::Boolean,
151
+ }.freeze,
152
+ T::Hash[Class, Object],
153
+ )
154
+
155
+ # Maps Shale setter types to Sorbet types
156
+ SHALE_SETTER_TYPES_MAP = T.let(
134
157
  {
135
158
  ::Shale::Type::Value => Object,
136
159
  ::Shale::Type::String => String,
@@ -138,46 +161,47 @@ module Tapioca
138
161
  ::Shale::Type::Integer => Integer,
139
162
  ::Shale::Type::Time => Time,
140
163
  ::Shale::Type::Date => Date,
164
+ ::Shale::Type::Boolean => Object,
141
165
  }.freeze,
142
- T::Hash[Class, Class],
166
+ T::Hash[Class, Object],
143
167
  )
144
168
 
145
- sig { params(attribute: ::Shale::Attribute).returns([String, String]) }
169
+ sig { params(attribute: ::Shale::Attribute).returns([Object, T::Boolean]) }
146
170
  def shale_type_to_sorbet_return_type(attribute)
147
- return_type = SHALE_TYPES_MAP[attribute.type]
171
+ return_type = SHALE_RETURN_TYPES_MAP[attribute.type]
148
172
  return complex_shale_type_to_sorbet_return_type(attribute) unless return_type
149
- return [T.must(return_type.name), T.must(return_type.name)] if attribute.collection? || attribute.default.is_a?(return_type)
173
+ return return_type, false if attribute.collection? || return_type.is_a?(Module) && attribute.default.is_a?(return_type)
150
174
 
151
- [T.must(return_type.name), "T.nilable(#{return_type.name})"]
175
+ [return_type, true]
152
176
  end
153
177
 
154
- sig { params(attribute: ::Shale::Attribute).returns([String, String]) }
178
+ sig { params(attribute: ::Shale::Attribute).returns([Object, T::Boolean]) }
155
179
  def complex_shale_type_to_sorbet_return_type(attribute)
156
- return [T.cast(attribute.type.to_s, String), "T.nilable(#{attribute.type})"] unless attribute.type.respond_to?(:return_type)
180
+ return attribute.type, true unless attribute.type.respond_to?(:return_type)
157
181
 
158
182
  return_type_string = attribute.type.return_type.to_s
159
- [return_type_string, return_type_string]
183
+ [return_type_string, false]
160
184
  end
161
185
 
162
- sig { params(attribute: ::Shale::Attribute).returns([String, String]) }
186
+ sig { params(attribute: ::Shale::Attribute).returns([Object, T::Boolean]) }
163
187
  def shale_type_to_sorbet_setter_type(attribute)
164
- setter_type = SHALE_TYPES_MAP[attribute.type]
188
+ setter_type = SHALE_SETTER_TYPES_MAP[attribute.type]
165
189
  return complex_shale_type_to_sorbet_setter_type(attribute) unless setter_type
166
- return [T.must(setter_type.name), T.must(setter_type.name)] if attribute.collection? || attribute.default.is_a?(setter_type)
190
+ return setter_type, false if attribute.collection? || setter_type.is_a?(Module) && attribute.default.is_a?(setter_type)
167
191
 
168
- [T.must(setter_type.name), "T.nilable(#{setter_type.name})"]
192
+ [setter_type, true]
169
193
  end
170
194
 
171
- sig { params(attribute: ::Shale::Attribute).returns([String, String]) }
195
+ sig { params(attribute: ::Shale::Attribute).returns([Object, T::Boolean]) }
172
196
  def complex_shale_type_to_sorbet_setter_type(attribute)
173
197
  if attribute.type.respond_to?(:setter_type)
174
- setter_type_string = attribute.type.setter_type.to_s
175
- [setter_type_string, setter_type_string]
198
+ setter_type_string = attribute.type.setter_type
199
+ [setter_type_string, false]
176
200
  elsif attribute.type.respond_to?(:return_type)
177
- return_type_string = attribute.type.return_type.to_s
178
- [return_type_string, return_type_string]
201
+ return_type_string = attribute.type.return_type
202
+ [return_type_string, false]
179
203
  else
180
- [T.cast(attribute.type.to_s, String), "T.nilable(#{attribute.type})"]
204
+ [attribute.type, true]
181
205
  end
182
206
  end
183
207
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shale-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-26 00:00:00.000000000 Z
11
+ date: 2025-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: booleans