camel_snake_struct 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +43 -0
- data/camel_snake_struct.gemspec +1 -1
- data/lib/camel_snake_struct.rb +5 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 810f5a99f464439e0e6ebeebad3df0dbfb41bcd868a8d0418cfe60b6df7c9b8e
|
4
|
+
data.tar.gz: a8e071224633a912dc8ca9f32ee2aaeb03b2e5f0c1bd63b7a1f910d38d53e0d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e272d3ebeb81fa42f0a61c6f31e398d41f16a1fb6a946c2091cd469dae8f2f171cc5ca02b2185c2fc24504fe723218eec4f83f942c863ff8d1f48e00878c6aaa
|
7
|
+
data.tar.gz: 49d5154129c2a0497e549b50b8caade8ab6533d329c8a7d2c5853d59f95d084511c7a58b3b02573c7582a5ffad7638aa9981c3edfc7ef00eb8f503981a4e1409
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# 0.3.2
|
2
|
+
* fix handling of "@" in keys
|
3
|
+
|
4
|
+
# 0.3.1
|
5
|
+
* handle situation where the key of the hash will not make a valid method, EG { "2020-01-02 19:00:00 UTC" => [""] }
|
6
|
+
|
1
7
|
# 0.3.0
|
2
8
|
* Learning Structs store meta data about the shape of the hash from the examples provided. This can be used with tapioca custom DSL compilers to generate sorbet types
|
3
9
|
|
data/README.md
CHANGED
@@ -78,6 +78,49 @@ puts MyLoadedStruct.types_meta_data
|
|
78
78
|
# {"data"=>#<struct CamelSnakeStruct::Type__Meta__Data class_types=#<Set: {MyLoadedStruct::Datum}>, array=true>, "errors"=>#<struct CamelSnakeStruct::Type__Meta__Data class_types=#<Set: {}>, array=true>, "date"=>#<struct CamelSnakeStruct::Type__Meta__Data class_types=#<Set: {MyLoadedStruct::Date}>, array=false>}
|
79
79
|
```
|
80
80
|
|
81
|
+
```ruby
|
82
|
+
# typed: true
|
83
|
+
|
84
|
+
module Tapioca
|
85
|
+
module Compilers
|
86
|
+
class CamelSnakeStructCompiler < Tapioca::Dsl::Compiler
|
87
|
+
extend T::Sig
|
88
|
+
|
89
|
+
ConstantType = type_member {{ fixed: T.class_of(CamelSnakeStruct) }}
|
90
|
+
|
91
|
+
sig { override.returns(T::Enumerable[Module]) }
|
92
|
+
def self.gather_constants
|
93
|
+
all_classes.select do |c|
|
94
|
+
c < ::CamelSnakeStruct && !c.types_meta_data.empty?
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
sig { override.void }
|
99
|
+
def decorate
|
100
|
+
root.create_path(constant) do |klass|
|
101
|
+
constant.types_meta_data.each do |name, meta_data|
|
102
|
+
classes = meta_data.classes.to_a.map{|a| [TrueClass,FalseClass].include?(a) ? "T::Boolean" : a.to_s }.uniq
|
103
|
+
return_type = if classes.size == 1
|
104
|
+
classes.first.to_s
|
105
|
+
else
|
106
|
+
"T.any(#{classes.join(', ')})"
|
107
|
+
end
|
108
|
+
if meta_data.array
|
109
|
+
klass.create_method(name.to_s, parameters: [], return_type: "T::Array[#{return_type}]")
|
110
|
+
elsif return_type == 'NilClass' || return_type == 'String'
|
111
|
+
klass.create_method(name.to_s, parameters: [], return_type: 'T.nilable(String)')
|
112
|
+
else
|
113
|
+
klass.create_method(name.to_s, parameters: [], return_type: return_type)
|
114
|
+
end
|
115
|
+
klass.create_method("#{name}?", parameters: [], return_type: 'T::Boolean')
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
81
124
|
|
82
125
|
### Limitations
|
83
126
|
|
data/camel_snake_struct.gemspec
CHANGED
data/lib/camel_snake_struct.rb
CHANGED
@@ -45,9 +45,11 @@ class CamelSnakeStruct
|
|
45
45
|
def initialize(hash)
|
46
46
|
@_raw_hash = hash&.to_h || {}
|
47
47
|
@_method_to_key = @_raw_hash.keys.each_with_object({}) do |key, mapping|
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
if key.gsub('@', '') =~ /^[A-Za-z]/
|
49
|
+
normalize_key = key.gsub('@', '').gsub(/\.|\s/, '_')
|
50
|
+
mapping[normalize_key] = key
|
51
|
+
mapping[normalize_key.underscore] = key
|
52
|
+
end
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: camel_snake_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grant Petersen-Speelman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|