trusty 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/trusty/environment/dot_file_parser.rb +19 -0
- data/lib/trusty/environment.rb +99 -0
- data/lib/trusty/utilities/method_name.rb +84 -0
- data/lib/trusty/utilities/method_name_extensions.rb +26 -0
- data/lib/trusty/utilities/path_finder.rb +17 -0
- data/lib/trusty/utilities/yaml.rb +19 -0
- data/lib/trusty/utilities.rb +10 -0
- data/lib/trusty/version.rb +3 -0
- data/lib/trusty.rb +9 -0
- data/trusty.gemspec +25 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 396691c71380d978d8119d8bcb3f0d32491f1888
|
4
|
+
data.tar.gz: 2b29a3e6491960ed1f96630e7ca213305dbda6f4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb6b586a37a1060600fbeca34fa3fea9826f020cae310be08cda2f77b4e3da74554728cf3c9ade77e2586b24c26e4a934c9124eaf4d6528452f7e545773843ef
|
7
|
+
data.tar.gz: d39b0d14db882904031780ae22a5e2f02284a620c9003090b8b1dd0d8c7397e895ec469ea28ab655582cf25659a5a8bffa677c32ab1992f4c3f11053b47cc0de
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Joel Van Horn
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Trusty
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'trusty'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install trusty
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'dotenv'
|
2
|
+
|
3
|
+
module Trusty
|
4
|
+
module Environment
|
5
|
+
class DotFileParser < Dotenv::Environment
|
6
|
+
|
7
|
+
def initialize(source)
|
8
|
+
@source = source
|
9
|
+
end
|
10
|
+
|
11
|
+
def read
|
12
|
+
@source.split("\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :parse, :load
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require_relative "utilities"
|
2
|
+
require_relative "environment/dot_file_parser"
|
3
|
+
|
4
|
+
module Trusty
|
5
|
+
module Environment
|
6
|
+
module ClassMethods
|
7
|
+
include Utilities::MethodNameExtensions
|
8
|
+
|
9
|
+
# load env.yml into ENV (e.g. ENV['DATABASE_URL'])
|
10
|
+
def load_env!(options = {})
|
11
|
+
source = load_yaml_file("env.yml")
|
12
|
+
source = source.fetch(options[:env_section] || env_section, {})
|
13
|
+
|
14
|
+
source.each do |key, value|
|
15
|
+
if !ENV.has_key?(key) || options[:overwrite] == true
|
16
|
+
ENV[key.to_s.upcase] = value.to_s
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# load env.yml into constants (e.g. Vars::DATABASE_URL)
|
22
|
+
def load_constants!(options = {})
|
23
|
+
source = load_yaml_file("env.yml")
|
24
|
+
source = source.fetch(options[:env_section] || env_section, {})
|
25
|
+
|
26
|
+
source.each do |key, value|
|
27
|
+
constant_name = key.to_s.upcase.to_sym
|
28
|
+
|
29
|
+
if !constants.include(constant_name) || options[:overwrite] == true
|
30
|
+
constant_set constant_name, value.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def [](key)
|
36
|
+
env[key.to_s.downcase]
|
37
|
+
end
|
38
|
+
|
39
|
+
# downcases keys to method names
|
40
|
+
def env(env_section = default_env_section)
|
41
|
+
@env ||= {}
|
42
|
+
@env[env_section] ||= methodize_hash env_source(env_section).merge(ENV.to_hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
def env_source(env_section = default_env_section)
|
46
|
+
@env_source ||= {}
|
47
|
+
@env_source[env_section] ||= load_yaml_file("env.yml").fetch(env_section, {})
|
48
|
+
end
|
49
|
+
|
50
|
+
def default_env_section
|
51
|
+
@default_env_section ||= ENV['ENV_SECTION'] || Rails.env
|
52
|
+
end
|
53
|
+
|
54
|
+
def config(default_value = nil)
|
55
|
+
# loads YAML on-the-fly when a key doesn't exist
|
56
|
+
@config ||= Hashie::Mash.new do |hash, key|
|
57
|
+
hash[key] = methodize_hash load_yaml_file("#{key}.yml", default_value)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def paths
|
62
|
+
@paths ||= [ Rails.root.join("config").to_s ]
|
63
|
+
end
|
64
|
+
|
65
|
+
# dynamically add methods that forward to config
|
66
|
+
def method_missing(name, *args, &block)
|
67
|
+
if !method_name_info(name).special?
|
68
|
+
instance_variable_name = :"@#{name}"
|
69
|
+
instance_variable_value = config[name]
|
70
|
+
instance_variable_set instance_variable_name, instance_variable_value
|
71
|
+
|
72
|
+
define_singleton_method name do
|
73
|
+
instance_variable_get instance_variable_name
|
74
|
+
end
|
75
|
+
|
76
|
+
instance_variable_value
|
77
|
+
else
|
78
|
+
super
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def load_yaml_file(filename, default_value = {})
|
85
|
+
Utilities::Yaml.load_file(filename, paths) || default_value
|
86
|
+
end
|
87
|
+
|
88
|
+
def methodize_hash(hash)
|
89
|
+
if hash != nil
|
90
|
+
Hashie::Mash.new hash.inject({}){|result, (key, value)| result.merge(key.underscore.downcase => value)}
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
# create class methods
|
97
|
+
extend ClassMethods
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Trusty
|
2
|
+
module Utilities
|
3
|
+
class MethodName
|
4
|
+
|
5
|
+
attr_reader :name, :base, :convention
|
6
|
+
|
7
|
+
def initialize(name)
|
8
|
+
@name = name
|
9
|
+
# returns a method name and its ending (e.g. "config?" returns ["config", "?"])
|
10
|
+
@base, @convention = name.to_s.match(/\A(\w+?)(\W+)?\Z/).to_a.slice(1, 2)
|
11
|
+
end
|
12
|
+
|
13
|
+
# indicates that it's not a vanilla method name
|
14
|
+
def special?
|
15
|
+
base == nil || convention != nil
|
16
|
+
end
|
17
|
+
|
18
|
+
# has a name and is not shorthand (e.g. "[]", "[]=", or "<<" type method)
|
19
|
+
def named?
|
20
|
+
base != nil
|
21
|
+
end
|
22
|
+
|
23
|
+
# has no name and is probably just "[]", "[]=", or "<<" type method
|
24
|
+
def shorthand?
|
25
|
+
!named?
|
26
|
+
end
|
27
|
+
|
28
|
+
def boolean?
|
29
|
+
convention == '?'
|
30
|
+
end
|
31
|
+
|
32
|
+
def modifier?
|
33
|
+
convention == '!'
|
34
|
+
end
|
35
|
+
|
36
|
+
def value_for(target, *args, &block)
|
37
|
+
if target.respond_to?(name) || !target.respond_to?(base)
|
38
|
+
method_value_for(target, name, *args, &block)
|
39
|
+
else
|
40
|
+
base_value_for(target, *args, &block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def base_value_for(target, *args, &block)
|
45
|
+
method_value_for(target, base, *args, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_for(target, options = { :on => :all })
|
49
|
+
class_result = define_with_method :define_method, target unless options[:on] == :instance
|
50
|
+
instance_result = define_with_method :define_singleton_method, target unless options[:on] == :class
|
51
|
+
|
52
|
+
# indicate if a method was defined
|
53
|
+
class_result == true || instance_result == true
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def define_with_method(define_method_name, target)
|
59
|
+
# create helper method that sees if a config is blank
|
60
|
+
if boolean? && target.respond_to?(define_method_name)
|
61
|
+
helper = self
|
62
|
+
target.send define_method_name, name do |*args, &block|
|
63
|
+
helper.base_value_for(self, *args, &block)
|
64
|
+
end
|
65
|
+
|
66
|
+
true
|
67
|
+
else
|
68
|
+
false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def method_value_for(target, method_name, *args, &block)
|
73
|
+
value = target.send(method_name, *args, &block)
|
74
|
+
|
75
|
+
if boolean?
|
76
|
+
value != nil && (!value.respond_to?(:empty?) || !value.empty?)
|
77
|
+
else
|
78
|
+
value
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Trusty
|
2
|
+
module Utilities
|
3
|
+
module MethodNameExtensions
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend self
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_name_info(method_name)
|
10
|
+
@method_name_info ||= {}
|
11
|
+
@method_name_info[method_name.to_s] ||= MethodName.new(method_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
# dynamically add methods that forward to config
|
15
|
+
def method_missing(name, *args, &block)
|
16
|
+
method_name = method_name_info(name)
|
17
|
+
|
18
|
+
if method_name.define_for(self)
|
19
|
+
method_name.value_for(self)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Trusty
|
2
|
+
module Utilities
|
3
|
+
module PathFinder
|
4
|
+
module ClassMethods
|
5
|
+
def find(filename, paths = [])
|
6
|
+
if File.exists? filename
|
7
|
+
filename
|
8
|
+
elsif root = paths.find{|path| File.exists?(File.join(path, filename))}
|
9
|
+
File.join(root, filename)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
extend ClassMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Trusty
|
2
|
+
module Utilities
|
3
|
+
module Yaml
|
4
|
+
module ClassMethods
|
5
|
+
def load_file(filename, paths = [])
|
6
|
+
# allow multiple filenames and use the first one that exists
|
7
|
+
if path = PathFinder.find(filename, paths)
|
8
|
+
source = File.read(path)
|
9
|
+
source = ERB.new(source).result
|
10
|
+
|
11
|
+
YAML.load(source)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
extend ClassMethods
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/trusty.rb
ADDED
data/trusty.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'trusty/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "trusty"
|
8
|
+
spec.version = Trusty::VERSION
|
9
|
+
spec.authors = ["Joel Van Horn"]
|
10
|
+
spec.email = ["joel@joelvanhorn.com"]
|
11
|
+
spec.description = %q{Trusty is a configuration and utilities library.}
|
12
|
+
spec.summary = %q{Trusty allows you to manage environment variables and other common configuration challenges.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "dotenv", ">= 0.9.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trusty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Van Horn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-20 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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: dotenv
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.9.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.9.0
|
55
|
+
description: Trusty is a configuration and utilities library.
|
56
|
+
email:
|
57
|
+
- joel@joelvanhorn.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/trusty.rb
|
68
|
+
- lib/trusty/environment.rb
|
69
|
+
- lib/trusty/environment/dot_file_parser.rb
|
70
|
+
- lib/trusty/utilities.rb
|
71
|
+
- lib/trusty/utilities/method_name.rb
|
72
|
+
- lib/trusty/utilities/method_name_extensions.rb
|
73
|
+
- lib/trusty/utilities/path_finder.rb
|
74
|
+
- lib/trusty/utilities/yaml.rb
|
75
|
+
- lib/trusty/version.rb
|
76
|
+
- trusty.gemspec
|
77
|
+
homepage: ''
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.1.5
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Trusty allows you to manage environment variables and other common configuration
|
101
|
+
challenges.
|
102
|
+
test_files: []
|