finest-builder 0.0.3 → 0.0.4

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: '018ef6f0c97be9bf47f517b2ac49831f84c679225d85f0755d8b9786d8908f7c'
4
- data.tar.gz: ac90aad8f8b4efab4736931e097d885a54119d8fdd2f1daad2b03653a4ce7d10
3
+ metadata.gz: 6b0f3df268427f9e1a393a8caf84442dd9c928e6355fe6bcd8c6057e43dd35e6
4
+ data.tar.gz: 45f85f5fe8938a51e58178032c2d0bc7f1ab1a02e0133370fe3ff3439bc420f1
5
5
  SHA512:
6
- metadata.gz: b018cc343c668f7a654d3c1609f6bbaa61ddbb59269312f07b9b46e4bcb27b2d4cb17ef3b0bbb05c7800588d646896e5be1775315a570ebed19714077403a9d0
7
- data.tar.gz: 4472b5431bb7b1159d5cd14e245a7eb3c3fa291ebcb82e56938252a27c8f26ce7248efbb72d219ab0d1578a9173df79c0673b7c9458614dd4b874a18c50fc0bf
6
+ metadata.gz: 6598c52de94933ebcfecc67db8796ff957fc93e8c380f7355d3d0e7dfa441235993908211510aba32af0ec8d4fe504e095050b59fca0f0340779c193209bb1b5
7
+ data.tar.gz: c6d48a2efc27b554c04bd282c45a54f113399669d2765b72212b64913db9f5efa47e1cf721849689dd0bfd1223cbe91be891de10bd8c2364d05795cf441e1ac0
data/Gemfile CHANGED
@@ -3,5 +3,5 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in finest-builder.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
7
- gem "minitest", "~> 5.0"
6
+ gem "rake"
7
+ gem "minitest"
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Finest::Builder ![Travis](https://travis-ci.org/eddygarcas/finest-builder.svg) [![Gem Version](https://badge.fury.io/rb/binky-builder.svg)](https://badge.fury.io/rb/binky-builder)
1
+ # Finest::Builder ![Travis](https://travis-ci.org/eddygarcas/finest-builder.svg) [![Gem Version](https://badge.fury.io/rb/finest-builder.svg)](https://badge.fury.io/rb/finest-builder)
2
2
 
3
3
  ## Installation
4
4
 
@@ -81,9 +81,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
81
81
 
82
82
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
83
 
84
+ Rake issues bundle install --path vendor/cache
85
+
84
86
  ## Contributing
85
87
 
86
- Bug reports and pull requests are welcome on GitHub at https://github.com/eddygarcas/finest-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/binky-builder/blob/master/CODE_OF_CONDUCT.md).
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eddygarcas/finest-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/Finest-builder/blob/master/CODE_OF_CONDUCT.md).
87
89
 
88
90
 
89
91
  ## License
@@ -1,20 +1,39 @@
1
1
  require "finest/builder/version"
2
2
 
3
+ class String
4
+ def snake_case
5
+ length > 2 ? gsub(/(\w)[A-Z]/) { |e| "#{e[0]}_#{e[1].downcase}" }.downcase : downcase
6
+ end
7
+ end
8
+
3
9
  module Finest
4
10
  module Helper
5
11
 
6
- # Parses a given json structure looking for specific keys inside the structure.
7
- # Keys are given through a block.
8
- # The result of it it's stored on a instance variable called to_hash and accessible through accessors with same name.
12
+ # Parses a given json structure looking for specific keys inside the structure if passed
13
+ #
14
+ # The result it's stored on a instance variable called to_h and accessible through accessor with same name
15
+ # as well as it created a instance method for every key.
16
+ # All methods are created using the snake case approach.
17
+ #
18
+ # e = MyObjectBuilder.new({"client"=> {"idSA"=>1,"id"=>3434, "ManagementType"=>"iOSUnsupervised"}})
19
+ #
20
+ # Result:
21
+ # e.client.to_h[:id_sa]
22
+ # e.client.id_sa
23
+ #
24
+ # Any key value less than three characters will just be down cased.
25
+ # e.client.to_h[:id]
26
+ # e.client.id
27
+ #
9
28
  def build_by_keys(json = {}, keys = nil)
10
29
  k = keys || json&.keys
11
30
  raise ArgumentError "keys argument is not an array" unless k&.respond_to?(:each)
12
- accessor_builder('to_h',{}) unless self.class.method_defined?(:as_json)
31
+ accessor_builder('to_h', {}) unless self.class.method_defined?(:as_json)
13
32
  json.transform_keys!(&:to_s)
