hashlation 0.1.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68d3645838c4821d08458fc685b7ea0e0e4a6c849ca43d8c3008915e5dd40a91
4
- data.tar.gz: eb99e52cf5a8c7831b1f3136aac4457d97214db84af6575f544667146fb28585
3
+ metadata.gz: 1ea79a7925cc1bbb74a610f5a501cda5bd7e75fce4ef3cf07769e16bd7182bd6
4
+ data.tar.gz: 89dd89bbe44300fc32075c695b412719a357da34862cc16ed88e6b68914f3bcf
5
5
  SHA512:
6
- metadata.gz: 7af40ef344bbda534fc05cdc7b6a55a064d3124a1e0c9da4c73dd7a61f95366370ce73955ec9cb83e30dd899c391b7178236ef61e637f6075fd8b23147aa0726
7
- data.tar.gz: 3eb70282efe7e2ade0017ebd919729b918bc5004cc8843871fbc8c73e1801c4aa19d19f86e80bef2fbae9a264bc8982eb751b585a2b3bc0a7a107e99eccb9ca3
6
+ metadata.gz: dd5bf21e523ba96bfb500461e12fba4c8d27c590df9fcd1fcc5bf4808f7f36470242bb41d318cb8f41579ee64ad0abb980d5df4267ffe53becf6f634e875a017
7
+ data.tar.gz: 9bbec94a8480565dbd3153cb14e6a7023b065bea688b63b1db4406f533634e2bf440210be10e65d3ffd35c1804bfb1a11b5d46a9305e5999fb8c7ff4bdefce44
data/README.md CHANGED
@@ -17,13 +17,9 @@ Or install it yourself as:
17
17
  $ gem install hashlation
18
18
 
19
19
 
20
- Hashlation::Complex -> Hashlation::Complex.new(hash)
21
-
22
-
23
20
 
24
21
 
25
-
26
- ##Methods
22
+ ## Methods
27
23
 
28
24
  The returned `obj` includes the method `.keys` to list the keys on the object at every level.
29
25
 
@@ -50,7 +46,21 @@ Usage:
50
46
 
51
47
  ---
52
48
 
53
- **If `Complex` fails due to inability to read key, please report in Issues. Thanks! **
49
+ ** If `Complex` fails due to inability to read a key, please report in GitHub Issues. Thanks! **
50
+
51
+ ---
52
+
53
+ ## Translated Keys Format:
54
+
55
+ Keys that cannot be set as-is by attr_accessor will be transformed.
56
+
57
+ ### Simple:
58
+
59
+ - `'Test-Name' -> obj.test_name`
60
+
61
+ ### Complex:
62
+
63
+ - `'11123:12312' -> obj._11123_12312`
54
64
 
55
65
  ## Development
56
66
 
@@ -65,3 +75,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/rharri
65
75
  ## License
66
76
 
67
77
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+
79
+ ---
80
+
81
+ *Naming credit belongs to my wife, who bales out my inability to name stuff..*
data/hashlation.gemspec CHANGED
@@ -8,11 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["rharris389"]
9
9
  spec.email = ["56176404+rharris389@users.noreply.github.com"]
10
10
 
11
- spec.summary = "Simple/Lightweight hash parsing gem."
12
- spec.description = "Quickly parses hashes into objects for cleaner navigation of large response objects.
13
- Hashlation::Simple.new(hash_object) will handle most common string and symbol keys.
14
- Certain characters in a key, such as a leading Integer or a ':' will not translate with Simple.
15
- These must be processed with the Hashlation::Complex.new(hash_object) method. Name credit to my wife."
11
+ spec.summary = "Lightweight response parsing"
12
+ spec.description = "Lightweight parser for cleaner navigation of large or deeply nested hashes."
16
13
  spec.homepage = "https://github.com/rharris389/hashlation"
17
14
  spec.license = "MIT"
18
15
  spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hashlation
4
- VERSION = "0.1.2"
4
+ VERSION = "1.0.0"
5
5
  end
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
- class Error < StandardError; end
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,20 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashlation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
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-16 00:00:00.000000000 Z
11
+ date: 2022-05-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: |-
14
- Quickly parses hashes into objects for cleaner navigation of large response objects.
15
- Hashlation::Simple.new(hash_object) will handle most common string and symbol keys.
16
- Certain characters in a key, such as a leading Integer or a ':' will not translate with Simple.
17
- These must be processed with the Hashlation::Complex.new(hash_object) method. Name credit to my wife.
13
+ description: Lightweight parser for cleaner navigation of large or deeply nested hashes.
18
14
  email:
19
15
  - 56176404+rharris389@users.noreply.github.com
20
16
  executables: []
@@ -44,8 +40,6 @@ files:
44
40
  - bin/setup
45
41
  - hashlation.gemspec
46
42
  - lib/hashlation.rb
47
- - lib/hashlation/complex.rb
48
- - lib/hashlation/simple.rb
49
43
  - lib/hashlation/version.rb
50
44
  homepage: https://github.com/rharris389/hashlation
51
45
  licenses:
@@ -73,5 +67,5 @@ requirements: []
73
67
  rubygems_version: 3.1.2
74
68
  signing_key:
75
69
  specification_version: 4
76
- summary: Simple/Lightweight hash parsing gem.
70
+ summary: Lightweight response parsing
77
71
  test_files: []
@@ -1,78 +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
- # the PrimeTrust API responses only need to be underscored as they do not contain leading integers or dis-allowed characters
64
- key[0,1] =~ /^[0-9].*/ ? "_#{key.tr(':', '_').underscore}" : key.tr(':', '_').underscore
65
- end
66
-
67
- # Allows us to handle methods ending with '?' as Singleton Methods
68
- def handle_method(key:, value:)
69
- define_singleton_method(key) do
70
- return value
71
- end
72
- end
73
-
74
- def keys
75
- instance_variables
76
- end
77
- end
78
- end
@@ -1,76 +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
- # the PrimeTrust API responses only need to be underscored as they do not contain leading integers or dis-allowed characters
62
- key.underscore
63
- end
64
-
65
- # Allows us to handle methods ending with '?' as Singleton Methods
66
- def handle_method(key:, value:)
67
- define_singleton_method(key) do
68
- return value
69
- end
70
- end
71
-
72
- def keys
73
- instance_variables
74
- end
75
- end
76
- end