finest-builder 1.1.1 → 2.0.0

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: e8d3cea35e0970444a21955f7eb8ac6c01e898e3983e898ca90e7ddc975639c7
4
- data.tar.gz: 6037b6ebead0b302224d33679773df44f87376bf79716a9479ea3502d08f6b30
3
+ metadata.gz: 66c7b96883a0a0dfec0738a446e283eda837a06585d04309c07badf3425b0d96
4
+ data.tar.gz: 2ba3a8157b0ec1e4e42e5f9f77810cab68dfc5f91a5ab6d6689cdf330ebe3d1b
5
5
  SHA512:
6
- metadata.gz: a5d526311e33271542eb2ee6c14afc25fc59885c6d8aeb6d5ae754177ac11573ae843f6aa647c235078900ff6b14575aeb741cf5502a6c7190677523666469e9
7
- data.tar.gz: 0e8b70132694761f164b0ef1701dd69da4ccd3fbc2aa32b3410032ca5215d3da8c664559e919ee8bdb4cf31525d2c50342141d6bbbc6ab387bbcab08e00242af
6
+ metadata.gz: fe4a9c7f0f9cdac5d70dd4ab0c0f31fb71b1efffd3171b3279a1fc184e5cd42d1ebbf5432740a7b5de76ad35406de5b3acd9ef337c0e9dc45ac5106a0f921e1d
7
+ data.tar.gz: 8c306f2dce9adda3937b47f8f43b0f60cdff083cce5b04163cb4ecab88d83e3ce4d30779b7b16ab44a7e5dd83f594136c37ce9139e4c44f43951d4b75afb96bb
data/.travis.yml CHANGED
@@ -2,5 +2,5 @@
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.7.1
6
- before_install: gem install bundler -v 2.1.4
5
+ - 3.1.2
6
+ before_install: gem install bundler -v 2.2.24
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  #spec.description = %q{TODO: Write a longer description or delete this line.}
13
13
  spec.homepage = 'https://github.com/eddygarcas/finest-builder'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 2.7.4')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.1.2')
16
16
 
17
17
  spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
18
18
 
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "1.1.1"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -30,6 +30,7 @@ module Finest
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
 
@@ -56,18 +57,31 @@ module Finest
56
57
  #
57
58
  # If result object is a hash itself, will call constructor method to parse this hash first.
58
59
  #
59
- # obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
60
+ # if obj[key].is_a?(Hash)
61
+ # self.class.new(obj[key])
62
+ #
63
+ # If it's an array, will call the constructor method for each element of the array, mapping the result.
64
+ #
65
+ # elsif (obj[key].is_a?(Array))
60
66
  #
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)
67
+ # As mentioned before, this methods looks for the key passed as parameter, if it's not found, will
68
+ # go through the nested hash looking for the key, calling itself recursively.
69
+ #
70
+ # This way we can look for specific keys inside a complex hash structure and ignore the rest.
71
+ # The result of this action will be an object with the keys found and their values.
72
+ # If eventually the keys was not found, it will assign nil to the instance variable.
64
73
  #
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
74
  def nested_hash_value(obj, key)
69
75
  if obj.respond_to?(:key?) && obj.key?(key)
70
- obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
76
+ if obj[key].is_a?(Hash)
77
+ self.class.new(obj[key])
78
+ elsif (obj[key].is_a?(Array))
79
+ obj[key].map! do |a|
80
+ a.respond_to?(:key?) ? self.class.new(a) : a
81
+ end
82
+ else
83
+ obj[key]
84
+ end
71
85
  elsif obj.respond_to?(:each)
72
86
  r = nil
73
87
  obj.find do |*a|
@@ -81,7 +95,9 @@ module Finest
81
95
  accessor_builder(name.to_s.gsub(/=$/, ''), args[0]) if name.to_s =~ /=$/
82
96
  end
83
97
 
84
- def respond_to_missing?; end
98
+ def respond_to_missing?(method_name, include_private = false)
99
+ method_name.to_s.start_with?('to_') || super
100
+ end
85
101
 
86
102
  def attribute_from_inner_key(elem, attr, in_key = nil)
87
103
  { attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s) }
@@ -107,7 +123,7 @@ module Finest
107
123
  if attribute =~ /=$/
108
124
  @to_h[attribute.chop] =
109
125
  if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
110
- self.class.new(args[0])
126
+ self.class.new(json: args[0])
111
127
  else
112
128
  args[0]
113
129
  end
@@ -122,10 +138,12 @@ module Finest
122
138
 
123
139
  # Finest Builder
124
140
  module Builder
141
+ include Helper
142
+
125
143
  class Error < StandardError; end
126
144
 
127
- include Helper
128
145
  alias initialize build_by_keys
146
+
129
147
  end
130
148
 
131
149
  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.1
4
+ version: 2.0.0
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: 2021-11-08 00:00:00.000000000 Z
11
+ date: 2022-11-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -46,14 +46,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 2.7.4
49
+ version: 3.1.2
50
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  requirements: []
56
- rubygems_version: 3.1.6
56
+ rubygems_version: 3.3.7
57
57
  signing_key:
58
58
  specification_version: 4
59
59
  summary: Builder modules to create either class ghost methods from a given JSON or