load_file 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 370bdebd061e99a284e045ac276ebbf6525957656b133873acb19aa2206fcf6f
4
+ data.tar.gz: 1fdc0d2148d432bc65b61bcaa5e002e7e74eecc3bb0eb90721c6cceb0f7cfc70
5
+ SHA512:
6
+ metadata.gz: 87067068f85f261224bb1b19f0b164aa1f1c3f8a15f32c42253df95d43e71e3017fc714e5146731624050598c86b08b8c2ef7386dba2798c5e9cd90b65ac4c6e
7
+ data.tar.gz: b8208625d5c588b40e6a24c36627a755726c0d45ee9e4e7bc353a7da70e5234519f930729ce9a579d0d67bcad0c4af123b6a46b0db532c46910d8c02d75eee51
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ 1.0.0 includes:
6
+
7
+ - LoadFile.load
8
+ - LoadFile.load!
9
+ - LoadFile.load_files
10
+ - LoadFile.load_files!
11
+ - LoadFile.overload
12
+ - LoadFile.overload!
13
+ - LoadFile.overload_files
14
+ - LoadFile.overload_files!
15
+
16
+ to load / overload file(s) into desired constant.
17
+
18
+ ## 0.0.0
19
+
20
+ Init devyml gem.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in devyml.gemspec
6
+ gemspec
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ load_file (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ pry (0.11.3)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (12.3.1)
16
+ rspec (3.8.0)
17
+ rspec-core (~> 3.8.0)
18
+ rspec-expectations (~> 3.8.0)
19
+ rspec-mocks (~> 3.8.0)
20
+ rspec-core (3.8.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-expectations (3.8.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-mocks (3.8.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-support (3.8.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.0)
35
+ load_file!
36
+ pry
37
+ rake
38
+ rspec
39
+
40
+ BUNDLED WITH
41
+ 1.16.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Juanito Fatas
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.
@@ -0,0 +1,41 @@
1
+ # LoadFile
2
+
3
+ Load/Overload YAML/JSON file(s) into desired constant.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "load_file"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ LoadFile.load(file: "examples/.travis.yml", constant: :TravisConfig)
17
+
18
+ # now you can access TravisConfig for configs in examples/.travis.yml
19
+ TravisConfig
20
+ => {"sudo"=>false, "language"=>"ruby", "cache"=>"bundler", "rvm"=>["2.5.1"]}
21
+
22
+ LoadFile.load(file: "examples/app.json", constant: :HerokuApp)
23
+
24
+ # now you can access HerokuApp for configs in examples/app.json
25
+ HerokuApp
26
+ => {"name"=>"Small Sharp Tool",
27
+ ...
28
+ "environments"=>{"test"=>{"scripts"=>{"test"=>"bundle exec rake test"}}}}
29
+ ```
30
+
31
+ ### What if I want to override existing constant?
32
+
33
+ Use `overload` APIs.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/devyml.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "load_file"
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
+ require "pry"
10
+ Pry.start
@@ -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,6 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "Small Sharp Tool",
3
+ "description": "This app does one little thing, and does it well.",
4
+ "keywords": [
5
+ "productivity",
6
+ "HTML5",
7
+ "scalpel"
8
+ ],
9
+ "website": "https://small-sharp-tool.com/",
10
+ "repository": "https://github.com/jane-doe/small-sharp-tool",
11
+ "logo": "https://small-sharp-tool.com/logo.svg",
12
+ "success_url": "/welcome",
13
+ "scripts": {
14
+ "postdeploy": "bundle exec rake bootstrap"
15
+ },
16
+ "env": {
17
+ "SECRET_TOKEN": {
18
+ "description": "A secret key for verifying the integrity of signed cookies.",
19
+ "generator": "secret"
20
+ },
21
+ "WEB_CONCURRENCY": {
22
+ "description": "The number of processes to run.",
23
+ "value": "5"
24
+ }
25
+ },
26
+ "formation": {
27
+ "web": {
28
+ "quantity": 2,
29
+ "size": "Performance-M"
30
+ }
31
+ },
32
+ "image": "heroku/ruby",
33
+ "addons": [
34
+ "openredis",
35
+ {
36
+ "plan": "mongolab:shared-single-small",
37
+ "as": "MONGO"
38
+ },
39
+ {
40
+ "plan": "heroku-postgresql",
41
+ "options": {
42
+ "version": "9.5"
43
+ }
44
+ }
45
+ ],
46
+ "buildpacks": [
47
+ {
48
+ "url": "https://github.com/stomita/heroku-buildpack-phantomjs"
49
+ }
50
+ ],
51
+ "environments": {
52
+ "test": {
53
+ "scripts": {
54
+ "test": "bundle exec rake test"
55
+ }
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+ require "load_file/version"
3
+ require "load_file/loader"
4
+
5
+ # Module to load file(s) into constant
6
+ module LoadFile
7
+ # Loads file into constant.
8
+ #
9
+ # @param [String, Pathname, File] path to load the file from
10
+ # @param [String, Symbol] constant name to load into
11
+ # @return [Hash] loaded file content
12
+ # @return [NilClass] nil when file not exists
13
+ def self.load(file:, constant:)
14
+ ignore_file_not_exists do
15
+ reader = Loader.new(file, constant)
16
+ reader.set_constant
17
+ end
18
+ end
19
+
20
+ # Loads file into constant.
21
+ #
22
+ # @param [String, Pathname, File] path to load the file from
23
+ # @param [String, Symbol] constant name to load into
24
+ # @return [Hash] loaded file content, raises an error when file not exists
25
+ # @return [NilClass] nil when file not exists
26
+ def self.load!(file:, constant:)
27
+ reader = Loader.new(file, constant)
28
+ reader.set_constant
29
+ end
30
+
31
+ # Loads files into constant.
32
+ # Any file not exists will be ignored and not raise error.
33
+ #
34
+ # @param [Array<String>] list of files to load
35
+ # @param [String, Symbol] constant name to load into
36
+ # @return [Hash] last loaded file content
37
+ # @return [NilClass] nil when last file not exists
38
+ def self.load_files(files:, constant:)
39
+ files.each { |file| load(file: file, constant: constant) }
40
+ end
41
+
42
+ # Loads files into constant.
43
+ #
44
+ # @param [Array<String>] list of files to load
45
+ # @param [String, Symbol] constant name to load into
46
+ # @return [Hash] last loaded file content, raises an error when any file not exists
47
+ def self.load_files!(files:, constant:)
48
+ files.each { |file| load!(file: file, constant: constant) }
49
+ end
50
+
51
+ # Overload a `file` into `constant`.
52
+ # Same as `load`, but will override existing values in `constant`.
53
+ #
54
+ # @param [String, Pathname, File] path to overload the file from
55
+ # @param [String, Symbol] constant name to overload into
56
+ # @return [Hash] overloaded file content
57
+ # @return [NilClass] nil when file not exists
58
+ def self.overload(file:, constant:)
59
+ ignore_file_not_exists do
60
+ reader = Loader.new(file, constant)
61
+ reader.set_constant!
62
+ end
63
+ end
64
+
65
+ # Overload a `file` into `constant`.
66
+ # Same as `load!`, but will override existing values in `constant`.
67
+ #
68
+ # @param [String, Pathname, File] path to overload the file from
69
+ # @param [String, Symbol] constant name to overload into
70
+ # @return [Hash] overloaded file content, raises an error when file not exists
71
+ # @return [NilClass] nil when file not exists
72
+ def self.overload!(file:, constant:)
73
+ reader = Loader.new(file, constant)
74
+ reader.set_constant!
75
+ end
76
+
77
+ # Overload files into constant.
78
+ # Any file not exists will be ignored and not raise error.
79
+ # Same as `load_files`, but will override existing values in `constant`.
80
+ #
81
+ # @param [Array<String>] list of files to overload
82
+ # @param [String, Symbol] constant name to overload into
83
+ # @return [Hash] last overloaded file content
84
+ # @return [NilClass] nil when last file not exists
85
+ def self.overload_files(files:, constant:)
86
+ files.each { |file| overload(file: file, constant: constant) }
87
+ end
88
+
89
+ # Overload files into constant.
90
+ # Any file not exists will raise error.
91
+ # Same as `load_files!`, but will override existing values in `constant`.
92
+ #
93
+ # @param [Array<String>] list of files to overload
94
+ # @param [String, Symbol] constant name to overload into
95
+ # @return [Hash] last overloaded file content, raises an error when any file not exists
96
+ def self.overload_files!(files:, constant:)
97
+ files.each { |file| overload!(file: file, constant: constant) }
98
+ end
99
+
100
+ # @private
101
+ def self.ignore_file_not_exists
102
+ yield
103
+ rescue Errno::ENOENT
104
+ end
105
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+ require_relative "parser"
3
+
4
+ module LoadFile
5
+ # A class to load `file` into Hash, find or create constant by `constant_name`.
6
+ #
7
+ # @return [Hash] parsed YAML or JSON content
8
+ class Loader < Hash
9
+ def initialize(file, constant_name)
10
+ @file = file
11
+ @constant = find_or_define(constant_name)
12
+
13
+ update parsed_content
14
+ end
15
+
16
+ # Set `values` into `constant` if not exists.
17
+ def set_constant
18
+ each { |key, value| constant[key] ||= value }
19
+ end
20
+
21
+ # Override `values` into `constant`.
22
+ def set_constant!
23
+ each { |key, value| constant[key] = value }
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :file, :constant
29
+
30
+ def find_or_define(constant_name)
31
+ if Object.const_defined?(constant_name)
32
+ Object.const_get(constant_name)
33
+ else
34
+ Object.const_set(constant_name, {})
35
+ end
36
+ end
37
+
38
+ def parsed_content
39
+ case File.extname(file)
40
+ when ".yml", ".yaml"
41
+ Parser.yaml(content)
42
+ when ".json"
43
+ Parser.json(content)
44
+ else
45
+ raise_parser_error("don't know how to parse #{file}")
46
+ end
47
+ rescue Parser::ParserError
48
+ raise_parser_error("#{file} format is invalid")
49
+ end
50
+
51
+ def raise_parser_error(message)
52
+ raise Parser::ParserError, message
53
+ end
54
+
55
+ def content
56
+ IO.read(file)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ require "yaml"
3
+ require "json"
4
+
5
+ module LoadFile
6
+ class Parser
7
+ ParserError = Class.new(StandardError)
8
+
9
+ def self.yaml(content)
10
+ if present?(content)
11
+ YAML.safe_load(content, [Regexp, Symbol])
12
+ else
13
+ {}
14
+ end
15
+ rescue Psych::SyntaxError
16
+ raise ParserError
17
+ end
18
+
19
+ def self.json(content)
20
+ if present?(content)
21
+ JSON.parse(content)
22
+ else
23
+ {}
24
+ end
25
+ rescue JSON::ParserError
26
+ raise ParserError
27
+ end
28
+
29
+ def self.present?(string)
30
+ string && !string.empty?
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module LoadFile
3
+ VERSION = "1.0.0"
4
+ end
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "load_file/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "load_file"
7
+ spec.version = LoadFile::VERSION
8
+ spec.authors = ["Juanito Fatas"]
9
+ spec.email = ["me@juanitofatas.com"]
10
+
11
+ spec.summary = %q{A Ruby gem to load file into desired constant.}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/juanitofatas/load_file"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.0"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "pry"
29
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: load_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juanito Fatas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A Ruby gem to load file into desired constant.
70
+ email:
71
+ - me@juanitofatas.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/hack
86
+ - bin/setup
87
+ - examples/.travis.yml
88
+ - examples/app.json
89
+ - lib/load_file.rb
90
+ - lib/load_file/loader.rb
91
+ - lib/load_file/parser.rb
92
+ - lib/load_file/version.rb
93
+ - load_file.gemspec
94
+ homepage: https://github.com/juanitofatas/load_file
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.7.7
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A Ruby gem to load file into desired constant.
118
+ test_files: []