hashlation 0.1.4 → 1.0.0
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 +4 -4
- data/lib/hashlation/version.rb +1 -1
- data/lib/hashlation.rb +89 -3
- metadata +2 -4
- data/lib/hashlation/complex.rb +0 -77
- data/lib/hashlation/simple.rb +0 -75
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1ea79a7925cc1bbb74a610f5a501cda5bd7e75fce4ef3cf07769e16bd7182bd6
|
|
4
|
+
data.tar.gz: 89dd89bbe44300fc32075c695b412719a357da34862cc16ed88e6b68914f3bcf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dd5bf21e523ba96bfb500461e12fba4c8d27c590df9fcd1fcc5bf4808f7f36470242bb41d318cb8f41579ee64ad0abb980d5df4267ffe53becf6f634e875a017
|
|
7
|
+
data.tar.gz: 9bbec94a8480565dbd3153cb14e6a7023b065bea688b63b1db4406f533634e2bf440210be10e65d3ffd35c1804bfb1a11b5d46a9305e5999fb8c7ff4bdefce44
|
data/lib/hashlation/version.rb
CHANGED
data/lib/hashlation.rb
CHANGED
|
@@ -1,9 +1,95 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "hashlation/version"
|
|
4
|
-
require_relative "hashlation/complex"
|
|
5
|
-
require_relative "hashlation/simple"
|
|
6
4
|
|
|
7
5
|
module Hashlation
|
|
8
|
-
|
|
6
|
+
# SHOULD process ANY Hash object return, if object if failing, it is likely that a key is not getting transformed
|
|
7
|
+
# correctly, add that character to the transformation string to process correctly.
|
|
8
|
+
class Any
|
|
9
|
+
# Only Assumption is that base level object is a Hash Array or Hash Object
|
|
10
|
+
def initialize(i, key: nil)
|
|
11
|
+
case i
|
|
12
|
+
when Array
|
|
13
|
+
process_array(i, key: key)
|
|
14
|
+
when Hash
|
|
15
|
+
process_hash(i)
|
|
16
|
+
else
|
|
17
|
+
puts 'Base object must be Hash Object or Array'
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def process_array(i, key: nil)
|
|
22
|
+
attr_key = key ? key.underscore : 'array'
|
|
23
|
+
array = []
|
|
24
|
+
unless i.empty?
|
|
25
|
+
i.each do |array_item|
|
|
26
|
+
array << case array_item.class.to_s.to_sym
|
|
27
|
+
when :Hash || :Array
|
|
28
|
+
Any.new(array_item)
|
|
29
|
+
else
|
|
30
|
+
array_item
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
begin
|
|
35
|
+
self.class.attr_accessor(attr_key)
|
|
36
|
+
send("#{attr_key}=", array)
|
|
37
|
+
instance_variable_set("@#{attr_key}", array)
|
|
38
|
+
rescue NameError
|
|
39
|
+
handle_complex_key(attr_key, value)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def process_hash(i)
|
|
44
|
+
i.each do |key, value|
|
|
45
|
+
if key[-1, 1] == '?'
|
|
46
|
+
handle_method(key: key, value: value)
|
|
47
|
+
else
|
|
48
|
+
k_underscore = (key.instance_of? Symbol) ? key : key.underscore
|
|
49
|
+
|
|
50
|
+
case value
|
|
51
|
+
when Hash
|
|
52
|
+
res = Any.new(value, key: k_underscore)
|
|
53
|
+
begin
|
|
54
|
+
self.class.attr_accessor(k_underscore)
|
|
55
|
+
send("#{k_underscore}=", res)
|
|
56
|
+
instance_variable_set("@#{k_underscore}", res)
|
|
57
|
+
rescue NameError
|
|
58
|
+
handle_complex_key(k_underscore, res)
|
|
59
|
+
end
|
|
60
|
+
when Array
|
|
61
|
+
process_array(value, key: k_underscore)
|
|
62
|
+
else
|
|
63
|
+
begin
|
|
64
|
+
self.class.attr_accessor(k_underscore)
|
|
65
|
+
send("#{k_underscore}=", value)
|
|
66
|
+
instance_variable_set("@#{k_underscore}", value)
|
|
67
|
+
rescue NameError
|
|
68
|
+
handle_complex_key(k_underscore, value)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def handle_complex_key(key, value)
|
|
76
|
+
# If this is to be responses may contain improper keys, uncomment and implement for all attr names
|
|
77
|
+
k_special_underscore = key[0,1] =~ /^[0-9].*/ ? "_#{key.tr(':', '_').underscore}" : key.tr(':', '_').underscore
|
|
78
|
+
self.class.attr_accessor(k_special_underscore)
|
|
79
|
+
send("#{k_special_underscore}=", value)
|
|
80
|
+
instance_variable_set("@#{k_special_underscore}", value)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Allows us to handle methods ending with '?' as Singleton Methods
|
|
84
|
+
def handle_method(key:, value:)
|
|
85
|
+
define_singleton_method(key) do
|
|
86
|
+
return value
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def keys
|
|
91
|
+
instance_variables
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
9
95
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hashlation
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- rharris389
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-05-
|
|
11
|
+
date: 2022-05-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Lightweight parser for cleaner navigation of large or deeply nested hashes.
|
|
14
14
|
email:
|
|
@@ -40,8 +40,6 @@ files:
|
|
|
40
40
|
- bin/setup
|
|
41
41
|
- hashlation.gemspec
|
|
42
42
|
- lib/hashlation.rb
|
|
43
|
-
- lib/hashlation/complex.rb
|
|
44
|
-
- lib/hashlation/simple.rb
|
|
45
43
|
- lib/hashlation/version.rb
|
|
46
44
|
homepage: https://github.com/rharris389/hashlation
|
|
47
45
|
licenses:
|
data/lib/hashlation/complex.rb
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
module Hashlation
|
|
2
|
-
# ---------------------------------------COMPLEX PARSER-----------------------------------------
|
|
3
|
-
# SHOULD process ANY Hash object return, if object if failing, it is likely that a key is not getting transformed
|
|
4
|
-
# correctly, add that character to the transformation string to process correctly.
|
|
5
|
-
class Complex
|
|
6
|
-
# Only Assumption is that base level object is a Hash Array or Hash Object
|
|
7
|
-
def initialize(i, key: nil)
|
|
8
|
-
case i
|
|
9
|
-
when Array
|
|
10
|
-
process_array(i, key: key)
|
|
11
|
-
when Hash
|
|
12
|
-
process_hash(i)
|
|
13
|
-
else
|
|
14
|
-
puts 'Base object must be Hash Object or Array'
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def process_array(i, key: nil)
|
|
19
|
-
attr_key = key ? sanatize_key(key) : 'array'
|
|
20
|
-
array = []
|
|
21
|
-
unless i.empty?
|
|
22
|
-
i.each do |array_item|
|
|
23
|
-
array << case array_item.class.to_s.to_sym
|
|
24
|
-
when :Hash || :Array
|
|
25
|
-
Complex.new(array_item)
|
|
26
|
-
else
|
|
27
|
-
array_item
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
self.class.attr_accessor(attr_key)
|
|
32
|
-
send("#{attr_key}=", array)
|
|
33
|
-
instance_variable_set("@#{attr_key}", array)
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def process_hash(i)
|
|
37
|
-
i.each do |key, value|
|
|
38
|
-
if key[-1, 1] == '?'
|
|
39
|
-
handle_method(key: key, value: value)
|
|
40
|
-
else
|
|
41
|
-
k_underscore = (key.instance_of? Symbol) ? key : sanatize_key(key)
|
|
42
|
-
case value
|
|
43
|
-
when Hash
|
|
44
|
-
res = Complex.new(value, key: k_underscore)
|
|
45
|
-
self.class.attr_accessor
|
|
46
|
-
self.class.attr_accessor(k_underscore)
|
|
47
|
-
send("#{k_underscore}=", res)
|
|
48
|
-
instance_variable_set("@#{k_underscore}", res)
|
|
49
|
-
when Array
|
|
50
|
-
process_array(value, key: k_underscore)
|
|
51
|
-
else
|
|
52
|
-
self.class.attr_accessor(k_underscore)
|
|
53
|
-
send("#{k_underscore}=", value)
|
|
54
|
-
instance_variable_set("@#{k_underscore}", value)
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def sanatize_key(key)
|
|
62
|
-
# If this is to be responses may contain improper keys, uncomment and implement for all attr names
|
|
63
|
-
key[0,1] =~ /^[0-9].*/ ? "_#{key.tr(':', '_').underscore}" : key.tr(':', '_').underscore
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Allows us to handle methods ending with '?' as Singleton Methods
|
|
67
|
-
def handle_method(key:, value:)
|
|
68
|
-
define_singleton_method(key) do
|
|
69
|
-
return value
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def keys
|
|
74
|
-
instance_variables
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
data/lib/hashlation/simple.rb
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
module Hashlation
|
|
2
|
-
# ---------------------------------------SIMPLE PARSER-----------------------------------------
|
|
3
|
-
# SHOULD process MOST common hash keys quickly. Will fail on Leading integers on keys and inclusion of ':' characters
|
|
4
|
-
class Simple
|
|
5
|
-
# Only Assumption is that base level object is a Hash Array or Hash Object
|
|
6
|
-
def initialize(i, key: nil)
|
|
7
|
-
case i
|
|
8
|
-
when Array
|
|
9
|
-
process_array(i, key: key)
|
|
10
|
-
when Hash
|
|
11
|
-
process_hash(i)
|
|
12
|
-
else
|
|
13
|
-
puts 'Base object must be Hash Object or Array'
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def process_array(i, key: nil)
|
|
18
|
-
attr_key = key ? sanatize_key(key) : 'array'
|
|
19
|
-
array = []
|
|
20
|
-
unless i.empty?
|
|
21
|
-
i.each do |array_item|
|
|
22
|
-
array << case array_item.class.to_s.to_sym
|
|
23
|
-
when :Hash || :Array
|
|
24
|
-
Simple.new(array_item)
|
|
25
|
-
else
|
|
26
|
-
array_item
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
self.class.attr_accessor(attr_key)
|
|
31
|
-
send("#{attr_key}=", array)
|
|
32
|
-
instance_variable_set("@#{attr_key}", array)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def process_hash(i)
|
|
36
|
-
i.each do |key, value|
|
|
37
|
-
if key[-1, 1] == '?'
|
|
38
|
-
handle_method(key: key, value: value)
|
|
39
|
-
else
|
|
40
|
-
k_underscore = (key.instance_of? Symbol) ? key : sanatize_key(key)
|
|
41
|
-
case value
|
|
42
|
-
when Hash
|
|
43
|
-
res = Simple.new(value, key: k_underscore)
|
|
44
|
-
self.class.attr_accessor
|
|
45
|
-
self.class.attr_accessor(k_underscore)
|
|
46
|
-
send("#{k_underscore}=", res)
|
|
47
|
-
instance_variable_set("@#{k_underscore}", res)
|
|
48
|
-
when Array
|
|
49
|
-
process_array(value, key: k_underscore)
|
|
50
|
-
else
|
|
51
|
-
self.class.attr_accessor(k_underscore)
|
|
52
|
-
send("#{k_underscore}=", value)
|
|
53
|
-
instance_variable_set("@#{k_underscore}", value)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def sanatize_key(key)
|
|
60
|
-
# If this is to be responses may contain improper keys, uncomment and implement for all attr names
|
|
61
|
-
key.underscore
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
# Allows us to handle methods ending with '?' as Singleton Methods
|
|
65
|
-
def handle_method(key:, value:)
|
|
66
|
-
define_singleton_method(key) do
|
|
67
|
-
return value
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def keys
|
|
72
|
-
instance_variables
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|