confg 1.0.3 → 2.0.0.rc1

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
- SHA1:
3
- metadata.gz: 3a22248aee15636933d13104780068971f2663a3
4
- data.tar.gz: e8b1bbf96b54edf9cf796ed5c49724df47b93f87
2
+ SHA256:
3
+ metadata.gz: 50b51215735b3ac4f8caedb04e36ae28176bb1bd6e7c14c2e3cdced3e7480a1e
4
+ data.tar.gz: 95b45d10ab8cb6751afd546d28f4fa76d48e56c4e2746489c5a91cb8dcfcba07
5
5
  SHA512:
6
- metadata.gz: 2e7cf98c81efeb762c04acc64c82e7c15a5792d9b54c8a1de90433562e4fcd27f797fc09869abaa297b9881270d40b9d3dd06017984f43337cc82527129092b8
7
- data.tar.gz: 12dfb2ab1b2d4c6c1a1e5cff7adf12a0e3ffc8919686e09a72f54cffc9b251f4fcaeb37e27faa7b20e7f81b1d2a5957cc4caf348eb7e876498b3eadf89eddc58
6
+ metadata.gz: c94266338ff570a78f1db7e736be6264f89658d3c96711c78871a34a4a01746ad4b54eb117d909c07857a52549a3d8bd8f9898648611fb809560e656dfa360b8
7
+ data.tar.gz: a2c2042c39354842a10b72d5616dc725ba538b6f7e0ccd01ebd88f98fd84f74635949158aa4e30928c2d4738bc9b02b8faf0a6976563231fce48ce409296ca9b
data/Rakefile CHANGED
@@ -1 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -1,21 +1,25 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'confg/version'
5
+ require "confg/version"
5
6
 
6
- Gem::Specification.new do |gem|
7
- gem.name = "confg"
8
- gem.version = Confg::VERSION
9
- gem.authors = ["Mike Nelson"]
10
- gem.email = ["mike@mnelson.io"]
11
- gem.description = %q{Config the pipes}
12
- gem.summary = %q{Sets shared variables for applications}
13
- gem.homepage = ""
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "confg"
9
+ spec.version = Confg::VERSION
10
+ spec.authors = ["Mike Nelson"]
11
+ spec.email = ["mike@mnelson.io"]
12
+ spec.description = "Config the pipes"
13
+ spec.summary = "Sets shared variables for applications"
14
+ spec.homepage = ""
14
15
 
15
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
+ spec.executables = spec.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
19
20
 
20
- gem.add_dependency 'activesupport', '>= 3.0.0'
21
+ spec.add_development_dependency "bundler", "~> 1.17"
22
+ spec.add_development_dependency "byebug"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ spec.add_development_dependency "rake", "~> 10.0"
21
25
  end
@@ -1,13 +1,23 @@
1
- require 'confg/version'
2
- require 'confg/configuration'
3
- require 'confg/erb_context'
1
+ # frozen_string_literal: true
2
+
3
+ require "confg/version"
4
+ require "confg/configuration"
5
+ require "confg/erb_context"
4
6
 
5
7
  module Confg
6
8
 
7
9
  class << self
8
10
 
9
11
  def root
10
- @root ||= Pathname.new(calc_root_string).expand_path
12
+ return @root if defined?(@root)
13
+
14
+ @root = calc_root_path
15
+ end
16
+
17
+ def env
18
+ return @env if defined?(@env)
19
+
20
+ @env = calc_env_string
11
21
  end
12
22
 
13
23
  def erb_function(function_name, &block)
@@ -17,29 +27,21 @@ module Confg
17
27
  self
18
28
  end
19
29
 
20
- def configure(raise_on_missed_key = false)
21
- @configuration ||= ::Confg::Configuration.new(raise_on_missed_key)
30
+ def configure
31
+ @configuration ||= ::Confg::Configuration.new(env: env, root: root)
22
32
  yield @configuration if block_given?
23
33
  @configuration
24
34
  end
25
- alias_method :config, :configure
35
+ alias config configure
26
36
 
27
37
  def method_missing(method_name, *args, &block)
28
38
  config.send(method_name, *args, &block)
29
39
  end
30
40
 
31
- def respond_to_missing?(*args)
41
+ def respond_to_missing?(*_args)
32
42
  true
33
43
  end
34
44
 
35
- def get(path)
36
- thing = self
37
- path.split('.').each do |piece|
38
- thing = thing.try(piece)
39
- end
40
- thing
41
- end
42
-
43
45
  protected
44
46
 
45
47
  def calc_root_string
@@ -47,13 +49,27 @@ module Confg
47
49
  return RAILS_ROOT if defined?(RAILS_ROOT)
