finest-builder 2.0.0 → 2.0.2

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: 66c7b96883a0a0dfec0738a446e283eda837a06585d04309c07badf3425b0d96
4
- data.tar.gz: 2ba3a8157b0ec1e4e42e5f9f77810cab68dfc5f91a5ab6d6689cdf330ebe3d1b
3
+ metadata.gz: dc3b19cc52ab26f3a5f67eac570e61c901cc0d73c78b9f03df03717488089f9b
4
+ data.tar.gz: ca43ea76e95b81cd437e58e66522188fb89dc552940f13c5392516d548dd1837
5
5
  SHA512:
6
- metadata.gz: fe4a9c7f0f9cdac5d70dd4ab0c0f31fb71b1efffd3171b3279a1fc184e5cd42d1ebbf5432740a7b5de76ad35406de5b3acd9ef337c0e9dc45ac5106a0f921e1d
7
- data.tar.gz: 8c306f2dce9adda3937b47f8f43b0f60cdff083cce5b04163cb4ecab88d83e3ce4d30779b7b16ab44a7e5dd83f594136c37ce9139e4c44f43951d4b75afb96bb
6
+ metadata.gz: 7cb3859299ad76491af236d5377998ccbb3b31f66a7ec2234a6b99fdccb37860adcaab0e11ff0cacd76e6c6deaa7154eebb24cfd1e318e10969b5ac910dfbcb1
7
+ data.tar.gz: 00c79eb2acc898f2f491511a0b396e9eb73b787effbcf001e669d159394436a6425bbba79645cb64c4f89e47c73c965982cad229e733d01076b1c0b1f203f5f8
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.1
@@ -3,16 +3,16 @@
3
3
  require_relative 'lib/finest/builder/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
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]
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 = %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.1.2')
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.4.1')
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 = 'exe'
29
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
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
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "2.0.0"
3
+ VERSION = "2.0.2"
4
4
  end
5
5
  end
@@ -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 inside the structure if passed
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 three characters will just be down cased.
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,9 +35,11 @@ module Finest
35
35
  raise ArgumentError unless keys&.respond_to?(:each)
36
36
 
37
37
  json.transform_keys!(&:to_s)
38
- keys&.reject! { |key| key.end_with?('=') }
39
- keys&.each do |key|
40
- send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
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.
42
+ send("#{key.to_s.snake_case.gsub(/[^a-zA-Z0-9_]/, "_")}=", nested_hash_value(json, key.to_s))
41
43
  end
42
44
  yield self if block_given?
43
45
  self
@@ -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? method,
55
- # which will mean it's a Hash and also if the Hash the method parameter key
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
- elsif (obj[key].is_a?(Array))
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)
@@ -119,7 +128,7 @@ module Finest
119
128
  end
120
129
 
121
130
  def method_missing(name, *args)
122
- attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
131
+ attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s.gsub(/[^a-zA-Z0-9_]/, "_")
123
132
  if attribute =~ /=$/
124
133
  @to_h[attribute.chop] =
125
134
  if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
metadata CHANGED
@@ -1,16 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finest-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduard Garcia Castello
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2022-11-14 00:00:00.000000000 Z
10
+ date: 2025-03-06 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description:
14
12
  email:
15
13
  - edugarcas@gmail.com
16
14
  - eduard@rzilient.club
@@ -19,6 +17,7 @@ extensions: []
19
17
  extra_rdoc_files: []
20
18
  files:
21
19
  - ".gitignore"
20
+ - ".ruby-version"
22
21
  - ".travis.yml"
23
22
  - CODE_OF_CONDUCT.md
24
23
  - Gemfile
@@ -38,7 +37,6 @@ metadata:
38
37
  homepage_uri: https://github.com/eddygarcas/finest-builder
39
38
  source_code_uri: https://github.com/eddygarcas/finest-builder
40
39
  changelog_uri: https://github.com/eddygarcas/finest-builder
41
- post_install_message:
42
40
  rdoc_options: []
43
41
  require_paths:
44
42
  - lib
@@ -46,15 +44,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
44
  requirements:
47
45
  - - ">="
48
46
  - !ruby/object:Gem::Version
49
- version: 3.1.2
47
+ version: 3.4.1
50
48
  required_rubygems_version: !ruby/object:Gem::Requirement
51
49
  requirements:
52
50
  - - ">="
53
51
  - !ruby/object:Gem::Version
54
52
  version: '0'
55
53
  requirements: []
56
- rubygems_version: 3.3.7
57
- signing_key:
54
+ rubygems_version: 3.6.3
58
55
  specification_version: 4
59
56
  summary: Builder modules to create either class ghost methods from a given JSON or
60
57
  a OpenStruct