finest-builder 0.0.2 → 0.1.0

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: d5501ea760786fcb70845bb8abf78fd29eee8c54263f53c585b0b64eb074ff58
4
- data.tar.gz: e25520d22838d23dd72c99c404220b10ca33d43259c1d6dff8b8c50339466535
3
+ metadata.gz: b91d61c258c8406ee407a8e2cd5bc94b2023ab31e5bfaf7bb66e7db3ae4a0278
4
+ data.tar.gz: 8696f9e7ac6b50084b4d72d5569a25736bc60e68957136eff76eee3e0b2abdad
5
5
  SHA512:
6
- metadata.gz: 66b609752b24608f7bc810bdd70230d9d98031f18d61d3551c01ed5d9490a27ddad5f39b0ce6615f04d1a4cbe99ae311b973cd89594ae593ac6ee6cec244f05d
7
- data.tar.gz: 8e3941f503dd9e6f71487954444d362551c7daa3b494429b57451526fcf7f85e2d34b8207e4012cbb197e7afe91546c7cb36a43926f8988af01006e01ce25082
6
+ metadata.gz: fdb2e7dbd54e9016f60efaaceba7fa064424e7a5c13b5e7ef5d11127dc1c7ebba72fdd7155a1658568b8473390592619dec07d45ab9207e39c9c0b2cb24b8e05
7
+ data.tar.gz: cb262897300489b7c04bb65dbc39db43b6efb10f8b00a09850fe9565d64360cc66b18553067c3fda7a8ef6fbe07b7b2637d7b261be89da43c6342222bed62f6a
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.com/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,42 @@
1
- require "finest/builder/version"
1
+ # frozen_string_literal: true
2
2
 
3
- module Binky
3
+ require 'finest/builder/version'
4
+
5
+ class String
6
+ def snake_case
7
+ length > 7 ? strip.gsub(/(\w[A-Z]|\s\S)/) { |e| "#{e[0].strip}_#{e[1].strip.downcase}" }.downcase : strip.downcase
8
+ end
9
+ end
10
+
11
+ module Finest
4
12
  module Helper
5
13
 
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.
14
+ # Parses a given json structure looking for specific keys inside the structure if passed
15
+ #
16
+ # The result it's stored on a instance variable called to_h and accessible through accessor with same name
17
+ # as well as it created a instance method for every key.
18
+ # All methods are created using the snake case approach.
19
+ #
20
+ # e = MyObjectBuilder.new({"client"=> {"idSA"=>1,"id"=>3434, "ManagementType"=>"iOSUnsupervised"}})
21
+ #
22
+ # Result:
23
+ # e.client.to_h[:id_sa]
24
+ # e.client.id_sa
25
+ #
26
+ # Any key value less than three characters will just be down cased.
27
+ # e.client.to_h[:id]
28
+ # e.client.id
29
+ #
9
30
  def build_by_keys(json = {}, keys = nil)
10
31
  k = keys || json&.keys
11
- 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)
32
+ raise ArgumentError 'keys argument is not an array' unless k&.respond_to?(:each)
33
+
34
+ accessor_builder('to_h', {}) unless self.class.method_defined?(:as_json)
13
35
  json.transform_keys!(&:to_s)
14
- k&.reject!{|ky| ky.end_with?('=')}
36
+ k&.reject! { |ky| ky.end_with?('=') }
15
37
  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)})
38
+ send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
39
+ @to_h&.merge!({ key.to_s.snake_case.to_sym => send("#{key.to_s.snake_case}") })
18
40
  end
19
41
  yield self if block_given?
20
42
  self
@@ -22,9 +44,9 @@ module Binky
22
44
 
23
45
  # Builds an instance variable as well as its class method accessors from a key value pair.
24
46
  def accessor_builder(k, v)
25
- 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)})
47
+ instance_variable_set("@#{k}", v)
48
+ self.class.send(:define_method, "#{k}", proc { instance_variable_get("@#{k}") })
49
+ self.class.send(:define_method, "#{k}=", proc { |v| instance_variable_set("@#{k}", v) })
28
50
  end
29
51
 
30
52
  #Goes through a complex Hash nest and gets the value of a passed key.
@@ -32,6 +54,10 @@ module Binky
32
54
  # which will mean it's a Hash and also if the Hash the method parameter key
33
55
  # if obj.respond_to?(:key?) && obj.key?(key)
34
56
  #
57
+ # If result object is a hash itself, will call constructor method to parse this hash first.
58
+ #
59
+ # obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
60
+ #
35
61
  # If it's not a Hash will check if it's a Array instead,
36
62
  # checking out whether it responds to a Array.each method or not.
37
63
  # elsif obj.respond_to?(:each)
@@ -41,7 +67,7 @@ module Binky
41
67
  # r = nested_hash_value(a.last, key)
42
68
  def nested_hash_value(obj, key)
43
69
  if obj.respond_to?(:key?) && obj.key?(key)
44
- obj[key]
70
+ obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
45
71
  elsif obj.respond_to?(:each)
46
72
  r = nil
47
73
  obj.find do |*a|
@@ -51,45 +77,53 @@ module Binky
51
77
  end
52
78
  end
53
79
 
54
-
55
- def method_missing(name,*args)
56
- accessor_builder(name.to_s.gsub(/=$/,''), args[0]) if name.to_s =~ /=$/
80
+ def method_missing(name, *args)
81
+ accessor_builder(name.to_s.gsub(/=$/, ''), args[0]) if name.to_s =~ /=$/
57
82
  end
58
83
 
84
+ def respond_to_missing?; end
85
+
59
86
  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)}
87
+ { attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s) }
61
88
  end
89
+
62
90
  end
63
91
 
64
92
  module Struct
65
93
  class Error < StandardError; end
94
+
66
95
  include Helper
67
96
 
68
97
  def initialize(json = nil)
69
- accessor_builder('to_h',{})
98
+ accessor_builder('to_h', {})
70
99
  json&.each do |k, v|
71
- self.send("#{k}=", v)
100
+ send("#{k}=", v)
72
101
  end
73
102
  end
74
103
 
75
104
  def method_missing(name, *args)
76
105
  attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
77
106
  if attribute =~ /=$/
78
- if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
79
- @to_h[attribute.chop] = self.class.new(args[0])
80
- else
81
- @to_h[attribute.chop] = args[0]
82
- end
107
+ @to_h[attribute.chop] =
108
+ if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
109
+ self.class.new(args[0])
110
+ else
111
+ args[0]
112
+ end
83
113
  else
84
114
  @to_h[attribute]
85
115
  end
86
116
  end
117
+
118
+ def respond_to_missing?; end
119
+
87
120
  end
88
121
 
89
122
  module Builder
90
123
  class Error < StandardError; end
124
+
91
125
  include Helper
92
- alias_method :initialize,:build_by_keys
126
+ alias initialize build_by_keys
93
127
  end
94
128
 
95
129
  end
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
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.2
4
+ version: 0.1.0
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-07-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.6
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.