finest-builder 1.0.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +1 -1
- data/README.md +3 -3
- data/finest-builder.gemspec +1 -1
- data/lib/finest/builder/version.rb +1 -1
- data/lib/finest/builder.rb +14 -14
- 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: 1ac8be2e6998bd2599cb0e89a25c0716b1c3891d90b077acd56079eb702d66a1
|
4
|
+
data.tar.gz: 67cf57d634ea3edda51266744019bb82d68cbf2aaa988c49ab50911d70a3bad1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b075bae8a917b4a26f961f9d07ac819d273f8fb67483b87d68b3676c2b75e2c1a1bccf340724d37b47b9e38372b27896d4d190894b317621610fb89d05f62719
|
7
|
+
data.tar.gz: d8446bbc192a8b192f6cfd00cd9c83628773f595242b5aedf48cda5e580345ab9474eac255172daf0f5ae89aab9993ee2ec834593de48123dfdeaa28ac8f1110
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -26,7 +26,7 @@ Once initialized just use the accessors as any other instance.
|
|
26
26
|
class Issue
|
27
27
|
include Finest::Struct
|
28
28
|
|
29
|
-
def initialize(json =
|
29
|
+
def initialize(json = {}, _keys = [])
|
30
30
|
super json
|
31
31
|
end
|
32
32
|
end
|
@@ -43,7 +43,7 @@ In case not using column names but an array of method names, new accessors would
|
|
43
43
|
include Finest::Helper
|
44
44
|
end
|
45
45
|
|
46
|
-
issue = Issue.new.build_by_keys({id: 1234,text: "hocus pocus"},Issue.column_names) # => Issue.column_names = id:
|
46
|
+
issue = Issue.new.build_by_keys({id: 1234,text: "hocus pocus"}, Issue.column_names) # => Issue.column_names = id:
|
47
47
|
issue.as_json # => {id: 1234}
|
48
48
|
issue.to_h # => nil
|
49
49
|
|
@@ -58,7 +58,7 @@ In case not using column names but an array of method names, new accessors would
|
|
58
58
|
Call *build_by_keys* method once the model has been initialized passing a json message,
|
59
59
|
it would *yield* itself as a block in case you want to perform further actions.
|
60
60
|
```ruby
|
61
|
-
build_by_keys(
|
61
|
+
build_by_keys(**args)
|
62
62
|
```
|
63
63
|
This method would also create an instance variable called *@to_h* contains a pair-value hash as a result.
|
64
64
|
*@to_h* instance variable won't be available if the class inherits from *ActiveRecord::Base*
|
data/finest-builder.gemspec
CHANGED
@@ -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.
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.4')
|
16
16
|
|
17
17
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
18
18
|
|
data/lib/finest/builder.rb
CHANGED
@@ -29,16 +29,14 @@ module Finest
|
|
29
29
|
# e.client.to_h[:id]
|
30
30
|
# e.client.id
|
31
31
|
#
|
32
|
-
def build_by_keys(
|
33
|
-
|
34
|
-
raise ArgumentError unless
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
send("#{key.to_s.snake_case}=", nested_hash_value(args.fetch(:json, {}), key.to_s))
|
41
|
-
@to_h&.merge!({ key.to_s.snake_case.to_sym => send(key.to_s.snake_case.to_s) })
|
32
|
+
def build_by_keys(json = {}, keys = [])
|
33
|
+
keys = keys.empty? ? json.keys : keys
|
34
|
+
raise ArgumentError unless keys&.respond_to?(:each)
|
35
|
+
|
36
|
+
json.transform_keys!(&:to_s)
|
37
|
+
keys&.reject! { |key| key.end_with?('=') }
|
38
|
+
keys&.each do |key|
|
39
|
+
send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
|
42
40
|
end
|
43
41
|
yield self if block_given?
|
44
42
|
self
|
@@ -69,7 +67,7 @@ module Finest
|
|
69
67
|
# r = nested_hash_value(a.last, key)
|
70
68
|
def nested_hash_value(obj, key)
|
71
69
|
if obj.respond_to?(:key?) && obj.key?(key)
|
72
|
-
obj[key].is_a?(Hash) ? self.class.new(
|
70
|
+
obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
|
73
71
|
elsif obj.respond_to?(:each)
|
74
72
|
r = nil
|
75
73
|
obj.find do |*a|
|
@@ -83,7 +81,9 @@ module Finest
|
|
83
81
|
accessor_builder(name.to_s.gsub(/=$/, ''), args[0]) if name.to_s =~ /=$/
|
84
82
|
end
|
85
83
|
|
86
|
-
def respond_to_missing
|
84
|
+
def respond_to_missing?(method_name, include_private = false)
|
85
|
+
method_name.to_s.start_with?('to_') || super
|
86
|
+
end
|
87
87
|
|
88
88
|
def attribute_from_inner_key(elem, attr, in_key = nil)
|
89
89
|
{ attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s) }
|
@@ -97,9 +97,9 @@ module Finest
|
|
97
97
|
|
98
98
|
include Helper
|
99
99
|
|
100
|
-
def initialize(
|
100
|
+
def initialize(json = {}, keys = [])
|
101
101
|
accessor_builder('to_h', {})
|
102
|
-
|
102
|
+
json.each do |k, v|
|
103
103
|
send("#{k}=", v)
|
104
104
|
end
|
105
105
|
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: 1.
|
4
|
+
version: 1.1.2
|
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:
|
11
|
+
date: 2022-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description:
|
14
14
|
email:
|
15
15
|
- edugarcas@gmail.com
|
16
16
|
- eduard@rzilient.club
|
@@ -38,7 +38,7 @@ metadata:
|
|
38
38
|
homepage_uri: https://github.com/eddygarcas/finest-builder
|
39
39
|
source_code_uri: https://github.com/eddygarcas/finest-builder
|
40
40
|
changelog_uri: https://github.com/eddygarcas/finest-builder
|
41
|
-
post_install_message:
|
41
|
+
post_install_message:
|
42
42
|
rdoc_options: []
|
43
43
|
require_paths:
|
44
44
|
- lib
|
@@ -46,7 +46,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
46
|
requirements:
|
47
47
|
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: 2.
|
49
|
+
version: 2.7.4
|
50
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
56
|
rubygems_version: 3.1.6
|
57
|
-
signing_key:
|
57
|
+
signing_key:
|
58
58
|
specification_version: 4
|
59
59
|
summary: Builder modules to create either class ghost methods from a given JSON or
|
60
60
|
a OpenStruct
|