48
50
  return RACK_ROOT if defined?(RACK_ROOT)
49
51
 
50
- ENV['RAILS_ROOT'] || ENV['RACK_ROOT'] || Dir.pwd
52
+ ENV["RAILS_ROOT"] || ENV["RACK_ROOT"] || Dir.pwd
53
+ end
54
+
55
+ def calc_root_path
56
+ ::Pathname.new(calc_root_string).expand_path
57
+ end
58
+
59
+ def calc_env_string
60
+ return ::Rails.env.to_s if defined?(::Rails)
61
+ return RAILS_ENV if defined?(RAILS_ENV)
62
+ return RACK_ENV if defined?(RACK_ENV)
63
+
64
+ ENV["RAILS_ENV"] || ENV["RACK_ENV"] || nil
51
65
  end
52
66
 
53
67
  def reset!
54
- remove_instance_variable("@configuration") if defined?(@configuration)
55
- remove_instance_variable("@root") if defined?(@root)
68
+ remove_instance_variable("@configuration") if defined?(@configuration)
69
+ remove_instance_variable("@env") if defined?(@env)
70
+ remove_instance_variable("@root") if defined?(@root)
56
71
  end
72
+
57
73
  end
58
74
 
59
75
  end
@@ -1,47 +1,62 @@
1
- require 'active_support/core_ext/module/delegation'
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
2
4
 
3
5
  module Confg
4
- class Configuration
6
+ class Configuration < ::SimpleDelegator
7
+
8
+ attr_reader :confg_env, :confg_root
9
+
10
+ def initialize(env: Confg.env, root: Confg.root)
11
+ @confg_env = env.to_s
12
+ @confg_root = Pathname.new(root)
5
13
 
6
- delegate :each, :inspect, :to => :@attributes
14
+ super({})
15
+ end
7
16
 
8
- def initialize(raise_error_on_miss = false, parent = nil)
9
- @attributes = {}
10
- @raise_error_on_miss = raise_error_on_miss
11
- @parent = parent
17
+ def tmp(key, value)
18
+ initial = get(key)
19
+ set(key, value)
20
+ yield
21
+ ensure
22
+ set(key, initial)
12
23
  end
13
24
 
14
- def merge(hash)
15
- hash.each do |k,v|
16
- self.set(k,v)
25
+ def merge(other)
26
+ other.each_pair do |k, v|
27
+ set(k, v)
17
28
  end
18
29
  end
19
- alias_method :merge!, :merge
30
+ alias merge! merge
20
31
 
21
- def to_hash
22
- out = @attributes.to_h.dup
23
- out.each_pair do |k,v|
24
- out[k] = v.to_hash if v.is_a?(self.class)
32
+ def to_h
33
+ __getobj__.transform_values do |v|
34
+ v.is_a?(self.class) ? v.to_h : v
25
35
  end
26
- out
27
36
  end
28
- alias_method :to_h, :to_hash
29
37
 
30
- def [](key)
31
- self.get(key)
38
+ def get(key)
39
+ __getobj__[key.to_s]
32
40
  end
41
+ alias [] get
33
42
 
34
- def []=(key, value)
35
- self.set(key, value)
43
+ def get!(key)
44
+ __getobj__.fetch(key.to_s)
36
45
  end
37
46
 
38
- def tmp(key, value)
39
- initial = self[key]
40
- self[key] = value
41
- yield
42
- ensure
43
- self[key] = initial
47
+ def set(key, value = nil)
48
+ __getobj__[key.to_s] = case value
49
+ when ::Hash
50
+ set_block(key) do |child|
51
+ value.each_pair do |k, v|
52
+ child.set(k, v)
53
+ end
54
+ end
55
+ else
56
+ value
57
+ end
44
58
  end
59
+ alias []= set
45
60
 
46
61
  def load_key(key)
47
62
  # loads yaml file with given key
@@ -49,121 +64,84 @@ module Confg
49
64
  end
50
65
 
51
66
  def load_yaml(path, key: nil, ignore_env: false)
52
- path = find_config_yaml(path)
53
- raw_content = File.open(path, 'r'){|io| io.read } rescue nil
67
+ found_path = find_config_yaml(path)
54
68
 
55
- return unless raw_content
69
+ raise ArgumentError, "#{path} could not be found" if found_path.nil?
56
70
 
57
71
  ctxt = ::Confg::ErbContext.new
58
- content = ctxt.evaluate(raw_content)
72
+ raw_content = ::File.read(found_path)
73
+ erb_content = ctxt.evaluate(raw_content)
74
+ yaml_content = ::YAML.send :load, erb_content
59
75
 
60
76
  unless ignore_env
61
- env = defined?(Rails) ? Rails.env.to_s : ENV["RAILS_ENV"] || ENV["RACK_ENV"]
62
- content = content[Rails.env] if env && content.is_a?(::Hash) && content.has_key?(Rails.env)
77
+ yaml_content = yaml_content[confg_env] if confg_env && yaml_content.is_a?(::Hash) && yaml_content.key?(confg_env)
63
78
  end
64
79
 
65
80
  if key
66
- self.set(key, content)
81
+ set(key, yaml_content)
67
82
  else
68
- if content.is_a?(Array)
69
- raise "A key must be provided to load the file at: #{path}"
83
+ if yaml_content.is_a?(Array)
84
+ raise "A key must be provided to load the file at: #{found_path}"
70
85
  else
71
- content.each do |k,v|
72
- self.set(k, v)
86
+ yaml_content.each do |k, v|
87
+ set(k, v)
73
88
  end
74
89
  end
75
90
  end
76
91
  end
77
- alias_method :load_yml, :load_yaml
92
+ alias load_yml load_yaml
78
93
 
79
94
  def method_missing(method_name, *args, &block)
80
- if method_name.to_s =~ /^(.+)=$/ && !args.empty?
81
- self.set($1, args.first)
82
- elsif method_name.to_s =~ /^([^=]+)$/
83
- if block_given?
84
- self.set_block($1, &block)
85
- elsif @attributes.respond_to?($1)
86
- @attributes.send($1, *args)
87
- else
88
- self.get($1)
89
- end
90
- else
95
+ key = method_name.to_s
96
+
97
+ if __getobj__.respond_to?(key)
91
98
  super
99
+ elsif key.end_with?("=") && !args.empty?
100
+ set(key[0...-1], args[0])
101
+ elsif block_given?
102
+ set_block(key, &block)
103
+ else
104
+ get!(key)
92
105
  end
93
106
  end
94
107
 
95
- def respond_to?(method_name, include_private = false)
108
+ def respond_to_missing?(*_args)
96
109
  true
97
110
  end
98
111
 
99
112
  protected
100
113
 
101
- def set(key, value = nil)
102
- case value
103
- when ::Hash
104
- set_block key do |inner|
105
- value.each do |k,v|
106
- inner.set(k, v)
107
- end
108
- end
109
- else
110
- @attributes[key.to_s] = value
111
- end
112
- end
113
-
114
- def get(key)
115
- if @attributes.has_key?(key.to_s)
116
- @attributes[key.to_s]
117
- else
118
- get_missing_key(key.to_s)
119
- end
120
- end
121
-
122
- def get_missing_key(key)
123
- if @raise_error_on_miss
124
- raise "Missing key: #{key} in #{@attributes.inspect}"
125
- else
126
- nil
127
- end
128
- end
129
-
130
- def set_block(key, &block)
131
- inner = @attributes[key.to_s] || child_new
132
- block.call(inner)
114
+ def set_block(key)
115
+ inner = get(key) || spawn_child
116
+ yield(inner)
133
117
  set(key, inner)
134
118
  end
135
119
 
136
120
  def find_config_yaml(path)
137
121
  path = path.to_s
138
122
  # give it back if it starts with a slash
139
- return path if path =~ /^\//
123
+ if path.start_with?("/")
124
+ return nil unless ::File.file?(path)
125
+
126
+ return path
127
+ end
140
128
 
141
129
  to_try = []
142
- unless path =~ /.yml$/
143
- to_try << Confg.root.join("config/#{path}.yml")
130
+ unless path.end_with?(".yml")
131
+ to_try << confg_root.join("config/#{path}.yml")
144
132
  end
145
- to_try << Confg.root.join("config/#{path}")
146
- to_try << Confg.root.join(path)
133
+ to_try << confg_root.join("config/#{path}")
134
+ to_try << confg_root.join(path)
147
135
 
148
136
  to_try.each do |file|
149
- return file if File.file?(file)
137
+ return file.to_s if File.file?(file)
150
138
  end
151
139
 
152
- to_try.first
153
- end
154
-
155
- def child_class
156
- # same as ours
157
- self.class
158
- end
159
-
160
- def child_raise_error_on_miss
161
- # same as ours
162
- @raise_error_on_miss
140
+ nil
163
141
  end
164
142
 
165
- def child_new
166
- child_class.new(child_raise_error_on_miss, self)
143
+ def spawn_child
144
+ self.class.new(env: confg_env, root: confg_root)
167
145
  end
168
146
 
169
147
  end
@@ -1,12 +1,12 @@
1
- require 'yaml'
2
- require 'erb'
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
3
4
 
4
5
  module Confg
5
6
  class ErbContext
