enviro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format nested
3
+
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@enviro_dev
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README ADDED
@@ -0,0 +1,5 @@
1
+ = Enviro
2
+
3
+ Add rails compatable environments, configutation and logging to any ruby object.
4
+
5
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+ Dir.glob(File.join(File.dirname(__FILE__), 'tasks/*.rake')).each {|f| load f }
@@ -0,0 +1,13 @@
1
+ <%
2
+ opts = '--color --require features'
3
+ rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
4
+ rerun_opts = rerun.to_s.strip.empty? ?
5
+ "#{opts} --format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} features" :
6
+ "#{opts} --format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
7
+ std_opts = "#{opts} --format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
8
+ %>
9
+ default: <%= std_opts %> features
10
+ wip: <%= opts %> --tags @wip:3 --wip features
11
+ rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
12
+ autotest: <%= std_opts %>
13
+ autotest-all: <%= std_opts %>
data/enviro.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "enviro/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "enviro"
7
+ s.version = Enviro::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bram Swenson"]
10
+ s.email = ["bram@craniumisajar.com"]
11
+ s.homepage = "http://github.com/bramswenson/enviro"
12
+ s.summary = %q{ Add rails like application wide environment configuration, logging and more. }
13
+ s.description = %q{ Add rails like application wide environment configuration, logging and more. }
14
+
15
+ s.rubyforge_project = "enviro"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency('cucumber')
23
+ s.add_development_dependency('rspec')
24
+ s.add_development_dependency('autotest-standalone')
25
+ s.add_development_dependency('autotest-growl')
26
+ s.add_development_dependency('simplecov')
27
+ s.add_development_dependency('ruby-debug19')
28
+ end
@@ -0,0 +1,8 @@
1
+ Feature: Enviro should be versioned
2
+
3
+ As an Enviro user
4
+ I should be able to figure out the current version
5
+ So I can respond as needed
6
+
7
+ Scenario: Enviro::VERSION
8
+ Then "Enviro::VERSION" should be "0.0.1"
@@ -0,0 +1,4 @@
1
+ Then /^"([^"]*)" should be "([^"]*)"$/ do |constant, value|
2
+ eval(constant).should == value
3
+ end
4
+
@@ -0,0 +1,3 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default, :development)
3
+ require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
@@ -0,0 +1,44 @@
1
+ module Enviro
2
+ module Configuration
3
+
4
+ class FileNotFound < StandardError; end
5
+ class UnknownEnvironment < StandardError; end
6
+
7
+ def self.included(base)
8
+ base.send(:extend, ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def configuration_path_env(value=nil)
14
+ @_configuration_path_env ||= 'ENVY_CONF_PATH'
15
+ @_configuration_path_env = value.to_s.upcase unless value.nil?
16
+ @_configuration_path_env
17
+ end
18
+
19
+ def configuration_path
20
+ @_configuration_path ||= (ENV[self.configuration_path_env]||'enviro.yml')
21
+ end
22
+
23
+ def configuration
24
+ @_configuration ||= _load_configuration_path
25
+ end
26
+
27
+ private
28
+
29
+ def _load_configuration_path
30
+ raise FileNotFound.new(self.configuration_path) unless
31
+ File.exists?(self.configuration_path)
32
+
33
+ @raw_configuration = YAML.load_file(self.configuration_path)
34
+
35
+ raise UnknownEnvironment.new(self.environment) unless
36
+ @raw_configuration.key?(self.environment)
37
+
38
+ OpenStruct.new(@raw_configuration[self.environment].merge(:environment => self.environment))
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,14 @@
1
+ module Enviro
2
+ module Environate
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ attr_accessor :environment, :configuration, :logger
7
+ end
8
+ base.send(:include, Enviro::Environment)
9
+ base.send(:include, Enviro::Configuration)
10
+ base.send(:include, Enviro::Logger)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ module Enviro
2
+ module Environment
3
+
4
+ def self.included(base)
5
+ base.send(:extend, ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def environment
10
+ @environment ||= _setup_environment
11
+ end
12
+
13
+ def environment=(value)
14
+ @environment = value
15
+ end
16
+ alias :env :environment
17
+
18
+ def env?(value)
19
+ self.environment == value.to_sym
20
+ end
21
+
22
+ private
23
+
24
+ def _setup_environment
25
+ if defined?(Rails)
26
+ Rails.env
27
+ else
28
+ ENV['ENVY_ENV'].nil? ? :development : ENV['ENVY_ENV'].to_sym
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,49 @@
1
+ module Enviro
2
+ module Logger
3
+
4
+ class DirectoryNotFound < StandardError; end
5
+
6
+ def self.included(base)
7
+ base.send(:extend, ClassMethods)
8
+ base.instance_eval do
9
+ class << self
10
+ extend Forwardable
11
+ def_delegators :logger, :debug, :error, :fatal, :info, :warn
12
+ end
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ def logger_dir_env(value=nil)
19
+ @_logger_dir_env ||= 'ENVY_LOG_DIR'
20
+ @_logger_dir_env = value.to_s.upcase unless value.nil?
21
+ @_logger_dir_env
22
+ end
23
+
24
+ def logger_dir
25
+ @_logger_dir ||= ENV[self.logger_dir_env]
26
+ end
27
+
28
+ def logger
29
+ @_logger ||= _setup_logger_for_environment
30
+ end
31
+
32
+ private
33
+
34
+ def _setup_logger_for_environment
35
+ return ::Rails.logger if defined?(::Rails)
36
+ unless self.logger_dir.nil?
37
+ raise DirectoryNotFound unless File::directory?(self.logger_dir)
38
+ @logger_path = File.join(self.logger_dir, "#{self.environment}.log")
39
+ else
40
+ @logger_path = STDOUT
41
+ end
42
+ require 'logger'
43
+ return ::Logger.new(@logger_path)
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+
@@ -0,0 +1,3 @@
1
+ module Enviro
2
+ VERSION = "0.0.1"
3
+ end
data/lib/enviro.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/setup"
2
+ Bundler.require
3
+ require 'forwardable'
4
+
5
+ module Enviro
6
+ autoload :Environment, 'enviro/environment'
7
+ autoload :Configuration, 'enviro/configuration'
8
+ autoload :Logger, 'enviro/logger'
9
+ autoload :Environate, 'enviro/environate'
10
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enviro::Configuration do
4
+ context "with standard environment variable" do
5
+ before(:each) do
6
+ Object.send(:remove_const, :TestEnviroConfiguration) if defined?(TestEnviroConfiguration)
7
+ ENV['ENVY_CONF_PATH'] = '/tmp/enviro.yml'
8
+ class TestEnviroConfiguration
9
+ include Enviro::Environment
10
+ include Enviro::Configuration
11
+ end
12
+ config = {
13
+ :development => {
14
+ },
15
+ :test => {
16
+ },
17
+ :production => {
18
+ },
19
+ }
20
+ File.open(ENV['ENVY_CONF_PATH'], 'w') do |f|
21
+ f.write(YAML.dump(config))
22
+ end
23
+ end
24
+
25
+ it "should have configuration available as a struct like object" do
26
+ TestEnviroConfiguration.configuration.should respond_to(:methods)
27
+ end
28
+
29
+ it "should have configuration for current environment" do
30
+ TestEnviroConfiguration.configuration.environment.should be(:development)
31
+ end
32
+
33
+ it "should raise when configuration file is not found" do
34
+ ENV['ENVY_CONF_PATH'] = 'who_the_heck_knows'
35
+ expect {
36
+ TestEnviroConfiguration.configuration
37
+ }.should raise_error(Enviro::Configuration::FileNotFound)
38
+ end
39
+
40
+ it "should raise when configuration for current environment is not found" do
41
+ ENV['ENVY_ENV'] = 'who_the_heck_knows'
42
+ expect {
43
+ TestEnviroConfiguration.configuration.environment
44
+ }.should raise_error(Enviro::Configuration::UnknownEnvironment)
45
+ end
46
+ end
47
+
48
+ context "with custom environment variable" do
49
+ before(:each) do
50
+ Object.send(:remove_const, :TestEnviroConfiguration) if defined?(TestEnviroConfiguration)
51
+ ENV['CUSTOM_PATH'] = '/tmp/enviro_custom.yml'
52
+ class TestEnviroConfiguration
53
+ include Enviro::Environment
54
+ include Enviro::Configuration
55
+ configuration_path_env :custom_path
56
+ end
57
+ config = {
58
+ :development => {
59
+ },
60
+ :test => {
61
+ },
62
+ :production => {
63
+ },
64
+ }
65
+ File.open(ENV['CUSTOM_PATH'], 'w') do |f|
66
+ f.write(YAML.dump(config))
67
+ end
68
+ end
69
+
70
+ it "should upcase the configuration_path_env attribute" do
71
+ TestEnviroConfiguration.configuration_path_env.should == 'CUSTOM_PATH'
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enviro::Environate do
4
+ context "the extended class" do
5
+
6
+ context "should respond to" do
7
+ %w( environment configuration logger).each do |meth|
8
+ it "#{meth}" do
9
+ EnviroMe.should respond_to(meth.to_sym)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enviro::Environment do
4
+ before(:each) do
5
+ Object.send(:remove_const, :TestEnviroEnvironment) if defined?(TestEnviroEnvironment)
6
+ class TestEnviroEnvironment
7
+ include Enviro::Environment
8
+ end
9
+ end
10
+
11
+ it "should have the environment attribute set the default when no session ENV setting is set" do
12
+ ENV['ENVY_ENV'] = nil
13
+ ENV['ENVY_ENV'].should be_nil
14
+ TestEnviroEnvironment.environment.should == :development
15
+ end
16
+
17
+ it "should have the environment attribute set the same as the session ENV setting" do
18
+ ENV['ENVY_ENV'] = 'production'
19
+ ENV['ENVY_ENV'].should == "production"
20
+ TestEnviroEnvironment.environment.should == :production
21
+ end
22
+
23
+ it "should alias env to environment" do
24
+ ENV['ENVY_ENV'] = nil
25
+ ENV['ENVY_ENV'].should be_nil
26
+ TestEnviroEnvironment.env.should == :development
27
+ end
28
+
29
+ it "should return true on env?(value) when value is environment" do
30
+ ENV['ENVY_ENV'] = nil
31
+ ENV['ENVY_ENV'].should be_nil
32
+ TestEnviroEnvironment.env?('development').should be_true
33
+ end
34
+
35
+ it "should return false on env?(value) when value is not environment" do
36
+ ENV['ENVY_ENV'] = nil
37
+ ENV['ENVY_ENV'].should be_nil
38
+ TestEnviroEnvironment.env?('production').should be_false
39
+ end
40
+
41
+ context "within Rails" do
42
+ before(:each) do
43
+ module Rails
44
+ def self.env
45
+ :production
46
+ end
47
+ end
48
+ end
49
+
50
+ after(:each) do
51
+ Object.send(:remove_const, :Rails) if defined?(Rails)
52
+ end
53
+
54
+ it "should inherit the environment from Rails should it be loaded" do
55
+ ENV['ENVY_ENV'] = nil
56
+ ENV['ENVY_ENV'].should be_nil
57
+ TestEnviroEnvironment.environment.should == :production
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enviro::Logger do
4
+ context "with standard environment variable" do
5
+ before(:each) do
6
+ Object.send(:remove_const, :TestEnviroLogger) if defined?(TestEnviroLogger)
7
+ class TestEnviroLogger
8
+ include Enviro::Environment
9
+ include Enviro::Logger
10
+ end
11
+ ENV['ENVY_LOG_DIR'] = nil
12
+ ENV['ENVY_CONF_PATH'] = nil
13
+ ENV['ENVY_ENV'] = nil
14
+ end
15
+
16
+ it "should have logger like object available" do
17
+ TestEnviroLogger.logger.should respond_to(:debug)
18
+ end
19
+
20
+ it "should log to STDOUT if ENVY_LOG_DIR is nil" do
21
+ TestEnviroLogger.logger.instance_variable_get(:@logdev).dev.should be(STDOUT)
22
+ end
23
+
24
+ it "should log to /tmp/development.log if ENVY_LOG_DIR is /tmp and ENVY_ENV is development" do
25
+ ENV['ENVY_LOG_DIR'] = '/tmp'
26
+ TestEnviroLogger.logger.instance_variable_get(:@logdev).dev.path.should == '/tmp/development.log'
27
+ end
28
+
29
+ %w( debug error fatal info warn ).each do |level|
30
+ it "should allow logging to level #{level}" do
31
+ expect { TestEnviroLogger.send(level.to_sym, 'test') }.should_not raise_error
32
+ end
33
+ end
34
+
35
+ it "should raise when log file is not writable" do
36
+ ENV['ENVY_LOG_DIR'] = '/tmp/this_dir_is_not_here'
37
+ expect {
38
+ TestEnviroLogger.logger
39
+ }.should raise_error(Enviro::Logger::DirectoryNotFound)
40
+ end
41
+
42
+ context "within Rails" do
43
+ before(:each) do
44
+ module ::Rails
45
+ def self.logger
46
+ ::Logger.new(STDOUT)
47
+ end
48
+ end
49
+ end
50
+
51
+ after(:each) do
52
+ Object.send(:remove_const, :Rails) if defined?(Rails)
53
+ end
54
+
55
+ it "should inherit the logger from Rails should it be loaded" do
56
+ # if we set the log dir to something that doesn't exist
57
+ # we can more sure that the Rails.logger is getting used
58
+ # since otherwise we would raise an error due to the missing dir
59
+ ENV['ENVY_LOG_DIR'] = '/tmp/this_should_not_exits'
60
+ TestEnviroLogger.logger.instance_variable_get(:@logdev).dev.should == Rails.logger.instance_variable_get(:@logdev).dev
61
+ end
62
+ end
63
+ end
64
+ context "with custom environment variable" do
65
+ before(:each) do
66
+ Object.send(:remove_const, :TestEnviroLogger) if defined?(TestEnviroLogger)
67
+ ENV['CUSTOM_LOG'] = '/tmp'
68
+ class TestEnviroLogger
69
+ include Enviro::Environment
70
+ include Enviro::Logger
71
+ logger_dir_env :custom_log
72
+ end
73
+ ENV['ENVY_LOG_DIR'] = nil
74
+ ENV['ENVY_CONF_PATH'] = nil
75
+ ENV['ENVY_ENV'] = nil
76
+ end
77
+
78
+ it "should upcase the logger_dir_env attribute" do
79
+ TestEnviroLogger.logger_dir_env.should == 'CUSTOM_LOG'
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enviro::VERSION do
4
+ it "should be 0.0.1" do
5
+ Enviro::VERSION.should == '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default, :development)
3
+
4
+ class EnviroMe
5
+ include Enviro::Environate
6
+ end
7
+
@@ -0,0 +1,8 @@
1
+ require 'cucumber'
2
+ require 'cucumber/rake/task'
3
+
4
+ Cucumber::Rake::Task.new(:features) do |t|
5
+ t.cucumber_opts = "--color --require features --format pretty features"
6
+ end
7
+ task :cucumber => :features
8
+ task :cuke => :features
@@ -0,0 +1 @@
1
+ task :default => [ :spec, :cuke ]
data/tasks/rspec.rake ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec) do |t|
4
+ t.fail_on_error = true
5
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enviro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bram Swenson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-03-04 00:00:00.000000000 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: cucumber
17
+ requirement: &20410940 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *20410940
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &20442280 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *20442280
37
+ - !ruby/object:Gem::Dependency
38
+ name: autotest-standalone
39
+ requirement: &20441860 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *20441860
48
+ - !ruby/object:Gem::Dependency
49
+ name: autotest-growl
50
+ requirement: &20441440 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *20441440
59
+ - !ruby/object:Gem::Dependency
60
+ name: simplecov
61
+ requirement: &20441020 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *20441020
70
+ - !ruby/object:Gem::Dependency
71
+ name: ruby-debug19
72
+ requirement: &20440600 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *20440600
81
+ description: ! ' Add rails like application wide environment configuration, logging
82
+ and more. '
83
+ email:
84
+ - bram@craniumisajar.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - .gitignore
90
+ - .rspec
91
+ - .rvmrc
92
+ - Gemfile
93
+ - README
94
+ - Rakefile
95
+ - config/cucumber.yml
96
+ - enviro.gemspec
97
+ - features/enviro/version.feature
98
+ - features/step_definitions/.initialize_steps.rb.swp
99
+ - features/step_definitions/version_steps.rb
100
+ - features/support/env.rb
101
+ - lib/enviro.rb
102
+ - lib/enviro/configuration.rb
103
+ - lib/enviro/environate.rb
104
+ - lib/enviro/environment.rb
105
+ - lib/enviro/logger.rb
106
+ - lib/enviro/version.rb
107
+ - spec/enviro/configuration_spec.rb
108
+ - spec/enviro/environate_spec.rb
109
+ - spec/enviro/environment_spec.rb
110
+ - spec/enviro/logger_spec.rb
111
+ - spec/enviro/version_spec.rb
112
+ - spec/spec_helper.rb
113
+ - tasks/cucumber.rake
114
+ - tasks/default.rake
115
+ - tasks/rspec.rake
116
+ has_rdoc: true
117
+ homepage: http://github.com/bramswenson/enviro
118
+ licenses: []
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project: enviro
137
+ rubygems_version: 1.5.3
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Add rails like application wide environment configuration, logging and more.
141
+ test_files: []