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 +4 -4
- data/Gemfile +2 -2
- data/README.md +4 -2
- data/lib/finest/builder.rb +60 -26
- data/lib/finest/builder/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b91d61c258c8406ee407a8e2cd5bc94b2023ab31e5bfaf7bb66e7db3ae4a0278
|
4
|
+
data.tar.gz: 8696f9e7ac6b50084b4d72d5569a25736bc60e68957136eff76eee3e0b2abdad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdb2e7dbd54e9016f60efaaceba7fa064424e7a5c13b5e7ef5d11127dc1c7ebba72fdd7155a1658568b8473390592619dec07d45ab9207e39c9c0b2cb24b8e05
|
7
|
+
data.tar.gz: cb262897300489b7c04bb65dbc39db43b6efb10f8b00a09850fe9565d64360cc66b18553067c3fda7a8ef6fbe07b7b2637d7b261be89da43c6342222bed62f6a
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Finest::Builder ![Travis](https://travis-ci.
|
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]/
|
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
|
data/lib/finest/builder.rb
CHANGED
@@ -1,20 +1,42 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
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
|
-
#
|
8
|
-
# The result
|
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
|
12
|
-
|
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
|
-
|
17
|
-
@to_h&.merge!({key.to_sym =>
|
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
|
-
|
26
|
-
self.class.send(:define_method, "#{k}", proc {
|
27
|
-
self.class.send(:define_method, "#{k}=", proc {|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
|
-
|
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
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
126
|
+
alias initialize build_by_keys
|
93
127
|
end
|
94
128
|
|
95
129
|
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
|
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-
|
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.
|
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.
|