mysticonfig 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20e1e44f2df29f68de88c7a553a10b761b8528451217636d8ffa7c9f01c04e2a
4
- data.tar.gz: 359a08038b71b236b6b4d35ef1e841937fa29c25c8e7e3e446c9f1ec8d57b5ec
3
+ metadata.gz: c413c8ed3554a42ad4ec25365550ffdee5aeb0ad33d9227c2fa8f455388aca3f
4
+ data.tar.gz: 2ee636a8d20690ec535f2255b700e29896fbdceba13e8a4e42e46b57945040af
5
5
  SHA512:
6
- metadata.gz: 378f5488068ed8b63276cb41b81105feaef7e7747b6bab14e6e2c0a738a1dae8def0cda9b345ba989edbb22364cd40ac0852d5d579fc64b36259edbfeed1796e
7
- data.tar.gz: 6f9853d8f2ba64230eb75f2c798bf404c046fdd708d421c3f0516f1cfd42bc7fdf28245ee05828142d4e480c4a7405d80827184b971e7f76f36dc54867186797
6
+ metadata.gz: 3f741e13986190f46ecee3519cdf21cbcabec6e88ab5fa70d15dc52464ecdce72f780d4c153cacb338b5c91ff27d98e4813d18bedb9a90f287c9bbe820348ce6
7
+ data.tar.gz: c65ea5327c013d60965691435ad13e5a4548e310a654c3c5b09e8792d3fbb90087eff4db9ff07ca3ca085834c4749047f3608dceec5e437c888a2886c066a55f
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2018 Saran Tanpituckpong
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 all
13
- 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 THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Saran Tanpituckpong
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,58 +1,58 @@
1
- # mysticonfig
2
- [![license](https://img.shields.io/github/license/gluons/mysticonfig.svg?style=flat-square)](./LICENSE)
3
- [![Gem](https://img.shields.io/gem/v/mysticonfig.svg?style=flat-square)](https://rubygems.org/gems/mysticonfig)
4
- [![Gem](https://img.shields.io/gem/dt/mysticonfig.svg?style=flat-square)](https://rubygems.org/gems/mysticonfig)
5
- [![Gemnasium](https://img.shields.io/gemnasium/gluons/mysticonfig.svg?style=flat-square)](https://gemnasium.com/github.com/gluons/mysticonfig)
6
- [![Travis](https://img.shields.io/travis/gluons/mysticonfig.svg?style=flat-square)](https://travis-ci.org/gluons/mysticonfig)
7
- <br><br>
8
- <p align="center">
9
- <strong>🔮 The configuration loader for wizard.</strong>
10
- </p>
11
- <br>
12
-
13
- A library to load `.appnamerc`, `.appnamerc.json` or `.appnamerc.yaml` config file easily.
14
-
15
- `mysticonfig` will lookup for your config file.
16
- It keeps traversing up until it **finds the config file** or **reaches your home directory**.
17
-
18
- > Gem's name inspired by [cosmiconfig](https://github.com/davidtheclark/cosmiconfig)
19
-
20
- ## Installation
21
-
22
- ```bash
23
- gem install mysticonfig
24
- ```
25
-
26
- ## Usage
27
-
28
- ### Normal
29
-
30
- ```ruby
31
- require 'mysticonfig'
32
-
33
- loader = Mysticonfig::Loader.new 'appname'
34
- config = loader.load # Automatically detect and load config
35
- json_config = loader.load_json # Only load config from JSON file
36
- yaml_config = loader.load_yaml # Only load config from YAML file (.yaml or .yml)
37
- ```
38
-
39
- ### With default config fallback
40
-
41
- ```ruby
42
- require 'mysticonfig'
43
-
44
- DEFAULT_CONFIG = {
45
- 'a' => 'A',
46
- 'b' => 'B',
47
- 'c' => 'C'
48
- }
49
-
50
- loader = Mysticonfig::Loader.new('appname', DEFAULT_CONFIG)
51
- config = loader.load # Automatically detect and load config
52
- json_config = loader.load_json # Only load config from JSON file
53
- yaml_config = loader.load_yaml # Only load config from YAML file (.yaml or .yml)
54
- ```
55
-
56
- ## Documentation
57
-
58
- See https://gluons.github.io/mysticonfig/
1
+ # mysticonfig
2
+ [![license](https://img.shields.io/github/license/gluons/mysticonfig.svg?style=flat-square)](./LICENSE)
3
+ [![Gem](https://img.shields.io/gem/v/mysticonfig.svg?style=flat-square)](https://rubygems.org/gems/mysticonfig)
4
+ [![Gem](https://img.shields.io/gem/dt/mysticonfig.svg?style=flat-square)](https://rubygems.org/gems/mysticonfig)
5
+ [![Gemnasium](https://img.shields.io/gemnasium/gluons/mysticonfig.svg?style=flat-square)](https://gemnasium.com/github.com/gluons/mysticonfig)
6
+ [![Travis](https://img.shields.io/travis/gluons/mysticonfig.svg?style=flat-square)](https://travis-ci.org/gluons/mysticonfig)
7
+ <br><br>
8
+ <p align="center">
9
+ <strong>🔮 The configuration loader for wizard.</strong>
10
+ </p>
11
+ <br>
12
+
13
+ A library to load `.appnamerc`, `.appnamerc.json` or `.appnamerc.yaml` config file easily.
14
+
15
+ `mysticonfig` will lookup for your config file.
16
+ It keeps traversing up until it **finds the config file** or **reaches your home directory**.
17
+
18
+ > Gem's name inspired by [cosmiconfig](https://github.com/davidtheclark/cosmiconfig)
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ gem install mysticonfig
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### Normal
29
+
30
+ ```ruby
31
+ require 'mysticonfig'
32
+
33
+ loader = Mysticonfig::Loader.new 'appname'
34
+ config = loader.load # Automatically detect and load config
35
+ json_config = loader.load_json # Only load config from JSON file
36
+ yaml_config = loader.load_yaml # Only load config from YAML file (.yaml or .yml)
37
+ ```
38
+
39
+ ### With default config fallback
40
+
41
+ ```ruby
42
+ require 'mysticonfig'
43
+
44
+ DEFAULT_CONFIG = {
45
+ 'a' => 'A',
46
+ 'b' => 'B',
47
+ 'c' => 'C'
48
+ }
49
+
50
+ loader = Mysticonfig::Loader.new('appname', DEFAULT_CONFIG)
51
+ config = loader.load # Automatically detect and load config
52
+ json_config = loader.load_json # Only load config from JSON file
53
+ yaml_config = loader.load_yaml # Only load config from YAML file (.yaml or .yml)
54
+ ```
55
+
56
+ ## Documentation
57
+
58
+ See https://gluons.github.io/mysticonfig/
@@ -1,91 +1,91 @@
1
- require 'json'
2
- require 'yaml'
3
-
4
- ##
5
- # Mystic Config
6
- module Mysticonfig
7
- ##
8
- # Utilities
9
- module Utils
10
- ##
11
- # Determine whether the given file is valid JSON file.
12
- def self.json_file?(file)
13
- return false if file.nil?
14
-
15
- JSON.parse(File.read(file))
16
- true
17
- rescue
18
- false
19
- end
20
-
21
- ##
22
- # Determine whether the given file is valid YAML file.
23
- def self.yaml_file?(file)
24
- return false if file.nil? || json_file?(file)
25
-
26
- result = YAML.safe_load(File.read(file))
27
- result.is_a?(Hash)
28
- rescue
29
- false
30
- end
31
-
32
- ##
33
- # Generate config filenames from given app name.
34
- def self.generate_config_filenames(appname)
35
- return nil if appname.nil?
36
-
37
- {
38
- plain: ".#{appname}rc",
39
- json: ".#{appname}rc.json",
40
- yaml: [".#{appname}rc.yaml", ".#{appname}rc.yml"]
41
- }
42
- end
43
-
44
- ##
45
- # Load JSON config file.
46
- def self.load_json(config_file)
47
- return {} if config_file.nil? || !File.exist?(config_file)
48
-
49
- JSON.parse(File.read(config_file))
50
- end
51
-
52
- ##
53
- # Load YAML config file.
54
- def self.load_yaml(config_file)
55
- return {} if config_file.nil? || !File.exist?(config_file)
56
-
57
- YAML.safe_load(File.read(config_file))
58
- end
59
-
60
- ##
61
- # Load config file with automatic file type detection.
62
- def self.load_auto(config_file)
63
- return {} if config_file.nil? || !File.exist?(config_file)
64
-
65
- if json_file?(config_file)
66
- load_json(config_file)
67
- elsif yaml_file?(config_file)
68
- load_yaml(config_file)
69
- else
70
- {}
71
- end
72
- end
73
-
74
- ##
75
- # Look up for an existent config file.
76
- def self.lookup_file(config_file, dir = Dir.pwd)
77
- dir = File.realpath dir
78
- current_filepath = File.expand_path(config_file, dir)
79
- return current_filepath if File.exist?(current_filepath)
80
-
81
- # Stop on home directory or root directory.
82
- home_path = File.expand_path '~'
83
- is_home = dir == home_path
84
- is_root = dir == '/'
85
- return nil if is_home || is_root
86
-
87
- upper_path = File.realpath('..', dir)
88
- lookup_file(config_file, upper_path) # Traverse up
89
- end
90
- end
91
- end
1
+ require 'json'
2
+ require 'yaml'
3
+
4
+ ##
5
+ # Mystic Config
6
+ module Mysticonfig
7
+ ##
8
+ # Utilities
9
+ module Utils
10
+ ##
11
+ # Determine whether the given file is valid JSON file.
12
+ def self.json_file?(file)
13
+ return false if file.nil?
14
+
15
+ JSON.parse(File.read(file))
16
+ true
17
+ rescue
18
+ false
19
+ end
20
+
21
+ ##
22
+ # Determine whether the given file is valid YAML file.
23
+ def self.yaml_file?(file)
24
+ return false if file.nil? || json_file?(file)
25
+
26
+ result = YAML.safe_load(File.read(file))
27
+ result.is_a?(Hash)
28
+ rescue
29
+ false
30
+ end
31
+
32
+ ##
33
+ # Generate config filenames from given app name.
34
+ def self.generate_config_filenames(appname)
35
+ return nil if appname.nil?
36
+
37
+ {
38
+ plain: ".#{appname}rc",
39
+ json: ".#{appname}rc.json",
40
+ yaml: [".#{appname}rc.yaml", ".#{appname}rc.yml"]
41
+ }
42
+ end
43
+
44
+ ##
45
+ # Load JSON config file.
46
+ def self.load_json(config_file)
47
+ return {} if config_file.nil? || !File.exist?(config_file)
48
+
49
+ JSON.parse(File.read(config_file))
50
+ end
51
+
52
+ ##
53
+ # Load YAML config file.
54
+ def self.load_yaml(config_file)
55
+ return {} if config_file.nil? || !File.exist?(config_file)
56
+
57
+ YAML.safe_load(File.read(config_file))
58
+ end
59
+
60
+ ##
61
+ # Load config file with automatic file type detection.
62
+ def self.load_auto(config_file)
63
+ return {} if config_file.nil? || !File.exist?(config_file)
64
+
65
+ if json_file?(config_file)
66
+ load_json(config_file)
67
+ elsif yaml_file?(config_file)
68
+ load_yaml(config_file)
69
+ else
70
+ {}
71
+ end
72
+ end
73
+
74
+ ##
75
+ # Look up for an existent config file.
76
+ def self.lookup_file(config_file, dir = Dir.pwd)
77
+ dir = File.realpath dir
78
+
79
+ # Stop on home directory or root directory.
80
+ home_path = File.expand_path '~'
81
+ while (dir != home_path) && (dir != '/')
82
+ current_filepath = File.expand_path(config_file, dir)
83
+ return current_filepath if File.exist?(current_filepath)
84
+
85
+ dir = File.realpath('..', dir) # Traverse up
86
+ end
87
+
88
+ nil
89
+ end
90
+ end
91
+ end
@@ -1,4 +1,4 @@
1
- # Mysticonfig
2
- module Mysticonfig
3
- VERSION = '0.1.0'.freeze
4
- end
1
+ # Mysticonfig
2
+ module Mysticonfig
3
+ VERSION = '0.1.1'.freeze
4
+ end
data/lib/mysticonfig.rb CHANGED
@@ -1,75 +1,75 @@
1
- require_relative 'mysticonfig/utils'
2
-
3
- ##
4
- # Mystic Config
5
- module Mysticonfig
6
- ##
7
- # Configuration loader.
8
- class Loader
9
- def initialize(appname, default_config = {})
10
- @filenames = Utils.generate_config_filenames appname
11
- @default_config = default_config
12
- end
13
-
14
- ##
15
- # Find and load config file.
16
- def load
17
- config_file = find_file @filenames
18
-
19
- config = Utils.load_auto config_file
20
- config.empty? ? @default_config : @default_config.merge(config)
21
- end
22
-
23
- ##
24
- # Find and load JSON config file.
25
- def load_json
26
- json_config_file = Utils.lookup_file @filenames[:json]
27
-
28
- config = Utils.load_json json_config_file
29
- config.empty? ? @default_config : @default_config.merge(config)
30
- end
31
-
32
- ##
33
- # Find and load YAML config file.
34
- def load_yaml
35
- yaml_config_files = @filenames[:yaml]
36
- yaml_config_file = nil
37
-
38
- yaml_config_files.each do |file|
39
- yaml_config_file = Utils.lookup_file file
40
- unless yaml_config_file.nil?
41
- config = Utils.load_yaml(yaml_config_file)
42
- return config.empty? ? @default_config : @default_config.merge(config)
43
- end
44
- end
45
-
46
- @default_config # Return default config when can't load config file
47
- end
48
-
49
- private
50
-
51
- # rubocop:disable Metrics/MethodLength
52
-
53
- ##
54
- # Find an existent config file from all posible files.
55
- def find_file(filenames)
56
- return nil if filenames.nil?
57
-
58
- filenames.each_value do |value|
59
- if value.is_a? Array
60
- value.each do |file|
61
- file_path = Utils.lookup_file file
62
- return file_path unless file_path.nil?
63
- end
64
- else
65
- file_path = Utils.lookup_file value
66
- return file_path unless file_path.nil?
67
- end
68
- end
69
-
70
- nil
71
- end
72
-
73
- # rubocop:enable Metrics/MethodLength
74
- end
75
- end
1
+ require_relative 'mysticonfig/utils'
2
+
3
+ ##
4
+ # Mystic Config
5
+ module Mysticonfig
6
+ ##
7
+ # Configuration loader.
8
+ class Loader
9
+ def initialize(appname, default_config = {})
10
+ @filenames = Utils.generate_config_filenames appname
11
+ @default_config = default_config
12
+ end
13
+
14
+ ##
15
+ # Find and load config file.
16
+ def load
17
+ config_file = find_file @filenames
18
+
19
+ config = Utils.load_auto config_file
20
+ config.empty? ? @default_config : @default_config.merge(config)
21
+ end
22
+
23
+ ##
24
+ # Find and load JSON config file.
25
+ def load_json
26
+ json_config_file = Utils.lookup_file @filenames[:json]
27
+
28
+ config = Utils.load_json json_config_file
29
+ config.empty? ? @default_config : @default_config.merge(config)
30
+ end
31
+
32
+ ##
33
+ # Find and load YAML config file.
34
+ def load_yaml
35
+ yaml_config_files = @filenames[:yaml]
36
+ yaml_config_file = nil
37
+
38
+ yaml_config_files.each do |file|
39
+ yaml_config_file = Utils.lookup_file file
40
+ unless yaml_config_file.nil?
41
+ config = Utils.load_yaml(yaml_config_file)
42
+ return config.empty? ? @default_config : @default_config.merge(config)
43
+ end
44
+ end
45
+
46
+ @default_config # Return default config when can't load config file
47
+ end
48
+
49
+ private
50
+
51
+ # rubocop:disable Metrics/MethodLength
52
+
53
+ ##
54
+ # Find an existent config file from all posible files.
55
+ def find_file(filenames)
56
+ return nil if filenames.nil?
57
+
58
+ filenames.each_value do |value|
59
+ if value.is_a? Array
60
+ value.each do |file|
61
+ file_path = Utils.lookup_file file
62
+ return file_path unless file_path.nil?
63
+ end
64
+ else
65
+ file_path = Utils.lookup_file value
66
+ return file_path unless file_path.nil?
67
+ end
68
+ end
69
+
70
+ nil
71
+ end
72
+
73
+ # rubocop:enable Metrics/MethodLength
74
+ end
75
+ end
data/mysticonfig.gemspec CHANGED
@@ -1,30 +1,30 @@
1
- lib = File.expand_path('./lib', File.dirname(__FILE__))
2
-
3
- $LOAD_PATH.push lib unless $LOAD_PATH.include?(lib)
4
-
5
- require 'mysticonfig/version'
6
-
7
- DESCRIPTION = 'The configuration loader to load rc config file.'.freeze
8
-
9
- Gem::Specification.new do |s|
10
- s.name = 'mysticonfig'
11
- s.version = Mysticonfig::VERSION
12
- s.summary = 'The configuration loader for wizard.'
13
- s.description = DESCRIPTION
14
- s.files = ['mysticonfig.gemspec', 'LICENSE', 'Gemfile']
15
- s.files += Dir['*.md', 'lib/**/*']
16
- s.required_ruby_version = '>= 2.1.0'
17
- s.author = 'Saran Tanpituckpong'
18
- s.license = 'MIT'
19
- s.homepage = 'https://github.com/gluons/mysticonfig'
20
- s.metadata = {
21
- 'bug_tracker_uri' => 'https://github.com/gluons/mysticonfig/issues',
22
- 'homepage_uri' => 'https://gluons.github.io/mysticonfig/'
23
- }
24
-
25
- s.add_runtime_dependency 'json', '~> 2.1'
26
-
27
- s.add_development_dependency 'rake', '~> 12.3'
28
- s.add_development_dependency 'rspec', '~> 3.7'
29
- s.add_development_dependency 'rubocop', '~> 0.52.1'
30
- end
1
+ lib = File.expand_path('./lib', File.dirname(__FILE__))
2
+
3
+ $LOAD_PATH.push lib unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'mysticonfig/version'
6
+
7
+ DESCRIPTION = 'The configuration loader to load rc config file.'.freeze
8
+
9
+ Gem::Specification.new do |s|
10
+ s.name = 'mysticonfig'
11
+ s.version = Mysticonfig::VERSION
12
+ s.summary = 'The configuration loader for wizard.'
13
+ s.description = DESCRIPTION
14
+ s.files = ['mysticonfig.gemspec', 'LICENSE', 'Gemfile']
15
+ s.files += Dir['*.md', 'lib/**/*']
16
+ s.required_ruby_version = '>= 2.1.0'
17
+ s.author = 'Saran Tanpituckpong'
18
+ s.license = 'MIT'
19
+ s.homepage = 'https://github.com/gluons/mysticonfig'
20
+ s.metadata = {
21
+ 'bug_tracker_uri' => 'https://github.com/gluons/mysticonfig/issues',
22
+ 'homepage_uri' => 'https://gluons.github.io/mysticonfig/'
23
+ }
24
+
25
+ s.add_runtime_dependency 'json', '~> 2.1'
26
+
27
+ s.add_development_dependency 'rake', '~> 12.3'
28
+ s.add_development_dependency 'rspec', '~> 3.7'
29
+ s.add_development_dependency 'rubocop', '~> 0.55.0'
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysticonfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saran Tanpituckpong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-16 00:00:00.000000000 Z
11
+ date: 2018-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.52.1
61
+ version: 0.55.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.52.1
68
+ version: 0.55.0
69
69
  description: The configuration loader to load rc config file.
70
70
  email:
71
71
  executables: []
@@ -101,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  version: '0'
102
102
  requirements: []
103
103
  rubyforge_project:
104
- rubygems_version: 2.7.5
104
+ rubygems_version: 2.7.6
105
105
  signing_key:
106
106
  specification_version: 4
107
107
  summary: The configuration loader for wizard.