14
- k&.reject!{|ky| ky.end_with?('=')}
33
+ k&.reject! { |ky| ky.end_with?('=') }
15
34
  k&.each do |key|
16
- self.send("#{key}=",nested_hash_value(json, key.to_s))
17
- @to_h&.merge!({key.to_sym => nested_hash_value(json,key.to_s)})
35
+ self.send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
36
+ @to_h&.merge!({ key.to_s.snake_case.to_sym => self.send("#{key.to_s.snake_case}") })
18
37
  end
19
38
  yield self if block_given?
20
39
  self
@@ -23,8 +42,8 @@ module Finest
23
42
  # Builds an instance variable as well as its class method accessors from a key value pair.
24
43
  def accessor_builder(k, v)
25
44
  self.instance_variable_set("@#{k}", v)
26
- self.class.send(:define_method, "#{k}", proc {self.instance_variable_get("@#{k}")})
27
- self.class.send(:define_method, "#{k}=", proc {|v| self.instance_variable_set("@#{k}", v)})
45
+ self.class.send(:define_method, "#{k}", proc { self.instance_variable_get("@#{k}") })
46
+ self.class.send(:define_method, "#{k}=", proc { |v| self.instance_variable_set("@#{k}", v) })
28
47
  end
29
48
 
30
49
  #Goes through a complex Hash nest and gets the value of a passed key.
@@ -32,6 +51,10 @@ module Finest
32
51
  # which will mean it's a Hash and also if the Hash the method parameter key
33
52
  # if obj.respond_to?(:key?) && obj.key?(key)
34
53
  #
54
+ # If result object is a hash itself, will call constructor method to parse this hash first.
55
+ #
56
+ # obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
57
+ #
35
58
  # If it's not a Hash will check if it's a Array instead,
36
59
  # checking out whether it responds to a Array.each method or not.
37
60
  # elsif obj.respond_to?(:each)
@@ -41,7 +64,7 @@ module Finest
41
64
  # r = nested_hash_value(a.last, key)
42
65
  def nested_hash_value(obj, key)
43
66
  if obj.respond_to?(:key?) && obj.key?(key)
44
- obj[key]
67
+ obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
45
68
  elsif obj.respond_to?(:each)
46
69
  r = nil
47
70
  obj.find do |*a|
@@ -51,22 +74,23 @@ module Finest
51
74
  end
52
75
  end
53
76
 
54
-
55
- def method_missing(name,*args)
56
- accessor_builder(name.to_s.gsub(/=$/,''), args[0]) if name.to_s =~ /=$/
77
+ def method_missing(name, *args)
78
+ accessor_builder(name.to_s.gsub(/=$/, ''), args[0]) if name.to_s =~ /=$/
57
79
  end
58
80
 
59
81
  def attribute_from_inner_key(elem, attr, in_key = nil)
60
- {attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s)}
82
+ { attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s) }
61
83
  end
84
+
62
85
  end
63
86
 
64
87
  module Struct
65
88
  class Error < StandardError; end
89
+
66
90
  include Helper
67
91
 
68
92
  def initialize(json = nil)
69
- accessor_builder('to_h',{})
93
+ accessor_builder('to_h', {})
70
94
  json&.each do |k, v|
71
95
  self.send("#{k}=", v)
72
96
  end
@@ -88,8 +112,9 @@ module Finest
88
112
 
89
113
  module Builder
90
114
  class Error < StandardError; end
115
+
91
116
  include Helper
92
- alias_method :initialize,:build_by_keys
117
+ alias_method :initialize, :build_by_keys
93
118
  end
94
119
 
95
120
  end
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finest-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduard Garcia Castello
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-28 00:00:00.000000000 Z
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email:
15
15
  - eduard@rzilient.club
16
16
  executables: []
@@ -37,7 +37,7 @@ metadata:
37
37
  homepage_uri: https://github.com/eddygarcas/finest-builder
38
38
  source_code_uri: https://github.com/eddygarcas/finest-builder
39
39
  changelog_uri: https://github.com/eddygarcas/finest-builder
40
- post_install_message:
40
+ post_install_message:
41
41
  rdoc_options: []
42
42
  require_paths:
43
43
  - lib
@@ -52,8 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubygems_version: 3.1.2
56
- signing_key:
55
+ rubygems_version: 3.1.4
56
+ signing_key:
57
57
  specification_version: 4
58
58
  summary: Builder modules to create either class ghost methods from a given json or
59
59
  an open struct.