6
7
 
7
8
  def evaluate(raw_content)
8
- raw_content = ERB.new(raw_content).result(binding)
9
- YAML.load(raw_content)
9
+ ::ERB.new(raw_content).result(binding)
10
10
  end
11
11
 
12
12
  end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Confg
2
- VERSION = "1.0.3"
4
+
5
+ VERSION = "2.0.0.rc1"
6
+
3
7
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class ConfgTest < Minitest::Test
6
+
7
+ def config
8
+ @config ||= ::Confg::Configuration.new(root: __dir__, env: "test")
9
+ end
10
+
11
+ def test_simple_keys_can_be_assigned
12
+ config.foo = "bar"
13
+ assert_equal "bar", config.foo
14
+ assert_equal({ "foo" => "bar" }, config.to_h)
15
+ end
16
+
17
+ def test_a_child_block_can_be_opened
18
+ config.foo do |child|
19
+ child.bar = "baz"
20
+ end
21
+
22
+ assert_equal "baz", config.foo.bar
23
+ assert_equal({ "foo" => { "bar" => "baz" } }, config.to_h)
24
+ end
25
+
26
+ def test_a_child_block_can_be_merged_by_assigning_a_hash
27
+ config.foo = { "bar" => "baz" }
28
+ assert_equal "baz", config.foo.bar
29
+ assert_equal({ "foo" => { "bar" => "baz" } }, config.to_h)
30
+ end
31
+
32
+ def test_an_error_is_raised_for_a_missing_key
33
+ assert_raises KeyError do
34
+ config.foo
35
+ end
36
+ end
37
+
38
+ def test_hash_methods_are_accessible
39
+ config.foo = "foo_value"
40
+ config.bar = "bar_value"
41
+
42
+ assert_equal(%w[foo bar], config.keys)
43
+ assert_equal(%w[foo_value bar_value], config.values)
44
+
45
+ ikeys = []
46
+ config.each_key { |k| ikeys << k }
47
+ assert_equal(%w[foo bar], ikeys)
48
+
49
+ ivalues = []
50
+ config.each_value { |v| ivalues << v }
51
+ assert_equal(%w[foo_value bar_value], ivalues)
52
+ end
53
+
54
+ def test_a_yml_file_can_be_loaded_by_env
55
+ config.load_yaml("example.yml")
56
+ assert_equal({ "foo" => "foo", "env_setting" => "setting_test" }, config.to_h)
57
+ end
58
+
59
+ def test_a_yml_file_can_be_loaded_by_raw
60
+ config.load_yaml("example.yml", ignore_env: true)
61
+ assert_equal({
62
+ "shared" => { "foo" => "foo" },
63
+ "production" => { "env_setting" => "setting_prod", "foo" => "foo" },
64
+ "test" => { "env_setting" => "setting_test", "foo" => "foo" },
65
+ }, config.to_h)
66
+ end
67
+
68
+ end
@@ -0,0 +1,10 @@
1
+ shared: &shared
2
+ foo: "foo"
3
+
4
+ production:
5
+ <<: *shared
6
+ env_setting: "setting_prod"
7
+
8
+ test:
9
+ <<: *shared
10
+ env_setting: "setting_test"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+ require "confg"
5
+
6
+ require "minitest/autorun"
metadata CHANGED
@@ -1,29 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-14 00:00:00.000000000 Z
11
+ date: 2020-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: 3.0.0
20
- type: :runtime
33
+ version: '0'
34
+ type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: 3.0.0
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
27
69
  description: Config the pipes
28
70
  email:
29
71
  - mike@mnelson.io
@@ -40,6 +82,9 @@ files:
40
82
  - lib/confg/configuration.rb
41
83
  - lib/confg/erb_context.rb
42
84
  - lib/confg/version.rb
85
+ - test/confg_test.rb
86
+ - test/example.yml
87
+ - test/test_helper.rb
43
88
  homepage: ''
44
89
  licenses: []
45
90
  metadata: {}
@@ -54,13 +99,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
99
  version: '0'
55
100
  required_rubygems_version: !ruby/object:Gem::Requirement
56
101
  requirements:
57
- - - ">="
102
+ - - ">"
58
103
  - !ruby/object:Gem::Version
59
- version: '0'
104
+ version: 1.3.1
60
105
  requirements: []
61
- rubyforge_project:
62
- rubygems_version: 2.6.8
106
+ rubygems_version: 3.0.3
63
107
  signing_key:
64
108
  specification_version: 4
65
109
  summary: Sets shared variables for applications
66
- test_files: []
110
+ test_files:
111
+ - test/confg_test.rb
112
+ - test/example.yml
113
+ - test/test_helper.rb