shale-builder 0.1.3 → 0.1.5

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
  SHA256:
3
- metadata.gz: 4b0eb2f219fcd64c0f89f347f70e892e96f7b18490450a42557a39525e14fabe
4
- data.tar.gz: c1f7b8a45efd094bc4f43028e56eae5d0da322357bbf0d6c1e882021a8da92e6
3
+ metadata.gz: c029074a32dcf979db1589f717921bec3739c32cd81d6e3885ade102119d1612
4
+ data.tar.gz: 7dfd4acf1e1e2442b39a8c62510f2ee332b1ada7e55a346ea7709901cdf3520d
5
5
  SHA512:
6
- metadata.gz: c02cb9e08061c9d978f2e64be2ede31a3a874cc4b5f9e420f00bfe39771e69d48930fd6c063d7a8d6d066a8a3bfcd3faf7c2e58983d081d1ee852c8a2d59c377
7
- data.tar.gz: 69bd7e9e44ae49c0f9bc2a7c19b5b7472d28a56c841b215b8a4be2bbd49dc42f99b3b43bd2d782a87b86a93d1534957af4356184a9e50926ee423b8277ef4bac
6
+ metadata.gz: 6d805f195b379215bc3e10b748919ba10ba3e51abb80c3973679a3546aff1f577983abd383cf1551e48da5fa1511b0110b4e4d5ac28a071f17f3f4ea3216a8c6
7
+ data.tar.gz: ad11f0fb55e725fa3145941d56ac4727b3080b0dfcccceb6acc83d81afea8766b2fa7131725a83d50f9cda62090c1cd17d0cdb54d5db517723a2dbb8a350253a
data/.rubocop.yml CHANGED
@@ -3,3 +3,5 @@ inherit_gem:
3
3
 
4
4
  AllCops:
5
5
  TargetRubyVersion: 3.0
6
+ Exclude:
7
+ - lib/tapioca/**/*.rb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.1.5] - 2024-05-13
2
+
3
+ - Fix a bug in the tapioca compiler
4
+
5
+ ## [0.1.4] - 2024-05-13
6
+
7
+ - Add a tapioca compiler for shale and shale builder
8
+
1
9
  ## [0.1.3] - 2023-11-23
2
10
 
3
11
  - Change shale version dependency from `< 1.0` to `< 2.0`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shale-builder (0.1.3)
4
+ shale-builder (0.1.5)
5
5
  shale (< 2.0)
6
6
 
7
7
  GEM
@@ -3,6 +3,6 @@
3
3
  module Shale
4
4
  module Builder
5
5
  # @return [String]
6
- VERSION = '0.1.3'
6
+ VERSION = '0.1.5'
7
7
  end
8
8
  end
@@ -0,0 +1,109 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require 'shale'
5
+ begin
6
+ require 'shale/builder'
7
+ rescue LoadError
8
+ end
9
+
10
+ module Tapioca
11
+ module Compilers
12
+ class Shale < Tapioca::Dsl::Compiler
13
+ ConstantType = type_member { { fixed: T.class_of(::Shale::Mapper) } }
14
+
15
+ class << self
16
+ sig { override.returns(T::Enumerable[Module]) }
17
+ def gather_constants
18
+ # Collect all the classes that inherit from Shale::Mapper
19
+ all_classes.select { |c| c < ::Shale::Mapper }
20
+ end
21
+ end
22
+
23
+ sig { override.void }
24
+ def decorate
25
+ # Create a RBI definition for each class that inherits from Shale::Mapper
26
+ root.create_path(constant) do |klass|
27
+ has_shale_builder = includes_shale_builder(constant)
28
+
29
+ # For each attribute defined in the class
30
+ constant.attributes.each_value do |attribute|
31
+ non_nilable_type, nilable_type = shale_type_to_sorbet_type(attribute)
32
+ type = nilable_type
33
+ if attribute.collection?
34
+ type = "T.nilable(T::Array[#{non_nilable_type}])"
35
+ end
36
+
37
+ if has_shale_builder && attribute.type < ::Shale::Mapper
38
+ sigs = T.let([], T::Array[RBI::Sig])
39
+ # simple getter
40
+ sigs << klass.create_sig(
41
+ parameters: { block: 'NilClass' },
42
+ return_type: type,
43
+ )
44
+ # getter with block
45
+ sigs << klass.create_sig(
46
+ parameters: { block: "T.proc.params(arg0: #{non_nilable_type}).void" },
47
+ return_type: non_nilable_type
48
+ )
49
+ klass.create_method_with_sigs(
50
+ attribute.name,
51
+ sigs: sigs,
52
+ parameters: [RBI::BlockParam.new('block')],
53
+ )
54
+ else
55
+ klass.create_method(attribute.name, return_type: type)
56
+ end
57
+
58
+ # setter
59
+ klass.create_method(
60
+ "#{attribute.name}=",
61
+ parameters: [create_param('value', type: type)],
62
+ return_type: type,
63
+ )
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ private
70
+
71
+ sig { params(klass: Class).returns(T.nilable(T::Boolean)) }
72
+ def includes_shale_builder(klass)
73
+ return false unless defined?(::Shale::Builder)
74
+
75
+ klass < ::Shale::Builder
76
+ end
77
+
78
+ SHALE_TYPES_MAP = T.let(
79
+ {
80
+ ::Shale::Type::Value => Object,
81
+ ::Shale::Type::String => String,
82
+ ::Shale::Type::Float => Float,
83
+ ::Shale::Type::Integer => Integer,
84
+ ::Shale::Type::Time => Time,
85
+ ::Shale::Type::Date => Date,
86
+ }.freeze,
87
+ T::Hash[Class, Class],
88
+ )
89
+
90
+ sig { params(attribute: ::Shale::Attribute).returns([String, String]) }
91
+ def shale_type_to_sorbet_type(attribute)
92
+ return_type = SHALE_TYPES_MAP[attribute.type]
93
+ return complex_shale_type_to_sorbet_type(attribute) unless return_type
94
+ return [T.must(return_type.name), T.must(return_type.name)] if attribute.collection? || attribute.default.is_a?(return_type)
95
+
96
+ [T.must(return_type.name), "T.nilable(#{return_type.name})"]
97
+ end
98
+
99
+ sig { params(attribute: ::Shale::Attribute).returns([String, String]) }
100
+ def complex_shale_type_to_sorbet_type(attribute)
101
+ return [T.cast(attribute.type.to_s, String), "T.nilable(#{attribute.type})"] unless attribute.type.respond_to?(:return_type)
102
+
103
+ return_type_string = attribute.type.return_type.to_s
104
+ [return_type_string, return_type_string]
105
+ end
106
+
107
+ end
108
+ end
109
+ end
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.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-23 00:00:00.000000000 Z
11
+ date: 2024-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shale
@@ -42,6 +42,7 @@ files:
42
42
  - Rakefile
43
43
  - lib/shale/builder.rb
44
44
  - lib/shale/builder/version.rb
45
+ - lib/tapioca/dsl/compilers/shale.rb
45
46
  - sig/shale/builder.rbs
46
47
  homepage: https://github.com/Verseth/ruby-shale-builder
47
48
  licenses: