smart_env 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ coverage
data/Gemfile CHANGED
@@ -1,7 +1,6 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in env.gemspec
1
+ source :gemcutter
4
2
  gemspec
5
3
 
6
- gem 'ruby-debug19'
7
- gem 'rspec'
4
+ group :development do
5
+ gem 'ruby-debug19', :platform => :mri_19
6
+ end
data/README.md CHANGED
@@ -25,7 +25,7 @@ Attach hooks to your ENV vars and wrap them in Proxy Objects.
25
25
 
26
26
  ### With a block
27
27
  class TestProxy
28
- def initialize(value)
28
+ def initialize(key, value)
29
29
  end
30
30
  end
31
31
 
data/Rakefile CHANGED
@@ -1 +1,21 @@
1
+ require 'bundler'
2
+ Bundler.require
1
3
  require 'bundler/gem_tasks'
4
+
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc 'Default: run specs.'
8
+ task :default => :spec
9
+
10
+ desc "Run specs"
11
+ RSpec::Core::RakeTask.new do |t|
12
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
13
+ # Put spec opts in a file named .rspec in root
14
+ end
15
+
16
+ desc "Generate code coverage"
17
+ RSpec::Core::RakeTask.new(:coverage) do |t|
18
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
19
+ t.rcov = true
20
+ t.rcov_opts = ['--exclude', 'spec']
21
+ end
@@ -2,6 +2,10 @@ require 'uri'
2
2
  require 'cgi'
3
3
  require 'forwardable'
4
4
 
5
+ class BasicObject
6
+ instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
7
+ end unless defined?(BasicObject)
8
+
5
9
  module SmartEnv
6
10
  class UriProxy < BasicObject
7
11
  attr_reader :params
@@ -1,3 +1,3 @@
1
1
  module SmartEnv
2
- VERSION = "0.1.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/smart_env.rb CHANGED
@@ -5,14 +5,10 @@ module SmartEnv
5
5
  attr_accessor :registry
6
6
 
7
7
  def clear_registry
8
- @registry = []
8
+ @registry = empty_registry
9
9
  end
10
10
 
11
- def reset_registry
12
- @registry = default
13
- end
14
-
15
- def default
11
+ def empty_registry
16
12
  []
17
13
  end
18
14
 
@@ -25,28 +21,24 @@ module SmartEnv
25
21
  end
26
22
 
27
23
  def when(&block)
28
- raise "Block must take 0 or 2 arguments: key and value" unless (block.arity == 0 || block.arity == 2)
24
+ raise "Block must take 0 or 2 arguments: key and value" unless (block.arity < 1 || block.arity == 2)
29
25
  registry << [@class, block]
30
26
  self
31
27
  end
32
28
 
33
29
  def registry
34
- @registry ||= default
35
- end
36
-
37
- def unload!
38
- class << self
39
- alias_method :[], :get
40
- # alias_method :[]=, :set
41
- end
30
+ @registry ||= empty_registry
42
31
  end
43
32
 
44
33
  def self.extended(base)
34
+ raise RuntimeError.new("#{base.inspect} doesn't respond to #[]") \
35
+ unless base.respond_to? :[]
36
+
45
37
  class << base
46
- alias_method :get, :[]
38
+ alias_method :__get, :[]
47
39
 
48
40
  def [](key)
49
- value = get(key)
41
+ value = __get(key)
50
42
  registry.each do |klass, condition|
51
43
  result = condition.call(key, value) rescue false
52
44
  value = klass.new(key, value) if value && result
data/smart_env.gemspec CHANGED
@@ -17,4 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
+ s.add_development_dependency 'rspec', '~> 2.0'
21
+ s.add_development_dependency 'rcov'
20
22
  end
@@ -1,18 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SmartEnv, 'load/unload' do
4
+ before do
5
+ class PassOne < String
6
+ attr_accessor :one
7
+ def initialize(key, value)
8
+ @one = true
9
+ super(value)
10
+ end
11
+ end
12
+
13
+ @env = {}
14
+ end
15
+
4
16
  context "on extend" do
5
17
  before do
6
- @env = {}
7
18
  @env.extend SmartEnv
19
+ end
8
20
 
9
- class PassOne < String
10
- attr_accessor :one
11
- def initialize(key, value)
12
- @one = true
13
- super(value)
14
- end
15
- end
21
+ after do
22
+ @env.clear_registry if @env
16
23
  end
17
24
 
18
25
  it "should not bleed" do
@@ -24,4 +31,16 @@ describe SmartEnv, 'load/unload' do
24
31
  @env['FOO'].one.should be_true
25
32
  end
26
33
  end
34
+
35
+ context "::clear_registry" do
36
+ before do
37
+ @env.extend SmartEnv
38
+ @env.use(PassOne).when{ true }
39
+ end
40
+
41
+ it "should clear the hooks" do
42
+ @env.clear_registry
43
+ @env['FOO'].should_not respond_to :one
44
+ end
45
+ end
27
46
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,8 @@
1
- require_relative '../lib/smart_env'
1
+ require File.expand_path('../../lib/smart_env', __FILE__)
2
+ require 'rspec/core'
2
3
 
3
4
  RSpec.configure do |config|
4
5
  config.after(:each) do
5
- ENV.reset_registry
6
- @env.reset_registry if @env
6
+ ENV.clear_registry
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-06 00:00:00.000000000 -07:00
13
- default_executable:
14
- dependencies: []
12
+ date: 2011-10-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70126964368960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70126964368960
25
+ - !ruby/object:Gem::Dependency
26
+ name: rcov
27
+ requirement: &70126964368000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70126964368000
15
36
  description: Allows you to register Proxy classes for ENV vars by using blocks to
16
37
  match keys and values
17
38
  email:
@@ -34,7 +55,6 @@ files:
34
55
  - spec/registry_spec.rb
35
56
  - spec/spec_helper.rb
36
57
  - spec/uri_spec.rb
37
- has_rdoc: true
38
58
  homepage: ''
39
59
  licenses: []
40
60
  post_install_message:
@@ -47,15 +67,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
67
  - - ! '>='
48
68
  - !ruby/object:Gem::Version
49
69
  version: '0'
70
+ segments:
71
+ - 0
72
+ hash: 3797767523412485183
50
73
  required_rubygems_version: !ruby/object:Gem::Requirement
51
74
  none: false
52
75
  requirements:
53
76
  - - ! '>='
54
77
  - !ruby/object:Gem::Version
55
78
  version: '0'
79
+ segments:
80
+ - 0
81
+ hash: 3797767523412485183
56
82
  requirements: []
57
83
  rubyforge_project: smart_env
58
- rubygems_version: 1.6.2
84
+ rubygems_version: 1.8.10
59
85
  signing_key:
60
86
  specification_version: 3
61
87
  summary: Lazily proxy ENV values