finest-builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f6bd0969d3381163aa131504fea2d04442e65c2534487ee0868111e3e4cf1fba
4
+ data.tar.gz: b4fb3de4f0660a3031faa62ab4c65c07b9d98e96f133d8ec8bf0a44eea068ac0
5
+ SHA512:
6
+ metadata.gz: 9aaf7f776df73d89f7b533a98f359b1a6722b2bcd27c532223c13850d400d1be194bc5ab4c7cd562ddb2c1f30c86fdc20c33f204b7cf2a6be68f386ac3478f2a
7
+ data.tar.gz: 545dec40b0e4b13887abd815908782a3322cba6363cc12bcdbfaedfdcb8b65fb604cf798e3084df2a4889679cd5d2ee4c2d8a1e070444764765e80262e9b4535
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea/*
10
+ Gemfile.lock
11
+ /.idea/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at edugarcas@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in finest-builder.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Eduard
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
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)
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'finest-builder'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install finest-builder
18
+
19
+ ## Usage
20
+ OpenStruct and accessor builder modules.
21
+
22
+ ### Used as OpenStruct
23
+ Initialize an instance using json data, including the Builder class on your class definition.
24
+ Once initialized just use the accessors as any other instance.
25
+ ```ruby
26
+ class Issue
27
+ include Finest::Struct
28
+
29
+ def initialize(json = nil)
30
+ super json
31
+ end
32
+ end
33
+
34
+ issue = Issue.new({id: 1234})
35
+ issue.id # => 1234
36
+ ```
37
+
38
+ ### Used on ActiveRecord models
39
+ Finest-Builder includes another helper that can be used to initialize *ActiveRecord* models based on its column names.
40
+ In case not using column names but an array of method names, new accessors would be crated to access those methods.
41
+ ```ruby
42
+ class Issue < ApplicationRecord
43
+ include Finest::Helper
44
+ end
45
+
46
+ issue = Issue.new.build_by_keys({id: 1234,text: "hocus pocus"},Issue.column_names) # => Issue.column_names = id:
47
+ issue.as_json # => {id: 1234}
48
+ issue.to_h # => nil
49
+
50
+ issue = Issue.new.build_by_keys({id: 1234,text: "hocus pocus"}) # => Issue.column_names = id:
51
+ issue.id # => {id: 1234}
52
+ issue.text # => {text: "hocus pocus"}
53
+ issue.as_json #=> {id: 1234,text: "hocus pocus"}
54
+ issue.to_h # => nil
55
+ ```
56
+
57
+
58
+ Call *build_by_keys* method once the model has been initialized passing a json message,
59
+ it would *yield* itself as a block in case you want to perform further actions.
60
+ ```ruby
61
+ build_by_keys(json = {},keys = nil)
62
+ ```
63
+ This method would also create an instance variable called *@to_h* contains a pair-value hash as a result.
64
+ *@to_h* instance variable won't be available if the class inherits from *ActiveRecord::Base*
65
+
66
+ ### Auxiliary methods
67
+ Finest-Builder comes with two extra methods to search and create instance variable and methods.
68
+ It creates a instance variable along with its accessor methods (read/write).
69
+ ```ruby
70
+ accessor_builder(key, value)
71
+ ```
72
+ This method goes through the whole object, being a hash, looking for the passed key and return the value found.
73
+ ```ruby
74
+ nested_hash_value(obj, key)
75
+ ```
76
+
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
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
+
84
+ ## Contributing
85
+
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).
87
+
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
92
+
93
+ ## Code of Conduct
94
+
95
+ Everyone interacting in the Finest::Builder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/binky-builder/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "finest/builder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/finest/builder/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "finest-builder"
5
+ spec.version = Finest::Builder::VERSION
6
+ spec.authors = ["Eduard Garcia Castello"]
7
+ spec.email = ["eduard@rzilient.club"]
8
+
9
+ spec.summary = %q{Builder modules to create either class ghost methods from a given json or an open struct.}
10
+ #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")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
16
+
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"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # 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
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,95 @@
1
+ require "binky/builder/version"
2
+
3
+ module Binky
4
+ module Helper
5
+
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?('=')}
15
+ 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)})
18
+ end
19
+ yield self if block_given?
20
+ self
21
+ end
22
+
23
+ # 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)})
28
+ end
29
+
30
+ #Goes through a complex Hash nest and gets the value of a passed key.
31
+ # First wil check whether the object has the key? method,
32
+ # which will mean it's a Hash and also if the Hash the method parameter key
33
+ # if obj.respond_to?(:key?) && obj.key?(key)
34
+ #
35
+ # If it's not a Hash will check if it's a Array instead,
36
+ # checking out whether it responds to a Array.each method or not.
37
+ # elsif obj.respond_to?(:each)
38
+ #
39
+ # For every Array found it make a recursive call to itself passing
40
+ # the last element of the array and the Key it's looking for.
41
+ # r = nested_hash_value(a.last, key)
42
+ def nested_hash_value(obj, key)
43
+ if obj.respond_to?(:key?) && obj.key?(key)
44
+ obj[key]
45
+ elsif obj.respond_to?(:each)
46
+ r = nil
47
+ obj.find do |*a|
48
+ r = nested_hash_value(a.last, key)
49
+ end
50
+ r
51
+ end
52
+ end
53
+
54
+
55
+ def method_missing(name,*args)
56
+ accessor_builder(name.to_s.gsub(/=$/,''), args[0]) if name.to_s =~ /=$/
57
+ end
58
+
59
+ 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)}
61
+ end
62
+ end
63
+
64
+ module Struct
65
+ class Error < StandardError; end
66
+ include Helper
67
+
68
+ def initialize(json = nil)
69
+ accessor_builder('to_h',{})
70
+ json&.each do |k, v|
71
+ self.send("#{k}=", v)
72
+ end
73
+ end
74
+
75
+ def method_missing(name, *args)
76
+ attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
77
+ 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
83
+ else
84
+ @to_h[attribute]
85
+ end
86
+ end
87
+ end
88
+
89
+ module Builder
90
+ class Error < StandardError; end
91
+ include Helper
92
+ alias_method :initialize,:build_by_keys
93
+ end
94
+
95
+ end
@@ -0,0 +1,5 @@
1
+ module Finest
2
+ module Builder
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finest-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eduard Garcia Castello
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - eduard@rzilient.club
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - finest-builder.gemspec
30
+ - lib/finest/builder.rb
31
+ - lib/finest/builder/version.rb
32
+ homepage: https://github.com/eddygarcas/finest-builder
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org/
37
+ homepage_uri: https://github.com/eddygarcas/finest-builder
38
+ source_code_uri: https://github.com/eddygarcas/finest-builder
39
+ changelog_uri: https://github.com/eddygarcas/finest-builder
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.3.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.1.4
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Builder modules to create either class ghost methods from a given json or
59
+ an open struct.
60
+ test_files: []