finest-builder 0.0.5 → 1.1.1
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 +4 -4
- data/.gitignore +2 -0
- data/README.md +4 -4
- data/finest-builder.gemspec +16 -14
- data/lib/finest/builder/version.rb +1 -1
- data/lib/finest/builder.rb +35 -24
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8d3cea35e0970444a21955f7eb8ac6c01e898e3983e898ca90e7ddc975639c7
|
4
|
+
data.tar.gz: 6037b6ebead0b302224d33679773df44f87376bf79716a9479ea3502d08f6b30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5d526311e33271542eb2ee6c14afc25fc59885c6d8aeb6d5ae754177ac11573ae843f6aa647c235078900ff6b14575aeb741cf5502a6c7190677523666469e9
|
7
|
+
data.tar.gz: 0e8b70132694761f164b0ef1701dd69da4ccd3fbc2aa32b3410032ca5215d3da8c664559e919ee8bdb4cf31525d2c50342141d6bbbc6ab387bbcab08e00242af
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Finest::Builder  [](https://badge.fury.io/rb/finest-builder)
|
2
2
|
|
3
3
|
## Installation
|
4
4
|
|
@@ -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
@@ -1,29 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'lib/finest/builder/version'
|
2
4
|
|
3
5
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name =
|
6
|
+
spec.name = 'finest-builder'
|
5
7
|
spec.version = Finest::Builder::VERSION
|
6
|
-
spec.authors = [
|
7
|
-
spec.email = [
|
8
|
+
spec.authors = ['Eduard Garcia Castello']
|
9
|
+
spec.email = %w[edugarcas@gmail.com eduard@rzilient.club]
|
8
10
|
|
9
|
-
spec.summary = %q{Builder modules to create either class ghost methods from a given
|
11
|
+
spec.summary = %q{Builder modules to create either class ghost methods from a given JSON or a OpenStruct}
|
10
12
|
#spec.description = %q{TODO: Write a longer description or delete this line.}
|
11
|
-
spec.homepage =
|
12
|
-
spec.license =
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new(
|
13
|
+
spec.homepage = 'https://github.com/eddygarcas/finest-builder'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.4')
|
14
16
|
|
15
|
-
spec.metadata[
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
16
18
|
|
17
|
-
spec.metadata[
|
18
|
-
spec.metadata[
|
19
|
-
spec.metadata[
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/eddygarcas/finest-builder'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/eddygarcas/finest-builder'
|
20
22
|
|
21
23
|
# Specify which files should be added to the gem when it is released.
|
22
24
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
-
spec.files
|
25
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
26
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
27
|
end
|
26
|
-
spec.bindir =
|
28
|
+
spec.bindir = 'exe'
|
27
29
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = [
|
30
|
+
spec.require_paths = ['lib']
|
29
31
|
end
|
data/lib/finest/builder.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'finest/builder/version'
|
4
|
+
|
5
|
+
# Add snake case in String
|
3
6
|
class String
|
4
7
|
def snake_case
|
5
|
-
length > 7 ? gsub(/(\w[A-Z])/) { |e| "#{e[0]}_#{e[1].downcase}" }.downcase : downcase
|
8
|
+
length > 7 ? strip.gsub(/(\w[A-Z]|\s\S)/) { |e| "#{e[0].strip}_#{e[1].strip.downcase}" }.downcase : strip.downcase
|
6
9
|
end
|
7
10
|
end
|
8
11
|
|
9
12
|
module Finest
|
13
|
+
# Finest Builder
|
10
14
|
module Helper
|
11
15
|
|
12
16
|
# Parses a given json structure looking for specific keys inside the structure if passed
|
@@ -25,28 +29,27 @@ module Finest
|
|
25
29
|
# e.client.to_h[:id]
|
26
30
|
# e.client.id
|
27
31
|
#
|
28
|
-
def build_by_keys(json = {}, keys =
|
29
|
-
|
30
|
-
raise ArgumentError
|
31
|
-
|
32
|
+
def build_by_keys(json = {}, keys = [])
|
33
|
+
keys = keys.empty? ? json.keys : keys
|
34
|
+
raise ArgumentError unless keys&.respond_to?(:each)
|
35
|
+
|
32
36
|
json.transform_keys!(&:to_s)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@to_h&.merge!({ key.to_s.snake_case.to_sym => self.send("#{key.to_s.snake_case}") })
|
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))
|
37
40
|
end
|
38
41
|
yield self if block_given?
|
39
42
|
self
|
40
43
|
end
|
41
44
|
|
42
45
|
# Builds an instance variable as well as its class method accessors from a key value pair.
|
43
|
-
def accessor_builder(
|
44
|
-
|
45
|
-
self.class.send(:define_method,
|
46
|
-
self.class.send(:define_method, "#{
|
46
|
+
def accessor_builder(key, val)
|
47
|
+
instance_variable_set("@#{key}", val)
|
48
|
+
self.class.send(:define_method, key.to_s, proc { instance_variable_get("@#{key}") })
|
49
|
+
self.class.send(:define_method, "#{key}=", proc { |val| instance_variable_set("@#{key}", val) })
|
47
50
|
end
|
48
51
|
|
49
|
-
#Goes through a complex Hash nest and gets the value of a passed key.
|
52
|
+
# Goes through a complex Hash nest and gets the value of a passed key.
|
50
53
|
# First wil check whether the object has the key? method,
|
51
54
|
# which will mean it's a Hash and also if the Hash the method parameter key
|
52
55
|
# if obj.respond_to?(:key?) && obj.key?(key)
|
@@ -78,43 +81,51 @@ module Finest
|
|
78
81
|
accessor_builder(name.to_s.gsub(/=$/, ''), args[0]) if name.to_s =~ /=$/
|
79
82
|
end
|
80
83
|
|
84
|
+
def respond_to_missing?; end
|
85
|
+
|
81
86
|
def attribute_from_inner_key(elem, attr, in_key = nil)
|
82
87
|
{ attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s) }
|
83
88
|
end
|
84
89
|
|
85
90
|
end
|
86
91
|
|
92
|
+
# Finest Struct
|
87
93
|
module Struct
|
88
94
|
class Error < StandardError; end
|
89
95
|
|
90
96
|
include Helper
|
91
97
|
|
92
|
-
def initialize(json =
|
98
|
+
def initialize(json = {}, keys = [])
|
93
99
|
accessor_builder('to_h', {})
|
94
|
-
json
|
95
|
-
|
100
|
+
json.each do |k, v|
|
101
|
+
send("#{k}=", v)
|
96
102
|
end
|
97
103
|
end
|
98
104
|
|
99
105
|
def method_missing(name, *args)
|
100
106
|
attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
|
101
107
|
if attribute =~ /=$/
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
108
|
+
@to_h[attribute.chop] =
|
109
|
+
if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
|
110
|
+
self.class.new(args[0])
|
111
|
+
else
|
112
|
+
args[0]
|
113
|
+
end
|
107
114
|
else
|
108
115
|
@to_h[attribute]
|
109
116
|
end
|
110
117
|
end
|
118
|
+
|
119
|
+
def respond_to_missing?; end
|
120
|
+
|
111
121
|
end
|
112
122
|
|
123
|
+
# Finest Builder
|
113
124
|
module Builder
|
114
125
|
class Error < StandardError; end
|
115
126
|
|
116
127
|
include Helper
|
117
|
-
|
128
|
+
alias initialize build_by_keys
|
118
129
|
end
|
119
130
|
|
120
131
|
end
|
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finest-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.1
|
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-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description:
|
14
14
|
email:
|
15
|
+
- edugarcas@gmail.com
|
15
16
|
- eduard@rzilient.club
|
16
17
|
executables: []
|
17
18
|
extensions: []
|
@@ -37,7 +38,7 @@ metadata:
|
|
37
38
|
homepage_uri: https://github.com/eddygarcas/finest-builder
|
38
39
|
source_code_uri: https://github.com/eddygarcas/finest-builder
|
39
40
|
changelog_uri: https://github.com/eddygarcas/finest-builder
|
40
|
-
post_install_message:
|
41
|
+
post_install_message:
|
41
42
|
rdoc_options: []
|
42
43
|
require_paths:
|
43
44
|
- lib
|
@@ -45,16 +46,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
46
|
requirements:
|
46
47
|
- - ">="
|
47
48
|
- !ruby/object:Gem::Version
|
48
|
-
version: 2.
|
49
|
+
version: 2.7.4
|
49
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
51
|
requirements:
|
51
52
|
- - ">="
|
52
53
|
- !ruby/object:Gem::Version
|
53
54
|
version: '0'
|
54
55
|
requirements: []
|
55
|
-
rubygems_version: 3.1.
|
56
|
-
signing_key:
|
56
|
+
rubygems_version: 3.1.6
|
57
|
+
signing_key:
|
57
58
|
specification_version: 4
|
58
|
-
summary: Builder modules to create either class ghost methods from a given
|
59
|
-
|
59
|
+
summary: Builder modules to create either class ghost methods from a given JSON or
|
60
|
+
a OpenStruct
|
60
61
|
test_files: []
|