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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e146958c0527929112ef202b28806eca032b460d5ab0db7fe7ed4d5071d0fbf
4
- data.tar.gz: a89df1b3b477cf4fa4104a4086d0426b0530f0f794fc6c2dca69c651321e9ddb
3
+ metadata.gz: 1ac8be2e6998bd2599cb0e89a25c0716b1c3891d90b077acd56079eb702d66a1
4
+ data.tar.gz: 67cf57d634ea3edda51266744019bb82d68cbf2aaa988c49ab50911d70a3bad1
5
5
  SHA512:
6
- metadata.gz: a42f861011bd9b67bd10f665ffc90d1e4724d9e35e0cc9dcdbcaf81adf667790e90a2d12bcd10dd9db24485ca580792ce1271d1fc89076e067304fccd077597f
7
- data.tar.gz: b70fe07db6f8214e09505f9e7db3c4be89e061183b6f03d01b0ed3dd5e684ecbf538abf26b4e521b8523255bb7a260331aa20ecedf24354df2cee7b8fe2b1e1e
6
+ metadata.gz: b075bae8a917b4a26f961f9d07ac819d273f8fb67483b87d68b3676c2b75e2c1a1bccf340724d37b47b9e38372b27896d4d190894b317621610fb89d05f62719
7
+ data.tar.gz: d8446bbc192a8b192f6cfd00cd9c83628773f595242b5aedf48cda5e580345ab9474eac255172daf0f5ae89aab9993ee2ec834593de48123dfdeaa28ac8f1110
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
  /.idea/*
10
10
  Gemfile.lock
11
11
  /.idea/
12
+ *.gem
13
+ /vendor/
data/.travis.yml CHANGED
@@ -2,5 +2,5 @@
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.7.1
5
+ - 2.7.4
6
6
  before_install: gem install bundler -v 2.1.4
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 = nil)
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(json = {},keys = nil)
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*
@@ -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.3.0')
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
 
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.2"
4
4
  end
5
5
  end
@@ -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(**args)
33
- k = args[:keys] || args.fetch(:json, {})&.keys
34
- raise ArgumentError unless k&.respond_to?(:each)
35
-
36
- accessor_builder('to_h', {}) unless self.class.method_defined?(:as_json)
37
- args.fetch(:json, {}).transform_keys!(&:to_s)
38
- k&.reject! { |ky| ky.end_with?('=') }
39
- k&.each do |key|
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(json: obj[key]) : obj[key]
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?; end
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(**args)
100
+ def initialize(json = {}, keys = [])
101
101
  accessor_builder('to_h', {})
102
- args[:json]&.each do |k, v|
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.0.0
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: 2021-08-16 00:00:00.000000000 Z
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.3.0
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