finest-builder 0.0.3 → 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: '018ef6f0c97be9bf47f517b2ac49831f84c679225d85f0755d8b9786d8908f7c'
4
- data.tar.gz: ac90aad8f8b4efab4736931e097d885a54119d8fdd2f1daad2b03653a4ce7d10
3
+ metadata.gz: 6e146958c0527929112ef202b28806eca032b460d5ab0db7fe7ed4d5071d0fbf
4
+ data.tar.gz: a89df1b3b477cf4fa4104a4086d0426b0530f0f794fc6c2dca69c651321e9ddb
5
5
  SHA512:
6
- metadata.gz: b018cc343c668f7a654d3c1609f6bbaa61ddbb59269312f07b9b46e4bcb27b2d4cb17ef3b0bbb05c7800588d646896e5be1775315a570ebed19714077403a9d0
7
- data.tar.gz: 4472b5431bb7b1159d5cd14e245a7eb3c3fa291ebcb82e56938252a27c8f26ce7248efbb72d219ab0d1578a9173df79c0673b7c9458614dd4b874a18c50fc0bf
6
+ metadata.gz: a42f861011bd9b67bd10f665ffc90d1e4724d9e35e0cc9dcdbcaf81adf667790e90a2d12bcd10dd9db24485ca580792ce1271d1fc89076e067304fccd077597f
7
+ data.tar.gz: b70fe07db6f8214e09505f9e7db3c4be89e061183b6f03d01b0ed3dd5e684ecbf538abf26b4e521b8523255bb7a260331aa20ecedf24354df2cee7b8fe2b1e1e
data/Gemfile CHANGED
@@ -3,5 +3,5 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in finest-builder.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
7
- gem "minitest", "~> 5.0"
6
+ gem "rake"
7
+ gem "minitest"
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/binky-builder.svg)](https://badge.fury.io/rb/binky-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
 
@@ -81,9 +81,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
81
81
 
82
82
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
83
 
84
+ Rake issues bundle install --path vendor/cache
85
+
84
86
  ## Contributing
85
87
 
86
- Bug reports and pull requests are welcome on GitHub at https://github.com/eddygarcas/finest-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/binky-builder/blob/master/CODE_OF_CONDUCT.md).
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eddygarcas/finest-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/Finest-builder/blob/master/CODE_OF_CONDUCT.md).
87
89
 
88
90
 
89
91
  ## License
@@ -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
@@ -1,37 +1,65 @@
1
- require "finest/builder/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'finest/builder/version'
4
+
5
+ # Add snake case in String
6
+ class String
7
+ def snake_case
8
+ length > 7 ? strip.gsub(/(\w[A-Z]|\s\S)/) { |e| "#{e[0].strip}_#{e[1].strip.downcase}" }.downcase : strip.downcase
9
+ end
10
+ end
2
11
 
3
12
  module Finest
13
+ # Finest Builder
4
14
  module Helper
5
15
 
6
- # Parses a given json structure looking for specific keys inside the structure.
7
- # Keys are given through a block.
8
- # The result of it it's stored on a instance variable called to_hash and accessible through accessors with same name.
9
- def build_by_keys(json = {}, keys = nil)
10
- k = keys || json&.keys
11
- raise ArgumentError "keys argument is not an array" unless k&.respond_to?(:each)
12
- accessor_builder('to_h',{}) unless self.class.method_defined?(:as_json)
13
- json.transform_keys!(&:to_s)
14
- k&.reject!{|ky| ky.end_with?('=')}
16
+ # Parses a given json structure looking for specific keys inside the structure if passed
17
+ #
18
+ # The result it's stored on a instance variable called to_h and accessible through accessor with same name
19
+ # as well as it created a instance method for every key.
20
+ # All methods are created using the snake case approach.
21
+ #
22
+ # e = MyObjectBuilder.new({"client"=> {"idSA"=>1,"id"=>3434, "ManagementType"=>"iOSUnsupervised"}})
23
+ #
24
+ # Result:
25
+ # e.client.to_h[:id_sa]
26
+ # e.client.id_sa
27
+ #
28
+ # Any key value less than three characters will just be down cased.
29
+ # e.client.to_h[:id]
30
+ # e.client.id
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?('=') }
15
39
  k&.each do |key|
