camel_snake_struct 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/README.md +12 -0
- data/bin/rake +1 -1
- data/bin/rspec +1 -1
- data/bin/rubocop +1 -1
- data/camel_snake_struct.gemspec +1 -1
- data/lib/camel_snake_struct.rb +24 -0
- metadata +3 -3
- /data/{gems.rb → Gemfile} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19cacf8a8dd3f5e1758672ed4ba28f90784c0017cf65bd39947293eab12d86a6
|
4
|
+
data.tar.gz: b4282acdaad784ebc5f7cf4aea44af63cccf5ef23e1c86cc8b6b876b9afcf8d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 200e79a30d0e957e8833d757a6ea342bd79778459a7cfad2ac20c6e07859cf556c00092c8f0050325535e8982a6a7ae15f174e9a413bb1182bd0ca206684bad2
|
7
|
+
data.tar.gz: d5229f7d105c08f93b869acd0d33875026788a09133a227a82ba006f2b99ddc782b4d856fbd49fbf2d5069b4a3ef933d352e3111f6a45b5ce7656e60a1a791ac
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -67,6 +67,18 @@ puts result3.date.timezone # nil
|
|
67
67
|
puts result3.date.unix_time # nil
|
68
68
|
```
|
69
69
|
|
70
|
+
Learning Structs store meta data about the shape of the hash from the examples provided.
|
71
|
+
This can be used with tapioca custom DSL compilers to generate sorbet types
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
MyLoadedStruct = Class.new(CamelSnakeStruct)
|
75
|
+
MyLoadedStruct.example('data' => [{ 'name' => 'Jeff' }], 'errors' => [], 'date' => { 'timezone' => 'UTC', 'unixTime' => 0})
|
76
|
+
|
77
|
+
puts MyLoadedStruct.types_meta_data
|
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
|
+
```
|
80
|
+
|
81
|
+
|
70
82
|
### Limitations
|
71
83
|
|
72
84
|
* Expects to receive a hash
|
data/bin/rake
CHANGED
data/bin/rspec
CHANGED
data/bin/rubocop
CHANGED
data/camel_snake_struct.gemspec
CHANGED
data/lib/camel_snake_struct.rb
CHANGED
@@ -3,10 +3,18 @@ require 'active_support/core_ext/string'
|
|
3
3
|
|
4
4
|
class CamelSnakeStruct
|
5
5
|
def self.example(data)
|
6
|
+
raise ArgumentError, "Examples are for Learning Structs" if self == CamelSnakeStruct
|
7
|
+
|
6
8
|
new_example = new(data)
|
7
9
|
walk_example(new_example)
|
8
10
|
end
|
9
11
|
|
12
|
+
Type__Meta__Data = Struct.new(:class_types, :array) do
|
13
|
+
def classes
|
14
|
+
class_types.to_a
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
10
18
|
def self.walk_example(new_example)
|
11
19
|
new_example.send(:_method_to_key).keys.each do |m_name|
|
12
20
|
result = new_example.public_send(m_name)
|
@@ -15,9 +23,25 @@ class CamelSnakeStruct
|
|
15
23
|
elsif result.is_a?(Array) && result.first.is_a?(CamelSnakeStruct)
|
16
24
|
walk_example(result.first)
|
17
25
|
end
|
26
|
+
|
27
|
+
store_meta_data(new_example.class, m_name, result)
|
18
28
|
end
|
19
29
|
end
|
20
30
|
|
31
|
+
def self.store_meta_data(example_class, m_name, result)
|
32
|
+
types_meta_data = (example_class.types_meta_data[m_name] ||= Type__Meta__Data.new(Set.new, false))
|
33
|
+
if result.is_a?(Array)
|
34
|
+
types_meta_data.array = true
|
35
|
+
result.map(&:class).each { |c| types_meta_data.class_types << c }
|
36
|
+
else
|
37
|
+
types_meta_data.class_types << result.class
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.types_meta_data
|
42
|
+
@types_meta_data ||= {}
|
43
|
+
end
|
44
|
+
|
21
45
|
def initialize(hash)
|
22
46
|
@_raw_hash = hash&.to_h || {}
|
23
47
|
@_method_to_key = @_raw_hash.keys.each_with_object({}) do |key, mapping|
|
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.
|
4
|
+
version: 0.3.0
|
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:
|
11
|
+
date: 2023-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- ".rspec"
|
42
42
|
- ".rubocop.yml"
|
43
43
|
- CHANGELOG.md
|
44
|
+
- Gemfile
|
44
45
|
- README.md
|
45
46
|
- Rakefile
|
46
47
|
- bin/console
|
@@ -49,7 +50,6 @@ files:
|
|
49
50
|
- bin/rubocop
|
50
51
|
- bin/setup
|
51
52
|
- camel_snake_struct.gemspec
|
52
|
-
- gems.rb
|
53
53
|
- lib/camel_snake_struct.rb
|
54
54
|
homepage: https://github.com/NEXL-LTS/camel_snake_struct-ruby
|
55
55
|
licenses: []
|
/data/{gems.rb → Gemfile}
RENAMED
File without changes
|