confg 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45bd8085ff033db9882dce3a0844800ce4f1774e
4
+ data.tar.gz: 3b59f41cda4d6f1c783af1f16cb6e55347be3d16
5
+ SHA512:
6
+ metadata.gz: 47aa0cf019bf18aabb38dc0990e66b5d90c2e26f2b1004d8d1a30c0f261c5b7edfa2b242575df09c438d72e6adc08d855617e3d815fa077ad1c31b32a76b1349
7
+ data.tar.gz: 9f06da78306f2b2c65d023a27da90b09a43be63ee6b9f4049ab524523965d8fe21dec25468a267319510884302beb3b79d36f5f1f45c1e70eecc4d40b68a2710
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
16
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in config.gemspec
4
+ gemspec
@@ -0,0 +1,45 @@
1
+ # Conf
2
+
3
+ This allows the Conf namespace to provide configuration information
4
+
5
+ ## Usage
6
+
7
+ Conf.configure do |c|
8
+ c.app_key = 'core'
9
+ c.app_name = 'core'
10
+ end
11
+
12
+ Feel free to nest as well
13
+
14
+ Conf.configure do |c|
15
+ c.api_keys do |a|
16
+ a.google_places = 'xyz'
17
+ a.mixpanel = 'abc'
18
+ end
19
+ end
20
+
21
+ Wanna use yaml files? Cool:
22
+
23
+ Conf.configure do |c|
24
+ c.load_yaml 'file.yml'
25
+ c.load_yaml :core
26
+ c.load_yaml '/path/to/some.yml', :something
27
+ end
28
+
29
+ file.yml and core.yml above will be looked for in the Conf.config_dir (which can also be set but defualts to root/config)
30
+ Yaml files can be namespaced by environment as well. Oh, and they can have ERB:
31
+
32
+ ---
33
+ development:
34
+ thing: <%= ENV["THING"] %>
35
+ staging:
36
+ thing: 'set value'
37
+
38
+ Ok, using the values:
39
+
40
+ Conf.app_key
41
+ # => 'core'
42
+ Conf.api_keys
43
+ # => #<Config::Configuration:0x007f9655e5ba58 @attributes={"google_places"=>"xyz", "mixpanel"=>"abc"} >
44
+ Conf.api_keys.google_places
45
+ # => 'xyz'
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'confg/version'
5
+
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 = ""
14
+
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"]
19
+
20
+ gem.add_dependency 'activesupport', '>= 3.0.0'
21
+ end
@@ -0,0 +1,51 @@
1
+ require 'confg/version'
2
+ require 'confg/configuration'
3
+
4
+ module Confg
5
+
6
+ class << self
7
+
8
+ def root
9
+ @root ||= Pathname.new(calc_root_string).expand_path
10
+ end
11
+
12
+ def configure(raise_on_missed_key = false)
13
+ @configuration ||= ::Confg::Configuration.new(raise_on_missed_key)
14
+ yield @configuration if block_given?
15
+ @configuration
16
+ end
17
+ alias_method :config, :configure
18
+
19
+ def method_missing(method_name, *args, &block)
20
+ config.send(method_name, *args, &block)
21
+ end
22
+
23
+ def respond_to_missing?(*args)
24
+ true
25
+ end
26
+
27
+ def get(path)
28
+ thing = self
29
+ path.split('.').each do |piece|
30
+ thing = thing.try(piece)
31
+ end
32
+ thing
33
+ end
34
+
35
+ protected
36
+
37
+ def calc_root_string
38
+ return Rails.root.to_s if defined?(Rails)
39
+ return RAILS_ROOT if defined?(RAILS_ROOT)
40
+ return RACK_ROOT if defined?(RACK_ROOT)
41
+
42
+ ENV['RAILS_ROOT'] || ENV['RACK_ROOT'] || Dir.pwd
43
+ end
44
+
45
+ def reset!
46
+ remove_instance_variable("@configuration") if defined?(@configuration)
47
+ remove_instance_variable("@root") if defined?(@root)
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,159 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ require 'active_support/core_ext/module/delegation'
4
+
5
+ module Confg
6
+ class Configuration
7
+
8
+ delegate :each, :inspect, :to => :@attributes
9
+
10
+ def initialize(raise_error_on_miss = false, parent = nil)
11
+ @attributes = {}
12
+ @raise_error_on_miss = raise_error_on_miss
13
+ @parent = parent
14
+ end
15
+
16
+ def merge(hash)
17
+ hash.each do |k,v|
18
+ self.set(k,v)
19
+ end
20
+ end
21
+ alias_method :merge!, :merge
22
+
23
+ def [](key)
24
+ self.get(key)
25
+ end
26
+
27
+ def []=(key, value)
28
+ self.set(key, value)
29
+ end
30
+
31
+ def tmp(key, value)
32
+ initial = self[key]
33
+ self[key] = value
34
+ yield
35
+ ensure
36
+ self[key] = initial
37
+ end
38
+
39
+ def load_key(key)
40
+ # loads yaml file with given key
41
+ load_yaml(key, key)
42
+ end
43
+
44
+ def load_yaml(path, key = nil)
45
+ path = find_config_yaml(path)
46
+ raw_content = File.open(path, 'r'){|io| io.read } rescue nil
47
+
48
+ return unless raw_content
49
+
50
+ raw_content = ERB.new(raw_content).result
51
+ content = YAML.load(raw_content)
52
+ content = content[Rails.env] if content.is_a?(::Hash) && content.has_key?(Rails.env)
53
+
54
+ if key
55
+ self.set(key, content)
56
+ else
57
+ if content.is_a?(Array)
58
+ raise "A key must be provided to load the file at: #{path}"
59
+ else
60
+ content.each do |k,v|
61
+ self.set(k, v)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ alias_method :load_yml, :load_yaml
67
+
68
+ def method_missing(method_name, *args, &block)
69
+ if method_name.to_s =~ /^(.+)=$/ && !args.empty?
70
+ self.set($1, args.first)
71
+ elsif method_name.to_s =~ /^([^=]+)$/
72
+ if block_given?
73
+ self.set_block($1, &block)
74
+ elsif @attributes.respond_to?($1)
75
+ @attributes.send($1, *args)
76
+ else
77
+ self.get($1)
78
+ end
79
+ else
80
+ super
81
+ end
82
+ end
83
+
84
+ def respond_to?(method_name, include_private = false)
85
+ true
86
+ end
87
+
88
+ protected
89
+
90
+ def set(key, value = nil)
91
+ case value
92
+ when ::Hash
93
+ set_block key do |inner|
94
+ value.each do |k,v|
95
+ inner.set(k, v)
96
+ end
97
+ end
98
+ else
99
+ @attributes[key.to_s] = value
100
+ end
101
+ end
102
+
103
+ def get(key)
104
+ if @attributes.has_key?(key.to_s)
105
+ @attributes[key.to_s]
106
+ else
107
+ get_missing_key(key.to_s)
108
+ end
109
+ end
110
+
111
+ def get_missing_key(key)
112
+ if @raise_error_on_miss
113
+ raise "Missing key: #{key} in #{@attributes.inspect}"
114
+ else
115
+ nil
116
+ end
117
+ end
118
+
119
+ def set_block(key, &block)
120
+ inner = @attributes[key.to_s] || child_new
121
+ block.call(inner)
122
+ set(key, inner)
123
+ end
124
+
125
+ def find_config_yaml(path)
126
+ path = path.to_s
127
+ # give it back if it starts with a slash
128
+ return path if path =~ /^\//
129
+
130
+ to_try = []
131
+ unless path =~ /.yml$/
132
+ to_try << Confg.root.join("config/#{path}.yml")
133
+ end
134
+ to_try << Confg.root.join("config/#{path}")
135
+ to_try << Confg.root.join(path)
136
+
137
+ to_try.each do |file|
138
+ return file if File.file?(file)
139
+ end
140
+
141
+ to_try.first
142
+ end
143
+
144
+ def child_class
145
+ # same as ours
146
+ self.class
147
+ end
148
+
149
+ def child_raise_error_on_miss
150
+ # same as ours
151
+ @raise_error_on_miss
152
+ end
153
+
154
+ def child_new
155
+ child_class.new(child_raise_error_on_miss, self)
156
+ end
157
+
158
+ end
159
+ end
@@ -0,0 +1,3 @@
1
+ module Confg
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confg
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Nelson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Config the pipes
28
+ email:
29
+ - mike@mnelson.io
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - confg.gemspec
39
+ - lib/confg.rb
40
+ - lib/confg/configuration.rb
41
+ - lib/confg/version.rb
42
+ homepage: ''
43
+ licenses: []
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.6.8
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Sets shared variables for applications
65
+ test_files: []