16
- self.send("#{key}=",nested_hash_value(json, key.to_s))
17
- @to_h&.merge!({key.to_sym => nested_hash_value(json,key.to_s)})
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) })
18
42
  end
19
43
  yield self if block_given?
20
44
  self
21
45
  end
22
46
 
23
47
  # Builds an instance variable as well as its class method accessors from a key value pair.
24
- def accessor_builder(k, v)
25
- self.instance_variable_set("@#{k}", v)
26
- self.class.send(:define_method, "#{k}", proc {self.instance_variable_get("@#{k}")})
27
- self.class.send(:define_method, "#{k}=", proc {|v| self.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) })
28
52
  end
29
53
 
30
- #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.
31
55
  # First wil check whether the object has the key? method,
32
56
  # which will mean it's a Hash and also if the Hash the method parameter key
33
57
  # if obj.respond_to?(:key?) && obj.key?(key)
34
58
  #
59
+ # If result object is a hash itself, will call constructor method to parse this hash first.
60
+ #
61
+ # obj[key].is_a?(Hash) ? self.class.new(obj[key]) : obj[key]
62
+ #
35
63
  # If it's not a Hash will check if it's a Array instead,
36
64
  # checking out whether it responds to a Array.each method or not.
37
65
  # elsif obj.respond_to?(:each)
@@ -41,7 +69,7 @@ module Finest
41
69
  # r = nested_hash_value(a.last, key)
42
70
  def nested_hash_value(obj, key)
43
71
  if obj.respond_to?(:key?) && obj.key?(key)
44
- obj[key]
72
+ obj[key].is_a?(Hash) ? self.class.new(json: obj[key]) : obj[key]
45
73
  elsif obj.respond_to?(:each)
46
74
  r = nil
47
75
  obj.find do |*a|
@@ -51,45 +79,55 @@ module Finest
51
79
  end
52
80
  end
53
81
 
54
-
55
- def method_missing(name,*args)
56
- accessor_builder(name.to_s.gsub(/=$/,''), args[0]) if name.to_s =~ /=$/
82
+ def method_missing(name, *args)
83
+ accessor_builder(name.to_s.gsub(/=$/, ''), args[0]) if name.to_s =~ /=$/
57
84
  end
58
85
 
86
+ def respond_to_missing?; end
87
+
59
88
  def attribute_from_inner_key(elem, attr, in_key = nil)
60
- {attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s)}
89
+ { attr.to_sym => nested_hash_value(elem, in_key&.present? ? in_key : attr.to_s) }
61
90
  end
91
+
62
92
  end
63
93
 
94
+ # Finest Struct
64
95
  module Struct
65
96
  class Error < StandardError; end
97
+
66
98
  include Helper
67
99
 
68
- def initialize(json = nil)
69
- accessor_builder('to_h',{})
70
- json&.each do |k, v|
71
- self.send("#{k}=", v)
100
+ def initialize(**args)
101
+ accessor_builder('to_h', {})
102
+ args[:json]&.each do |k, v|
103
+ send("#{k}=", v)
72
104
  end
73
105
  end
74
106
 
75
107
  def method_missing(name, *args)
76
108
  attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
77
109
  if attribute =~ /=$/
78
- if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
79
- @to_h[attribute.chop] = self.class.new(args[0])
80
- else
81
- @to_h[attribute.chop] = args[0]
82
- end
110
+ @to_h[attribute.chop] =
111
+ if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
112
+ self.class.new(args[0])
113
+ else
114
+ args[0]
115
+ end
83
116
  else
84
117
  @to_h[attribute]
85
118
  end
86
119
  end
120
+
121
+ def respond_to_missing?; end
122
+
87
123
  end
88
124
 
125
+ # Finest Builder
89
126
  module Builder
90
127
  class Error < StandardError; end
128
+
91
129
  include Helper
92
- alias_method :initialize,:build_by_keys
130
+ alias initialize build_by_keys
93
131
  end
94
132
 
95
133
  end
@@ -1,5 +1,5 @@
1
1
  module Finest
2
2
  module Builder
3
- VERSION = "0.0.3"
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.0.3
4
+ version: 1.0.0
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-04-28 00:00:00.000000000 Z
11
+ date: 2021-08-16 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
@@ -52,9 +53,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
53
  - !ruby/object:Gem::Version
53
54
  version: '0'
54
55
  requirements: []
55
- rubygems_version: 3.1.2
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 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: []