finest-builder 0.0.5 → 0.1.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: 2136b6f27d34551ee3104dc5d8a33d91ba2d8987a34fc702fc3e0a4d58f2647a
4
- data.tar.gz: 0c83bd8e15c4583d19e5dddd070f65705b6f799bac2e10bb90db13596211e495
3
+ metadata.gz: b91d61c258c8406ee407a8e2cd5bc94b2023ab31e5bfaf7bb66e7db3ae4a0278
4
+ data.tar.gz: 8696f9e7ac6b50084b4d72d5569a25736bc60e68957136eff76eee3e0b2abdad
5
5
  SHA512:
6
- metadata.gz: 9a5d9885b9f3669d14ddf8e30a2d81c68f890db213d2dc15783108b9dae5c553724207de5ed4734f33d32366bc499e785c647ea6ff93df05b16616304f445f23
7
- data.tar.gz: 01c405f3ee506d4f2b11436c9c08dd9c1300005d0345f87e896e31a31c92447186515ffe8cc3f7418a6ad21b4ead8380d91c6c59d706b1a008b917f2634ee996
6
+ metadata.gz: fdb2e7dbd54e9016f60efaaceba7fa064424e7a5c13b5e7ef5d11127dc1c7ebba72fdd7155a1658568b8473390592619dec07d45ab9207e39c9c0b2cb24b8e05
7
+ data.tar.gz: cb262897300489b7c04bb65dbc39db43b6efb10f8b00a09850fe9565d64360cc66b18553067c3fda7a8ef6fbe07b7b2637d7b261be89da43c6342222bed62f6a
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Finest::Builder ![Travis](https://travis-ci.org/eddygarcas/finest-builder.svg) [![Gem Version](https://badge.fury.io/rb/finest-builder.svg)](https://badge.fury.io/rb/finest-builder)
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
 
@@ -1,8 +1,10 @@
1
- require "finest/builder/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'finest/builder/version'
2
4
 
3
5
  class String
4
6
  def snake_case
5
- length > 7 ? gsub(/(\w[A-Z])/) { |e| "#{e[0]}_#{e[1].downcase}" }.downcase : downcase
7
+ length > 7 ? strip.gsub(/(\w[A-Z]|\s\S)/) { |e| "#{e[0].strip}_#{e[1].strip.downcase}" }.downcase : strip.downcase
6
8
  end
7
9
  end
8
10
 
@@ -27,13 +29,14 @@ module Finest
27
29
  #
28
30
  def build_by_keys(json = {}, keys = nil)
29
31
  k = keys || json&.keys
30
- raise ArgumentError "keys argument is not an array" unless k&.respond_to?(:each)
32
+ raise ArgumentError 'keys argument is not an array' unless k&.respond_to?(:each)
33
+
31
34
  accessor_builder('to_h', {}) unless self.class.method_defined?(:as_json)
32
35
  json.transform_keys!(&:to_s)
33
36
  k&.reject! { |ky| ky.end_with?('=') }
34
37
  k&.each do |key|
35
- self.send("#{key.to_s.snake_case}=", nested_hash_value(json, key.to_s))
36
- @to_h&.merge!({ key.to_s.snake_case.to_sym => self.send("#{key.to_s.snake_case}") })
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}") })
37
40
  end
38
41
  yield self if block_given?
39
42
  self
@@ -41,9 +44,9 @@ module Finest
41
44
 
42
45
  # Builds an instance variable as well as its class method accessors from a key value pair.
43
46
  def accessor_builder(k, v)
44
- self.instance_variable_set("@#{k}", v)
45
- self.class.send(:define_method, "#{k}", proc { self.instance_variable_get("@#{k}") })
46
- self.class.send(:define_method, "#{k}=", proc { |v| self.instance_variable_set("@#{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) })
47
50
  end
48
51
 
49
52
  #Goes through a complex Hash nest and gets the value of a passed key.
@@ -78,6 +81,8 @@ 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
@@ -92,29 +97,33 @@ module Finest
92
97
  def initialize(json = nil)
93
98
  accessor_builder('to_h', {})
94
99
  json&.each do |k, v|
95
- self.send("#{k}=", v)
100
+ send("#{k}=", v)
96
101
  end
97
102
  end
98
103
 
99
104
  def method_missing(name, *args)
100
105
  attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
101
106
  if attribute =~ /=$/
102
- if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
103
- @to_h[attribute.chop] = self.class.new(args[0])
104
- else
105
- @to_h[attribute.chop] = args[0]
106
- end
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
107
113
  else
108
114
  @to_h[attribute]
109
115
  end
110
116
  end
117
+
118
+ def respond_to_missing?; end
119
+
111
120
  end
112
121
 
113
122
  module Builder
114
123
  class Error < StandardError; end
115
124
 
116
125
  include Helper
117
- alias_method :initialize, :build_by_keys
126
+ alias initialize build_by_keys
118
127
  end
119
128
 
120
129
  end
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "0.0.5"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finest-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.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-04-29 00:00:00.000000000 Z
11
+ date: 2021-07-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -52,7 +52,7 @@ 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.4
55
+ rubygems_version: 3.1.6
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: Builder modules to create either class ghost methods from a given json or