finest-builder 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/finest-builder.gemspec +11 -11
- data/lib/finest/builder/version.rb +1 -1
- data/lib/finest/builder.rb +18 -9
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9500bcd59f3997b96597a06dc0548ab86a2b545ae1ded571462cbec3952b1aee
|
4
|
+
data.tar.gz: 6c03ffce45508b415c81afe0627948051fc9119e1c109326de8651bf1a12c1ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a9bd352545dc30bbdc85a03188b9dc3c713d6e84541753a713ec0aa116e6b01660b8ea0df212905899becaac25aa1172868543fd857c4129bb105a544d07477
|
7
|
+
data.tar.gz: 23a85268461f0283bbfcaae50059faa6830b5784a2588dcae1e637df4b31a431267e4b22526445b0bb6a90db70719cc7e2088a0a46197bf16f05630c7df6eba6
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.0
|
data/finest-builder.gemspec
CHANGED
@@ -3,16 +3,16 @@
|
|
3
3
|
require_relative 'lib/finest/builder/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name
|
7
|
-
spec.version
|
8
|
-
spec.authors
|
9
|
-
spec.email
|
6
|
+
spec.name = 'finest-builder'
|
7
|
+
spec.version = Finest::Builder::VERSION
|
8
|
+
spec.authors = ['Eduard Garcia Castello']
|
9
|
+
spec.email = %w[edugarcas@gmail.com eduard@rzilient.club]
|
10
10
|
|
11
|
-
spec.summary
|
12
|
-
#spec.description = %q{TODO: Write a longer description or delete this line.}
|
13
|
-
spec.homepage
|
14
|
-
spec.license
|
15
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 3.
|
11
|
+
spec.summary = %q{Builder modules to create either class ghost methods from a given JSON or a OpenStruct}
|
12
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
13
|
+
spec.homepage = 'https://github.com/eddygarcas/finest-builder'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 3.3.0')
|
16
16
|
|
17
17
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
18
18
|
|
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
26
26
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
27
|
end
|
28
|
-
spec.bindir
|
29
|
-
spec.executables
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
30
|
spec.require_paths = ['lib']
|
31
31
|
end
|
data/lib/finest/builder.rb
CHANGED
@@ -13,7 +13,7 @@ module Finest
|
|
13
13
|
# Finest Builder
|
14
14
|
module Helper
|
15
15
|
|
16
|
-
# Parses a given json structure looking for specific keys
|
16
|
+
# Parses a given +json+ structure looking for specific +keys+ and creating instance methods for each key.
|
17
17
|
#
|
18
18
|
# The result it's stored on a instance variable called to_h and accessible through accessor with same name
|
19
19
|
# as well as it created a instance method for every key.
|
@@ -25,7 +25,7 @@ module Finest
|
|
25
25
|
# e.client.to_h[:id_sa]
|
26
26
|
# e.client.id_sa
|
27
27
|
#
|
28
|
-
# Any key value less than
|
28
|
+
# Any key value less than seven characters will just be down cased.
|
29
29
|
# e.client.to_h[:id]
|
30
30
|
# e.client.id
|
31
31
|
#
|
@@ -35,8 +35,10 @@ module Finest
|
|
35
35
|
raise ArgumentError unless keys&.respond_to?(:each)
|
36
36
|
|
37
37
|
json.transform_keys!(&:to_s)
|
38
|
-
keys
|
39
|
-
keys
|
38
|
+
keys.reject! { |key| key.end_with?('=') }
|
39
|
+
keys.each do |key|
|
40
|
+
# Next call will provoke a +method_missing+ call that will later call to +accessor_builder+ method
|
41
|
+
# which eventually will define both methods +setter+ and +getter+ for the instance variable.
|
40
42
|
send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
|
41
43
|
end
|
42
44
|
yield self if block_given?
|
@@ -51,14 +53,13 @@ module Finest
|
|
51
53
|
end
|
52
54
|
|
53
55
|
# Goes through a complex Hash nest and gets the value of a passed key.
|
54
|
-
# First wil check whether the object has the key
|
55
|
-
# which
|
56
|
+
# First wil check whether the object has the +key?+ method,
|
57
|
+
# which means it's a +Hash+. If so, will look for the key and return its value.
|
56
58
|
# if obj.respond_to?(:key?) && obj.key?(key)
|
57
59
|
#
|
58
60
|
# If result object is a hash itself, will call constructor method to parse this hash first.
|
59
61
|
#
|
60
|
-
# if obj[key].is_a?(Hash)
|
61
|
-
# self.class.new(obj[key])
|
62
|
+
# if obj[key].is_a?(Hash) self.class.new(obj[key]) end;
|
62
63
|
#
|
63
64
|
# If it's an array, will call the constructor method for each element of the array, mapping the result.
|
64
65
|
#
|
@@ -72,17 +73,25 @@ module Finest
|
|
72
73
|
# If eventually the keys was not found, it will assign nil to the instance variable.
|
73
74
|
#
|
74
75
|
def nested_hash_value(obj, key)
|
76
|
+
# Check if the object is a +Hash+ and if that +Hash+ contains the key passed as parameter.
|
75
77
|
if obj.respond_to?(:key?) && obj.key?(key)
|
78
|
+
# If the value of the key is a +Hash+ will call the constructor method to parse this hash first.
|
79
|
+
# This way we can go through the nested hash looking for the key, calling itself recursively.
|
76
80
|
if obj[key].is_a?(Hash)
|
77
81
|
self.class.new(obj[key])
|
78
|
-
|
82
|
+
# If the value of the key is an +Array+ instead, will map the result of the constructor method for each
|
83
|
+
# element of the array.
|
84
|
+
elsif obj[key].is_a?(Array)
|
79
85
|
obj[key].map! do |a|
|
80
86
|
a.respond_to?(:key?) ? self.class.new(a) : a
|
81
87
|
end
|
82
88
|
else
|
89
|
+
# If the value of the key is not a +Hash+ nor an +Array+ will just return the value as it was found.
|
83
90
|
obj[key]
|
84
91
|
end
|
85
92
|
elsif obj.respond_to?(:each)
|
93
|
+
# If the object was an +Array+ in the first place, will recursively call itself for each element
|
94
|
+
# of the array passing the key we are looking for as parameter.
|
86
95
|
r = nil
|
87
96
|
obj.find do |*a|
|
88
97
|
r = nested_hash_value(a.last, key)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finest-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eduard Garcia Castello
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- ".gitignore"
|
22
|
+
- ".ruby-version"
|
22
23
|
- ".travis.yml"
|
23
24
|
- CODE_OF_CONDUCT.md
|
24
25
|
- Gemfile
|
@@ -46,14 +47,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
47
|
requirements:
|
47
48
|
- - ">="
|
48
49
|
- !ruby/object:Gem::Version
|
49
|
-
version: 3.
|
50
|
+
version: 3.3.0
|
50
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
52
|
requirements:
|
52
53
|
- - ">="
|
53
54
|
- !ruby/object:Gem::Version
|
54
55
|
version: '0'
|
55
56
|
requirements: []
|
56
|
-
rubygems_version: 3.
|
57
|
+
rubygems_version: 3.5.4
|
57
58
|
signing_key:
|
58
59
|
specification_version: 4
|
59
60
|
summary: Builder modules to create either class ghost methods from a given JSON or
|