hashlation 0.1.3 → 1.1.1

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: 6f73fbcd96c1c7beeedd568b29102b4931a8d2425aaa9dada4fede99d1c0aa88
4
- data.tar.gz: 8a7624e52ea1880b35e5df71d10d704fa8d25dd83205cac334faec748f4b4716
3
+ metadata.gz: 52fc04f602d14cd91c6c7f9041b1fe49700e522b4dee867334d646e2961ba3ec
4
+ data.tar.gz: e018fc8740c10f9cf02f5fab2b4e5c7439df395e7ef0fd18aa362dad2b15667c
5
5
  SHA512:
6
- metadata.gz: 467cbaa9f2c43460c61266487f8f3aece0ef1af931a01f2bfe279608cbef2c38e3f6dfeabebfb93c4c9602509727135457e4eacfd5c097c7804f667ddf9ede8c
7
- data.tar.gz: f95861a090051ccf724bc7cc10f7b43b45a15427847a7070487723c5cdac453545a0e83ac6a21a77f2d86111f77220a908dd63961da75c29364a26f4f398d29f
6
+ metadata.gz: ffc0fb90dd2edd7d3188e4b4f080a53f28189a4a3605384db2077878048af9453e62fd702e815a48094c8d091517d24c328a08d909e501d2b09336d571941991
7
+ data.tar.gz: 7c091b00481934e018c8af726dd0a6b1e9d34a981b263667884892da6de0e3bf80ec58349fcbb982e1f98aad87ed9e730bc18edfc4d882de7a11c66b39656e7a
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Hashlation
2
2
 
3
+ FAST and VERSATILE hash transformer. Born of the need to more easily read and traverse deeply nested response objects. Faster at 1 to 1 comparisons and more versatile than both OpenStruct and Hashie.
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's Gemfile:
@@ -19,49 +21,28 @@ Or install it yourself as:
19
21
 
20
22
 
21
23
 
22
- ## Methods
24
+ ## Usage
23
25
 
24
26
  The returned `obj` includes the method `.keys` to list the keys on the object at every level.
25
27
 
26
- ---
27
-
28
- ### Simple
29
-
30
- Handles simple string/symbol keys in conversion as well as singleton_methods. Very fast, but cannot handle edge cases in keys.
31
-
32
- Usage:
33
-
34
- `obj = Hashlation::Simple.new(hash)`
35
-
36
-
37
- ---
38
-
39
- ### Complex
28
+ `obj = Hashlation::Any.new(hash)`
40
29
 
41
- Handles MOST key types in conversion. If you know that your keys will contain leading Integer characters, or ':' use this method.
42
-
43
- Usage:
44
-
45
- `obj = Hashlation::Complex.new(hash)`
46
-
47
- ---
48
-
49
- ** If `Complex` fails due to inability to read a key, please report in GitHub Issues. Thanks! **
30
+ Handles both simple and complex string/symbol keys in translation. Defines singleton_methods for keys ending in '?'.
50
31
 
51
32
  ---
52
33
 
53
34
  ## Translated Keys Format:
54
35
 
55
- Keys that cannot be set as-is by attr_accessor will be transformed.
56
-
57
- ### Simple:
36
+ Keys that cannot be set as-is by attr_accessor will be transformed. For example:
58
37
 
59
38
  - `'Test-Name' -> obj.test_name`
60
39
 
61
- ### Complex:
62
-
63
40
  - `'11123:12312' -> obj._11123_12312`
64
41
 
42
+ ** If `Any` fails due to inability to read a key, please report in GitHub Issues. Thanks! **
43
+
44
+ ---
45
+
65
46
  ## Development
66
47
 
67
48
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/hashlation.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["56176404+rharris389@users.noreply.github.com"]
10
10
 
11
11
  spec.summary = "Lightweight response parsing"
12
- spec.description = "Lightweight hash parser for cleaner navigation of large response objects."
12
+ spec.description = "Lightweight parser for cleaner navigation of large and deeply nested hashes."
13
13
  spec.homepage = "https://github.com/rharris389/hashlation"
14
14
  spec.license = "MIT"
15
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.3"
4
+ VERSION = "1.1.1"
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 correctly.
7
+ # 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
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
+ case value
50
+ when Hash
51
+ res = Any.new(value, key: k_underscore)
52
+ begin
53
+ self.class.attr_accessor(k_underscore)
54
+ send("#{k_underscore}=", res)
55
+ instance_variable_set("@#{k_underscore}", res)
56
+ rescue NameError
57
+ handle_complex_key(k_underscore, res)
58
+ end
59
+ when Array
60
+ process_array(value, key: k_underscore)
61
+ else
62
+ begin
63
+ self.class.attr_accessor(k_underscore)
64
+ send("#{k_underscore}=", value)
65
+ instance_variable_set("@#{k_underscore}", value)
66
+ rescue NameError
67
+ handle_complex_key(k_underscore, value)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ # Use Begin/Rescue to submit complex keys be sanitized and written to attr_accessor without needlessly checking keys.
75
+ def handle_complex_key(key, value)
76
+ k_special_underscore = key[0,1] =~ /^[0-9].*/ ? "_#{key.tr(':', '_').underscore}" : key.tr(':', '_').underscore
77
+ self.class.attr_accessor(k_special_underscore)
78
+ send("#{k_special_underscore}=", value)
79
+ instance_variable_set("@#{k_special_underscore}", value)
80
+ end
81
+
82
+ # Allows us to handle methods ending with '?' as Singleton Methods
83
+ def handle_method(key:, value:)
84
+ define_singleton_method(key) do
85
+ return value
86
+ end
87
+ end
88
+
89
+ # Get keys at this object level.
90
+ def keys
91
+ instance_variables
92
+ end
93
+ end
94
+
9
95
  end
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashlation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.1.1
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: Lightweight hash parser for cleaner navigation of large response objects.
13
+ description: Lightweight parser for cleaner navigation of large and deeply nested
14
+ hashes.
14
15
  email:
15
16
  - 56176404+rharris389@users.noreply.github.com
16
17
  executables: []
@@ -40,8 +41,6 @@ files:
40
41
  - bin/setup
41
42
  - hashlation.gemspec
42
43
  - lib/hashlation.rb
43
- - lib/hashlation/complex.rb
44
- - lib/hashlation/simple.rb
45
44
  - lib/hashlation/version.rb
46
45
  homepage: https://github.com/rharris389/hashlation
47
46
  licenses:
@@ -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