savvy 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95186ba3a6494096fcac558c89fc868d8f790bde
4
+ data.tar.gz: 598b24403bac739d12b0d3c0b5b3ef9db3e275e4
5
+ SHA512:
6
+ metadata.gz: 4093c079265cfa65d1bdd34ed9111c86d360727a4a58d5664bf943ba0013bac696593a65d7dee8f9825f2d9fbe6719c67be920b9898d335bca9cf9d775c097fe
7
+ data.tar.gz: 0aed5f4081e6f689ea71471bbc85d1758e91cc06f7958a9f498e2af9e0cf758c6b929fa858a54c7666150337cd767d9c0810419e9245b9ee8683d393c2ee25f8
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ dist: trusty
3
+ cache: bundler
4
+ language: ruby
5
+ rvm:
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4.0
9
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at devel@mouse.vc. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in savvy.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alexa Grey
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,98 @@
1
+ # Savvy
2
+
3
+ [![Build Status](https://travis-ci.org/scryptmouse/savvy.svg?branch=master)](https://travis-ci.org/scryptmouse/savvy)
4
+
5
+ `ENV`-backed configuration helper for Redis, sidekiq, etc.
6
+
7
+ Designed to manage configuration of services across Rails apps without
8
+ repeating a lot of the same library code.
9
+
10
+ Still in very alpha.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'savvy'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install savvy
27
+
28
+ ## Setup
29
+
30
+ Add the following to application.rb, after the call to `Bundler.require` and before `module RailsApp`:
31
+
32
+ ```ruby
33
+ Bundler.require(*Rails.groups)
34
+
35
+ Savvy.initialize!
36
+
37
+ module RailsApp
38
+ class Application < Rails::Application
39
+
40
+ end
41
+ end
42
+ ```
43
+
44
+ For non-rails usage, add `Savvy.initialize!` after you require your dependencies / set up Bundler,
45
+ and before you initialize the rest of your app.
46
+
47
+ Once it is in place, run `bundle exec savvy init` to create a `Savvyfile` that can configure your application.
48
+
49
+ ## Usage
50
+
51
+ You can build an app specific namespace for general usage (e.g. memcached):
52
+
53
+ ```ruby
54
+ Savvy.namespace :cache, :http, separator: '/' # => "yourapp/cache/http"
55
+ ```
56
+
57
+ Redis settings are opinionated and simplified:
58
+
59
+ ```ruby
60
+ # Will auto-detect based on REDISTOGO_URL, REDISCLOUD_URL, BOXEN_REDIS_URL, REDIS_URL,
61
+ # or fall back to default 'redis://localhost:6379'
62
+ Savvy.redis.url
63
+
64
+ # Uses : as a separator by default, but can be overridden
65
+ Savvy.redis.namespace(:cache) # => "yourapp:cache"
66
+
67
+ # Build a ConnectionPool of namespaced Redis connections.
68
+ Savvy.redis.build_connection_pool(:objects, size: 5, timeout: 5)
69
+
70
+ # Build a plain Redis::Namespace connection
71
+ Savvy.build_connection :cache
72
+ ```
73
+
74
+ Sidekiq has a helper that builds a namespaced redis connection to keep sidekiq isolated:
75
+
76
+ ```ruby
77
+ Sidekiq.configure_client do |config|
78
+ config.redis = Savvy.sidekiq.redis_options
79
+ end
80
+
81
+ Sidekiq.configure_server do |config|
82
+ config.redis = Savvy.sidekiq.redis_options
83
+ end
84
+ ```
85
+
86
+ ## Development
87
+
88
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
89
+
90
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at https://github.com/scryptmouse/savvy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new('spec') do |t|
5
+ t.verbose = false
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "savvy"
5
+ require "pry"
6
+
7
+ Pry.config.prompt_name = 'savvy'
8
+
9
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "savvy"
4
+ require "savvy/application"
5
+
6
+ Savvy::Application.new.run
@@ -0,0 +1,57 @@
1
+ require 'pathname'
2
+ require 'uri'
3
+
4
+ require 'cleanroom'
5
+ require 'dux'
6
+
7
+ require 'savvy/version'
8
+
9
+ require 'savvy/configuration'
10
+ require 'savvy/configuration_dsl'
11
+ require 'savvy/environment_reader'
12
+ require 'savvy/root_finder'
13
+ require 'savvy/utility'
14
+
15
+ require 'savvy/configurators/all'
16
+
17
+ module Savvy
18
+ class << self
19
+ # @!attribute [r] config
20
+ # @return [Savvy::Configuration]
21
+ def config
22
+ @@config
23
+ end
24
+
25
+ # @!attribute [r] env
26
+ # @return [Savvy::EnvironmentReader]
27
+ def env
28
+ @@env
29
+ end
30
+
31
+ # @return [void]
32
+ def initialize!
33
+ @@config.setup!
34
+ end
35
+
36
+ # @param [<String>] parts
37
+ # @return [String]
38
+ def namespace(*parts, separator: ?.)
39
+ @@config.build_namespace(*parts, separator: separator)
40
+ end
41
+
42
+ # @return [Pathname]
43
+ def root
44
+ @@config.root
45
+ end
46
+
47
+ private
48
+
49
+ # @return [Pathname]
50
+ def figure_out_root!
51
+ Savvy::RootFinder.call Dir.pwd
52
+ end
53
+ end
54
+
55
+ @@env = Savvy::EnvironmentReader.new
56
+ @@config = Savvy::Configuration.new root: figure_out_root!, env: env
57
+ end
@@ -0,0 +1,60 @@
1
+ require 'commander'
2
+ require 'erb'
3
+
4
+ module Savvy
5
+ class Application
6
+ include Commander::Methods
7
+
8
+ SAVVYFILE_TEMPLATE = File.join(__dir__, 'templates', 'Savvyfile.rb.erb')
9
+
10
+ attr_reader :config
11
+ attr_reader :savvyfile
12
+
13
+ def initialize
14
+ @config = Savvy.config
15
+ @savvyfile = Savvy.config.root.join('Savvyfile')
16
+
17
+ @config.setup!
18
+ end
19
+
20
+ def run
21
+ program :name, 'savvy'
22
+ program :version, Savvy::VERSION
23
+ program :description, 'Savvy file generator'
24
+
25
+ command :init do |c|
26
+ c.syntax = 'savvy init'
27
+ c.description = 'Initialize a Savvyfile'
28
+ c.action do
29
+ if savvyfile.exist?
30
+ puts "! #{savvyfile} exists"
31
+
32
+ unless agree("Do you want to overwrite? ")
33
+ puts "Not overwriting"
34
+
35
+ exit
36
+ end
37
+ end
38
+
39
+ contents = savvyfile_contents
40
+
41
+ puts "Writing to #{savvyfile}"
42
+
43
+ savvyfile.open('w+') do |f|
44
+ f.write contents
45
+ end
46
+ end
47
+ end
48
+
49
+ run!
50
+ end
51
+
52
+ def savvyfile_contents
53
+ app_name = config.app_name = ask 'What is the name of your app? ' do |q|
54
+ q.default = config.app_name
55
+ end
56
+
57
+ ERB.new(File.read(SAVVYFILE_TEMPLATE)).result(binding)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,143 @@
1
+ module Savvy
2
+ # Configuration files that can be contained
3
+ # in an application root directory.
4
+ FILES = %w[
5
+ savvy.rb
6
+ Savvyfile
7
+ ].freeze
8
+
9
+ # Shared configuration object for a Savvy-enabled application
10
+ class Configuration
11
+ APP_ENV_VARS = %w[
12
+ RAILS_ENV
13
+ RACK_ENV
14
+ ].freeze
15
+
16
+ DEFAULT_REDIS_ENV_VARS = %w[
17
+ REDISTOGO_URL
18
+ REDISCLOUD_URL
19
+ BOXEN_REDIS_URL
20
+ REDIS_URL
21
+ ].freeze
22
+
23
+ DEFAULT_REDIS_URL = 'redis://localhost:6379'
24
+
25
+ attr_reader :env
26
+ attr_reader :root
27
+
28
+ def initialize(root:, env: Savvy.env)
29
+ @root = root
30
+ @env = env
31
+
32
+ @config_file = find_config_file
33
+
34
+ @app_name = root.basename.to_s
35
+ @include_app_env = false
36
+ @redis_env_vars = DEFAULT_REDIS_ENV_VARS.dup
37
+ @redis_default_url = DEFAULT_REDIS_URL
38
+ end
39
+
40
+ def setup!
41
+ @app_env = @env[*APP_ENV_VARS]
42
+
43
+ load_from_file!
44
+ end
45
+
46
+ # @return [String]
47
+ def build_namespace(*parts, separator: ?.)
48
+ [
49
+ app_name,
50
+ ( app_env if include_app_env? ),
51
+ *parts
52
+ ].compact.join(separator)
53
+ end
54
+
55
+ def configure
56
+ yield to_dsl if block_given?
57
+
58
+ return self
59
+ end
60
+
61
+ # @see [Savvy::EnvironmentReader#[]]
62
+ # @return [String]
63
+ def read_from_env(*vars, **options)
64
+ @env[*vars, **options]
65
+ end
66
+
67
+ attr_accessor :app_env
68
+
69
+ # @overload app_name
70
+ # Get the current app name
71
+ # @return [String]
72
+ # @overload app_name=(new_app_name)
73
+ # @param [String, Symbol] new_app_name
74
+ # @raise [TypeError] if provided with an invalid app name
75
+ attr_reader :app_name
76
+
77
+ def app_name=(new_app_name)
78
+ raise TypeError, "Must provide a string or symbol" unless new_app_name.kind_of?(String) || new_app_name.kind_of?(Symbol)
79
+ raise TypeError, "Must provide a valid app name" unless Dux.presentish?(new_app_name)
80
+
81
+ @app_name = new_app_name
82
+ end
83
+
84
+ # @!attribute [rw] include_app_env
85
+ # Whether the Rails/Rack env should be included when
86
+ # building namespaces.
87
+ # @return [Boolean]
88
+ attr_reader :include_app_env
89
+
90
+ alias_method :include_app_env?, :include_app_env
91
+
92
+ def include_app_env=(new_value)
93
+ @include_app_env = !!new_value
94
+ end
95
+
96
+ # @!attribute [rw] redis_default_url
97
+ # @return [String]
98
+ attr_reader :redis_default_url
99
+
100
+ def redis_default_url=(new_url)
101
+ raise TypeError, "Must be a redis:// url: `#{new_url.inspect}`" unless Savvy::Utility.valid_url?(new_url, scheme: 'redis')
102
+
103
+ @redis_default_url = new_url
104
+ end
105
+
106
+ # @overload redis_env_vars
107
+ # Get the current redis env keys
108
+ # @return [<String>]
109
+ # @overload redis_env_vars=(new_env_keys)
110
+ # @param [<String>] new_env_keys
111
+ # @raise [TypeError] if not an array of strings.
112
+ attr_reader :redis_env_vars
113
+
114
+ def redis_env_vars=(new_env_keys)
115
+ raise TypeError, "Must be an array of strings" unless Savvy::Utility.valid_env_vars?(new_env_keys)
116
+
117
+ @redis_env_vars = new_env_keys
118
+ end
119
+
120
+ private
121
+
122
+ # @return [Pathname]
123
+ def find_config_file
124
+ Savvy::FILES.each do |file|
125
+ config_path = @root.join(file)
126
+
127
+ return config_path if config_path.exist?
128
+ end
129
+
130
+ return nil
131
+ end
132
+
133
+ # @return [void]
134
+ def load_from_file!
135
+ to_dsl.evaluate_file(@config_file) if @config_file
136
+ end
137
+
138
+ # @return [Savvy::ConfigurationDSL]
139
+ def to_dsl
140
+ ConfigurationDSL.new self
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,38 @@
1
+ module Savvy
2
+ # Configuration DSL for a {Savvy::Configuration}.
3
+ #
4
+ # Keeps the methods separate / cleaner.
5
+ #
6
+ # @api private
7
+ class ConfigurationDSL
8
+ include Cleanroom
9
+
10
+ def initialize(config)
11
+ @config = config
12
+ end
13
+
14
+ def app_name(value)
15
+ @config.app_name = value
16
+ end
17
+
18
+ expose :app_name
19
+
20
+ def include_app_env_in_namespaces!
21
+ @config.include_app_env = true
22
+ end
23
+
24
+ expose :include_app_env_in_namespaces!
25
+
26
+ def redis_default_url(url)
27
+ @config.redis_default_url = url
28
+ end
29
+
30
+ expose :redis_default_url
31
+
32
+ def redis_env_vars(*vars)
33
+ @config.redis_env_vars = vars.flatten
34
+ end
35
+
36
+ expose :redis_env_vars
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ require 'savvy/configurators/redis'
2
+ require 'savvy/configurators/sidekiq'
@@ -0,0 +1,54 @@
1
+ module Savvy
2
+ module Configurators
3
+ class Redis
4
+ DEFAULT_SEPARATOR = ?:
5
+
6
+ # @!attribute [r] url
7
+ # @return [String]
8
+ attr_reader :url
9
+
10
+ # @param [Savvy::Config] config
11
+ def initialize(config: Savvy.config)
12
+ @config = config
13
+ @url = read_url_from_config
14
+ end
15
+
16
+ def namespace(*parts, separator: DEFAULT_SEPARATOR)
17
+ @config.build_namespace(*parts, separator: separator)
18
+ end
19
+
20
+ def namespaced_url(*parts)
21
+ "#{url}/#{namespace(*parts)}"
22
+ end
23
+
24
+ # @return [ConnectionPool]
25
+ def build_connection_pool(*namespace_parts, size: 5, timeout: 5)
26
+ raise 'Requires connection_pool gem' unless defined?(ConnectionPool)
27
+
28
+ ::ConnectionPool.new size: size, timeout: timeout do
29
+ build_connection(*namespace_parts)
30
+ end
31
+ end
32
+
33
+ def build_connection(*namespace_parts)
34
+ raise 'Requires redis-namespace gem' unless defined?(::Redis::Namespace)
35
+
36
+ ::Redis::Namespace.new(namespace(*namespace_parts), redis: ::Redis.new(url: url))
37
+ end
38
+
39
+ private
40
+
41
+ def read_url_from_config
42
+ @config.read_from_env *@config.redis_env_vars, fallback: @config.redis_default_url
43
+ end
44
+ end
45
+ end
46
+
47
+ class << self
48
+ # @!attribute [r] redis
49
+ # @return [Savvy::Configurators::Redis]
50
+ def redis
51
+ @redis ||= Savvy::Configurators::Redis.new
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,28 @@
1
+ require 'savvy/configurators/redis'
2
+
3
+ module Savvy
4
+ module Configurators
5
+ class Sidekiq
6
+ def initialize(config: Savvy.config, redis: Savvy.redis)
7
+ @config = config
8
+ @redis = redis
9
+ end
10
+
11
+ # @return [Hash]
12
+ def redis_options
13
+ {
14
+ url: @redis.url,
15
+ namespace: @redis.namespace('sidekiq')
16
+ }
17
+ end
18
+ end
19
+ end
20
+
21
+ class << self
22
+ # @!attribute [r] sidekiq
23
+ # @return [Savvy::Configurators::Sidekiq]
24
+ def sidekiq
25
+ @sidekiq ||= Savvy::Configurators::Sidekiq.new
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ module Savvy
2
+ class EnvironmentReader
3
+ NONE = Dux.null "Savvy::EnvironmentReader::NONE", purpose: 'When there is no fallback'
4
+
5
+ private_constant :NONE
6
+
7
+ # @param [#[]] env
8
+ def initialize(env = ENV)
9
+ @env = env
10
+ end
11
+
12
+ # Like {#fetch}, but with `raise_error` set to false and `presence` set to true.
13
+ #
14
+ # @param [<String>] names
15
+ # @param [Boolean] presence
16
+ def [](*names, presence: true, raise_error: false, **options)
17
+ options[:raise_error] = raise_error
18
+ options[:presence] = presence
19
+
20
+ fetch(*names, **options)
21
+ end
22
+
23
+ # @param [<String>] names
24
+ # @param [Object] fallback
25
+ # @param []
26
+ def fetch(*names, fallback: NONE, raise_error: true, presence: false)
27
+ names.flatten!
28
+
29
+ names.each do |name|
30
+ if ( presence && has_value?(name) ) || set?(name)
31
+ return @env[name]
32
+ end
33
+ end
34
+
35
+ if fallback != NONE
36
+ return fallback
37
+ elsif raise_error
38
+ raise ArgumentError, "No #{'present ' if presence}env value found for #{names.join(', ')}"
39
+ end
40
+ end
41
+
42
+ # Check if the environment has a present
43
+ # value set.
44
+ #
45
+ # @param [String] var
46
+ def has_value?(var)
47
+ set?(var) && Dux.presentish?(@env[var])
48
+ end
49
+
50
+ # Checks only if the variable is set,
51
+ # not necessarily that it is non-empty.
52
+ #
53
+ # @param [String] var
54
+ def set?(var)
55
+ @env.key?(var)
56
+ end
57
+
58
+ private
59
+ end
60
+ end
@@ -0,0 +1,44 @@
1
+ module Savvy
2
+ module RootFinder
3
+ module_function
4
+
5
+ # Something that, given a directory possessing such a file,
6
+ # would make it look like a potential root directory.
7
+ ROOT_FILES = [
8
+ *Savvy::FILES,
9
+ 'Gemfile'
10
+ ].freeze
11
+
12
+ def find_potential_root_from(origin = Dir.pwd)
13
+ start_root = Pathname.new(origin)
14
+
15
+ start_root.ascend do |path|
16
+ if looks_like_a_root_directory?(path)
17
+ return path
18
+ end
19
+ end
20
+
21
+ raise ArgumentError, "Could not find root from #{origin}"
22
+ end
23
+
24
+ def looks_like_a_root_directory?(path)
25
+ ROOT_FILES.any? do |filename|
26
+ path.join(filename).exist?
27
+ end
28
+ end
29
+
30
+ class << self
31
+ # @param [String, Pathname] origin
32
+ # @return [Pathname]
33
+ def call(origin = Dir.pwd)
34
+ if defined?(Rails) && Rails.root
35
+ Rails.root
36
+ elsif defined?(Bundler) && Bundler.root
37
+ Bundler.root
38
+ else
39
+ find_potential_root_from(origin)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Configure Savvy properties for this application.
4
+
5
+ app_name '<%= app_name %>'
6
+
7
+ # Include RAILS_ENV/RACK_ENV in namespaces
8
+ # include_app_env_in_namespaces!
9
+
10
+ # Override default redis url
11
+ # redis_default_url 'redis://localhost:6379'
@@ -0,0 +1,36 @@
1
+ module Savvy
2
+ module Utility
3
+ module_function
4
+
5
+ # Validate that the provided value is a one-dimensional
6
+ # array of strings that may or may not exist in `ENV`.
7
+ #
8
+ # @param [<String>] vars
9
+ def valid_env_vars?(vars)
10
+ return false unless vars.kind_of?(Array)
11
+
12
+ vars.all? do |var|
13
+ valid_env_var? var
14
+ end
15
+ end
16
+
17
+ # Validate that the provided var is a string
18
+ # that can be used as a name for environment
19
+ # variables.
20
+ #
21
+ # @param [String] var
22
+ def valid_env_var?(var)
23
+ var.kind_of?(String) && Dux.presentish?(var)
24
+ end
25
+
26
+ def valid_url?(url, scheme: nil)
27
+ return false unless url.kind_of?(String) && Dux.presentish?(url)
28
+
29
+ if scheme
30
+ return false unless url.start_with?("#{scheme}://")
31
+ end
32
+
33
+ return true
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Savvy
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'savvy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "savvy"
8
+ spec.version = Savvy::VERSION
9
+ spec.authors = ["Alexa Grey"]
10
+ spec.email = ["devel@mouse.vc"]
11
+
12
+ spec.summary = %q{ENV-backed configuration helper for Redis, Sidekiq, etc}
13
+ spec.description = %q{ENV-backed configuration helper for Redis, Sidekiq, etc}
14
+ spec.homepage = "https://github.com/scryptmouse/savvy"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "cleanroom", "~> 1.0"
25
+ spec.add_dependency "commander"
26
+ spec.add_dependency "dux", ">= 0.8.0", "< 2.0"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.14"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "simplecov"
32
+ spec.add_development_dependency "stub_env"
33
+ spec.add_development_dependency "pry"
34
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: savvy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexa Grey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cleanroom
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: commander
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: dux
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.0
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '2.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 0.8.0
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.14'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.14'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: simplecov
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: stub_env
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: pry
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ description: ENV-backed configuration helper for Redis, Sidekiq, etc
146
+ email:
147
+ - devel@mouse.vc
148
+ executables:
149
+ - savvy
150
+ extensions: []
151
+ extra_rdoc_files: []
152
+ files:
153
+ - ".gitignore"
154
+ - ".rspec"
155
+ - ".travis.yml"
156
+ - CODE_OF_CONDUCT.md
157
+ - Gemfile
158
+ - LICENSE.txt
159
+ - README.md
160
+ - Rakefile
161
+ - bin/console
162
+ - bin/setup
163
+ - exe/savvy
164
+ - lib/savvy.rb
165
+ - lib/savvy/application.rb
166
+ - lib/savvy/configuration.rb
167
+ - lib/savvy/configuration_dsl.rb
168
+ - lib/savvy/configurators/all.rb
169
+ - lib/savvy/configurators/redis.rb
170
+ - lib/savvy/configurators/sidekiq.rb
171
+ - lib/savvy/environment_reader.rb
172
+ - lib/savvy/root_finder.rb
173
+ - lib/savvy/templates/Savvyfile.rb.erb
174
+ - lib/savvy/utility.rb
175
+ - lib/savvy/version.rb
176
+ - savvy.gemspec
177
+ homepage: https://github.com/scryptmouse/savvy
178
+ licenses:
179
+ - MIT
180
+ metadata: {}
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubyforge_project:
197
+ rubygems_version: 2.6.8
198
+ signing_key:
199
+ specification_version: 4
200
+ summary: ENV-backed configuration helper for Redis, Sidekiq, etc
201
+ test_files: []