finest-builder 0.1.0 → 1.0.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: b91d61c258c8406ee407a8e2cd5bc94b2023ab31e5bfaf7bb66e7db3ae4a0278
4
- data.tar.gz: 8696f9e7ac6b50084b4d72d5569a25736bc60e68957136eff76eee3e0b2abdad
3
+ metadata.gz: 6e146958c0527929112ef202b28806eca032b460d5ab0db7fe7ed4d5071d0fbf
4
+ data.tar.gz: a89df1b3b477cf4fa4104a4086d0426b0530f0f794fc6c2dca69c651321e9ddb
5
5
  SHA512:
6
- metadata.gz: fdb2e7dbd54e9016f60efaaceba7fa064424e7a5c13b5e7ef5d11127dc1c7ebba72fdd7155a1658568b8473390592619dec07d45ab9207e39c9c0b2cb24b8e05
7
- data.tar.gz: cb262897300489b7c04bb65dbc39db43b6efb10f8b00a09850fe9565d64360cc66b18553067c3fda7a8ef6fbe07b7b2637d7b261be89da43c6342222bed62f6a
6
+ metadata.gz: a42f861011bd9b67bd10f665ffc90d1e4724d9e35e0cc9dcdbcaf81adf667790e90a2d12bcd10dd9db24485ca580792ce1271d1fc89076e067304fccd077597f
7
+ data.tar.gz: b70fe07db6f8214e09505f9e7db3c4be89e061183b6f03d01b0ed3dd5e684ecbf538abf26b4e521b8523255bb7a260331aa20ecedf24354df2cee7b8fe2b1e1e
@@ -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 = "finest-builder"
6
+ spec.name = 'finest-builder'
5
7
  spec.version = Finest::Builder::VERSION
6
- spec.authors = ["Eduard Garcia Castello"]
7
- spec.email = ["eduard@rzilient.club"]
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 json or an open struct.}
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 = "https://github.com/eddygarcas/finest-builder"
12
- spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+ spec.homepage = 'https://github.com/eddygarcas/finest-builder'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
16
 
15
- spec.metadata["allowed_push_host"] = "https://rubygems.org/"
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
16
18
 
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/eddygarcas/finest-builder"
19
- spec.metadata["changelog_uri"] = "https://github.com/eddygarcas/finest-builder"
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 = Dir.chdir(File.expand_path('..', __FILE__)) do
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 = "exe"
28
+ spec.bindir = 'exe'
27
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
30
+ spec.require_paths = ['lib']
29
31
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'finest/builder/version'
4
4
 
5
+ # Add snake case in String
5
6
  class String
6
7
  def snake_case
7
8
  length > 7 ? strip.gsub(/(\w[A-Z]|\s\S)/) { |e| "#{e[0].strip}_#{e[1].strip.downcase}" }.downcase : strip.downcase
@@ -9,6 +10,7 @@ class String
9
10
  end
10
11
 
11
12
  module Finest
13
+ # Finest Builder
12
14
  module Helper
13
15
 
14
16
  # Parses a given json structure looking for specific keys inside the structure if passed
@@ -27,29 +29,29 @@ module Finest
27
29
  # e.client.to_h[:id]
28
30
  # e.client.id
29
31
  #
30
- def build_by_keys(json = {}, keys = nil)
31
- k = keys || json&.keys
32
- raise ArgumentError 'keys argument is not an array' unless k&.respond_to?(:each)
32
+ def build_by_keys(**args)
33
+ k = args[:keys] || args.fetch(:json, {})&.keys
34
+ raise ArgumentError unless k&.respond_to?(:each)
33
35
 
34
36
  accessor_builder('to_h', {}) unless self.class.method_defined?(:as_json)
35
- json.transform_keys!(&:to_s)
37
+ args.fetch(:json, {}).transform_keys!(&:to_s)
36
38
  k&.reject! { |ky| ky.end_with?('=') }
37
39
  k&.each do |key|
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}") })
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) })
40
42
  end
41
43
  yield self if block_given?
42
44
  self
43
45
  end
44
46
 
45
47
  # Builds an instance variable as well as its class method accessors from a key value pair.
46
- def accessor_builder(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) })
48
+ def accessor_builder(key, val)
49
+ instance_variable_set("@#{key}", val)
50
+ self.class.send(:define_method, key.to_s, proc { instance_variable_get("@#{key}") })
51
+ self.class.send(:define_method, "#{key}=", proc { |val| instance_variable_set("@#{key}", val) })
50
52
  end
51
53
 
52
- #Goes through a complex Hash nest and gets the value of a passed key.
54
+ # Goes through a complex Hash nest and gets the value of a passed key.
53
55
  # First wil check whether the object has the key? method,
54
56
  # which will mean it's a Hash and also if the Hash the method parameter key
55
57
  # if obj.respond_to?(:key?) && obj.key?(key)
@@ -67,7 +69,7 @@ module Finest
67
69
  # r = nested_hash_value(a.last, key)
68
70
  def nested_hash_value(obj, key)
69
71
  if obj.respond_to?(:key?) && obj.key?(key)
70
- obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
72
+ obj[key].is_a?(Hash) ? self.class.new(json: obj[key]) : obj[key]
71
73
  elsif obj.respond_to?(:each)
72
74
  r = nil
73
75
  obj.find do |*a|
@@ -89,14 +91,15 @@ module Finest
89
91
 
90
92
  end
91
93
 
94
+ # Finest Struct
92
95
  module Struct
93
96
  class Error < StandardError; end
94
97
 
95
98
  include Helper
96
99
 
97
- def initialize(json = nil)
100
+ def initialize(**args)
98
101
  accessor_builder('to_h', {})
99
- json&.each do |k, v|
102
+ args[:json]&.each do |k, v|
100
103
  send("#{k}=", v)
101
104
  end
102
105
  end
@@ -119,6 +122,7 @@ module Finest
119
122
 
120
123
  end
121
124
 
125
+ # Finest Builder
122
126
  module Builder
123
127
  class Error < StandardError; end
124
128
 
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  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: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduard Garcia Castello
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-29 00:00:00.000000000 Z
11
+ date: 2021-08-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
15
+ - edugarcas@gmail.com
15
16
  - eduard@rzilient.club
16
17
  executables: []
17
18
  extensions: []
@@ -55,6 +56,6 @@ requirements: []
55
56
  rubygems_version: 3.1.6
56
57
  signing_key:
57
58
  specification_version: 4
58
- summary: Builder modules to create either class ghost methods from a given json or
59
- an open struct.
59
+ summary: Builder modules to create either class ghost methods from a given JSON or
60
+ a OpenStruct
60
61
  test_files: []