nested_send 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/nested_send.rb +91 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 31d3d0c7928a19452f75a1341a2ca83a241c37a017f7b8aa391a3da75843c158
4
+ data.tar.gz: 2c73703d350f25da042125654ebeaa245b16724de19f3ad451aa29058cdfa749
5
+ SHA512:
6
+ metadata.gz: 834fae2d39c01368d03e9e723a7968bd934509458e2d3fd7a7ae42b2f52e4d60e27f85bacdf93ee230f30cea15d8aaca260b0f54778a1cf60eab5ecc0213042e
7
+ data.tar.gz: 772f763f0a2b12aec2b02e4eda004f445d3c755ff531bd686d352d98c54324fe7c4fda70d277334600fd1f12d764e044f8e5b40a53713155d3261c2721636f0e
@@ -0,0 +1,91 @@
1
+ module NestedSend
2
+ class Checker
3
+ def self.kind_of_collection(attribute)
4
+ return :sym_hash if is_symbolized_hash?(attribute)
5
+ return :str_hash if is_complete_string?(attribute)
6
+ return :array if is_stringified_array?(attribute)
7
+ return false
8
+ end
9
+
10
+ def self.kind_of_attribute(kind)
11
+ return :method if kind == '.'
12
+ return :collection if kind == '['
13
+ end
14
+
15
+ def self.is_symbolized_hash?(attribute)
16
+ return attribute[0] == ':'
17
+ end
18
+
19
+ def self.is_string_hash?(attribute)
20
+ strings = ["'", '"']
21
+ strings.include?(attribute[0]) && strings.include?(attribute[-1])
22
+ end
23
+
24
+ def self.is_stringified_array?(attribute)
25
+ return attribute.to_i.to_s == attribute
26
+ end
27
+
28
+ def self.is_complete_string?(attribute)
29
+ start_char = attribute[0]
30
+ end_char = attribute[-1]
31
+ does_match = (start_char == end_char)
32
+ return does_match if is_string_hash?(attribute)
33
+ return false
34
+ end
35
+ end
36
+ end
37
+
38
+ Object.class_eval do
39
+
40
+ def nested_send(attribute, with_indifferent_access: false)
41
+
42
+ checker = NestedSend::Checker
43
+
44
+ matches = /([^\.\[\]]+)(.*)/.match(attribute)
45
+ attribute = matches.captures[0]
46
+ object = send(attribute)
47
+
48
+ until matches.captures[1].empty?
49
+
50
+ next_part = matches.captures[1]
51
+ kind = checker.kind_of_attribute(next_part.slice!(0))
52
+
53
+ if kind == :method
54
+
55
+ matches = /([^\.\[\]]+)(.*)/.match(next_part)
56
+ attribute = matches.captures[0]
57
+ object = object.send(attribute)
58
+
59
+ elsif kind == :collection
60
+
61
+ matches = /(['|"|:]*[^\]]+['|"]*)\](.*)/.match(next_part)
62
+ attribute = matches.captures[0]
63
+
64
+ type = checker.kind_of_collection(attribute)
65
+ raise ArgumentError, "Provided attribute was unable to be parsed: #{attribute}" unless type
66
+
67
+ if type == :sym_hash
68
+ attribute.slice!(0)
69
+ attribute = attribute.to_sym
70
+ elsif type == :str_hash
71
+ attribute.slice!(0)
72
+ attribute.chop!
73
+ elsif type == :array
74
+ attribute = attribute.to_i
75
+ end
76
+
77
+ if with_indifferent_access && object.is_a?(Hash)
78
+ object = object.with_indifferent_access
79
+ end
80
+
81
+ object = object[attribute]
82
+
83
+ else
84
+ raise ArgumentError, "Could not parse attribute: #{attribute}"
85
+ end
86
+
87
+ end
88
+
89
+ object
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nested_send
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Maze
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for Ruby that extends send functionality to allow for formatted
14
+ strings to execute chained sends.
15
+ email: rhoxiodbc@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/nested_send.rb
21
+ homepage: https://rubygems.org/gems/nested_send
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.2.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Nested send fucntionality for Ruby
44
+ test_files: []