finest-builder 1.1.2 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ac8be2e6998bd2599cb0e89a25c0716b1c3891d90b077acd56079eb702d66a1
4
- data.tar.gz: 67cf57d634ea3edda51266744019bb82d68cbf2aaa988c49ab50911d70a3bad1
3
+ metadata.gz: 9500bcd59f3997b96597a06dc0548ab86a2b545ae1ded571462cbec3952b1aee
4
+ data.tar.gz: 6c03ffce45508b415c81afe0627948051fc9119e1c109326de8651bf1a12c1ee
5
5
  SHA512:
6
- metadata.gz: b075bae8a917b4a26f961f9d07ac819d273f8fb67483b87d68b3676c2b75e2c1a1bccf340724d37b47b9e38372b27896d4d190894b317621610fb89d05f62719
7
- data.tar.gz: d8446bbc192a8b192f6cfd00cd9c83628773f595242b5aedf48cda5e580345ab9474eac255172daf0f5ae89aab9993ee2ec834593de48123dfdeaa28ac8f1110
6
+ metadata.gz: 0a9bd352545dc30bbdc85a03188b9dc3c713d6e84541753a713ec0aa116e6b01660b8ea0df212905899becaac25aa1172868543fd857c4129bb105a544d07477
7
+ data.tar.gz: 23a85268461f0283bbfcaae50059faa6830b5784a2588dcae1e637df4b31a431267e4b22526445b0bb6a90db70719cc7e2088a0a46197bf16f05630c7df6eba6
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.0
data/.travis.yml CHANGED
@@ -2,5 +2,5 @@
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.7.4
6
- before_install: gem install bundler -v 2.1.4
5
+ - 3.1.2
6
+ before_install: gem install bundler -v 2.2.24
@@ -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('>= 2.7.4')
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 = '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 = "1.1.2"
3
+ VERSION = "2.0.1"
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,17 +25,20 @@ 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
  #
32
32
  def build_by_keys(json = {}, keys = [])
33
+
33
34
  keys = keys.empty? ? json.keys : keys
34
35
  raise ArgumentError unless keys&.respond_to?(:each)
35
36
 
36
37
  json.transform_keys!(&:to_s)
37
- keys&.reject! { |key| key.end_with?('=') }
38
- keys&.each do |key|
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.
39
42
  send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
40
43
  end
41
44
  yield self if block_given?
@@ -50,25 +53,45 @@ module Finest
50
53
  end
51
54
 
52
55
  # Goes through a complex Hash nest and gets the value of a passed key.
53
- # First wil check whether the object has the key? method,
54
- # 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.
55
58
  # if obj.respond_to?(:key?) && obj.key?(key)
56
59
  #
57
60
  # If result object is a hash itself, will call constructor method to parse this hash first.
58
61
  #
59
- # obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
62
+ # if obj[key].is_a?(Hash) self.class.new(obj[key]) end;
63
+ #
64
+ # If it's an array, will call the constructor method for each element of the array, mapping the result.
65
+ #
66
+ # elsif (obj[key].is_a?(Array))
60
67
  #
61
- # If it's not a Hash will check if it's a Array instead,
62
- # checking out whether it responds to a Array.each method or not.
63
- # elsif obj.respond_to?(:each)
68
+ # As mentioned before, this methods looks for the key passed as parameter, if it's not found, will
69
+ # go through the nested hash looking for the key, calling itself recursively.
70
+ #
71
+ # This way we can look for specific keys inside a complex hash structure and ignore the rest.
72
+ # The result of this action will be an object with the keys found and their values.
73
+ # If eventually the keys was not found, it will assign nil to the instance variable.
64
74
  #
65
- # For every Array found it make a recursive call to itself passing
66
- # the last element of the array and the Key it's looking for.
67
- # r = nested_hash_value(a.last, key)
68
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.
69
77
  if obj.respond_to?(:key?) && obj.key?(key)
70
- obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[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.
80
+ if obj[key].is_a?(Hash)
81
+ self.class.new(obj[key])
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)
85
+ obj[key].map! do |a|
86
+ a.respond_to?(:key?) ? self.class.new(a) : a
87
+ end
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.
90
+ obj[key]
91
+ end
71
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.
72
95
  r = nil
73
96
  obj.find do |*a|
74
97
  r = nested_hash_value(a.last, key)
@@ -109,7 +132,7 @@ module Finest
109
132
  if attribute =~ /=$/
110
133
  @to_h[attribute.chop] =
111
134
  if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
112
- self.class.new(args[0])
135
+ self.class.new(json: args[0])
113
136
  else
114
137
  args[0]
115
138
  end
@@ -124,10 +147,12 @@ module Finest
124
147
 
125
148
  # Finest Builder
126
149
  module Builder
150
+ include Helper
151
+
127
152
  class Error < StandardError; end
128
153
 
129
- include Helper
130
154
  alias initialize build_by_keys
155
+
131
156
  end
132
157
 
133
158
  end
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: 1.1.2
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: 2022-03-25 00:00:00.000000000 Z
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: 2.7.4
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.1.6
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