mysticonfig 0.0.1 → 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: 8c5f9c4fb90ae354a44fa7482acf2fc3e3350735b3c11056041f27b6d521232c
4
- data.tar.gz: af6ce7b30d055dbbd2607d45d22cfda485e73ce373fa06837c3b901ae562cc7c
3
+ metadata.gz: 20e1e44f2df29f68de88c7a553a10b761b8528451217636d8ffa7c9f01c04e2a
4
+ data.tar.gz: 359a08038b71b236b6b4d35ef1e841937fa29c25c8e7e3e446c9f1ec8d57b5ec
5
5
  SHA512:
6
- metadata.gz: e8acfcf0d72f0542f291e143411462892dcfdf0d0380711665e457b9b24305053d385dc568b3eeee3ce7998fd97582b5fefe0718aa7f3ef1f406cbf277d6212f
7
- data.tar.gz: 4b6c5f9f00cfa2dc809fae164e347e8833dca0a580d4d95a32d3b1e7a14c11303b23aa2256fc452721350a90631155c6155948d19143439c6bee0db089152118
6
+ metadata.gz: 378f5488068ed8b63276cb41b81105feaef7e7747b6bab14e6e2c0a738a1dae8def0cda9b345ba989edbb22364cd40ac0852d5d579fc64b36259edbfeed1796e
7
+ data.tar.gz: 6f9853d8f2ba64230eb75f2c798bf404c046fdd708d421c3f0516f1cfd42bc7fdf28245ee05828142d4e480c4a7405d80827184b971e7f76f36dc54867186797
data/README.md CHANGED
@@ -12,6 +12,9 @@
12
12
 
13
13
  A library to load `.appnamerc`, `.appnamerc.json` or `.appnamerc.yaml` config file easily.
14
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
+
15
18
  > Gem's name inspired by [cosmiconfig](https://github.com/davidtheclark/cosmiconfig)
16
19
 
17
20
  ## Installation
@@ -22,6 +25,8 @@ gem install mysticonfig
22
25
 
23
26
  ## Usage
24
27
 
28
+ ### Normal
29
+
25
30
  ```ruby
26
31
  require 'mysticonfig'
27
32
 
@@ -30,3 +35,24 @@ config = loader.load # Automatically detect and load config
30
35
  json_config = loader.load_json # Only load config from JSON file
31
36
  yaml_config = loader.load_yaml # Only load config from YAML file (.yaml or .yml)
32
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/
data/lib/mysticonfig.rb CHANGED
@@ -6,8 +6,9 @@ module Mysticonfig
6
6
  ##
7
7
  # Configuration loader.
8
8
  class Loader
9
- def initialize(appname)
9
+ def initialize(appname, default_config = {})
10
10
  @filenames = Utils.generate_config_filenames appname
11
+ @default_config = default_config
11
12
  end
12
13
 
13
14
  ##
@@ -15,7 +16,8 @@ module Mysticonfig
15
16
  def load
16
17
  config_file = find_file @filenames
17
18
 
18
- Utils.load_auto config_file
19
+ config = Utils.load_auto config_file
20
+ config.empty? ? @default_config : @default_config.merge(config)
19
21
  end
20
22
 
21
23
  ##
@@ -23,7 +25,8 @@ module Mysticonfig
23
25
  def load_json
24
26
  json_config_file = Utils.lookup_file @filenames[:json]
25
27
 
26
- Utils.load_json json_config_file
28
+ config = Utils.load_json json_config_file
29
+ config.empty? ? @default_config : @default_config.merge(config)
27
30
  end
28
31
 
29
32
  ##
@@ -34,10 +37,13 @@ module Mysticonfig
34
37
 
35
38
  yaml_config_files.each do |file|
36
39
  yaml_config_file = Utils.lookup_file file
37
- return Utils.load_yaml(yaml_config_file) unless yaml_config_file.nil?
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
38
44
  end
39
45
 
40
- {} # Return empty hash when can't load config file
46
+ @default_config # Return default config when can't load config file
41
47
  end
42
48
 
43
49
  private
@@ -5,7 +5,7 @@ require 'yaml'
5
5
  # Mystic Config
6
6
  module Mysticonfig
7
7
  ##
8
- # Utilies
8
+ # Utilities
9
9
  module Utils
10
10
  ##
11
11
  # Determine whether the given file is valid JSON file.
@@ -1,4 +1,4 @@
1
1
  # Mysticonfig
2
2
  module Mysticonfig
3
- VERSION = '0.0.1'.freeze
3
+ VERSION = '0.1.0'.freeze
4
4
  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.0.1
4
+ version: 0.1.0
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-12 00:00:00.000000000 Z
11
+ date: 2018-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json