env 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.*
3
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1 @@
1
+ --markup markdown --title "env Documentation" --protected
@@ -0,0 +1,17 @@
1
+ ### 0.1.0 / 2011-01-22
2
+
3
+ * Initial release:
4
+ * Provides access to:
5
+ * `HOME` (or `HOMEPATH` on Windows)
6
+ * `PATH`
7
+ * `LD_LIBRARY_PATH`
8
+ * `SHELL`
9
+ * `TERM` and `COLORTERM`
10
+ * `COLOMNS`
11
+ * `LINES`
12
+ * `EDITOR`
13
+ * `BROWSER`
14
+ * `LANG`
15
+ * Added {Env}.
16
+ * Added {Env::Variables}.
17
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Hal Brodigan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,82 @@
1
+ # Env
2
+
3
+ * [Homepage](http://github.com/postmodern/env)
4
+ * [Issues](http://github.com/postmodern/env/issues)
5
+ * [Documentation](http://rubydoc.info/gems/env)
6
+ * Postmodern (postmodern.mod3 at gmail.com)
7
+
8
+ ## Description
9
+
10
+ {Env} provides a Ruby interface to common environment variables, used on
11
+ Linux, BSD, OSX and Windows.
12
+
13
+ ## Features
14
+
15
+ * Provides access to:
16
+ * `HOME` (or `HOMEPATH` on Windows)
17
+ * `PATH`
18
+ * `LD_LIBRARY_PATH`
19
+ * `SHELL`
20
+ * `TERM` and `COLORTERM`
21
+ * `COLOMNS`
22
+ * `LINES`
23
+ * `EDITOR`
24
+ * `BROWSER`
25
+ * `LANG`
26
+
27
+ ## Examples
28
+
29
+ require 'env'
30
+
31
+ Transparently access environment variables as a Hash:
32
+
33
+ Env['DESKTOP_SESSION']
34
+ # => "gnome"
35
+
36
+ Transparently access environment variables as Constants:
37
+
38
+ Env::DESKTOP_SESSION
39
+ # => "gnome"
40
+
41
+ Transparently access environment variables with methods:
42
+
43
+ Env.desktop_session
44
+ # => "gnome"
45
+
46
+ Parse complex variables:
47
+
48
+ Env.home
49
+ # => #<Pathname:/home/alice>
50
+
51
+ Env.paths
52
+ # => [#<Pathname:/usr/local/bin>, #<Pathname:/usr/bin>, #<Pathname:/bin>, #<Pathname:/usr/local/sbin>, #<Pathname:/usr/sbin>, #<Pathname:/sbin>]
53
+
54
+ Env.lang
55
+ # => ["en_US", "utf8"]
56
+
57
+ Env.terminal
58
+ # => "gnome-terminal"
59
+
60
+ Env.shell
61
+ # => "/bin/bash"
62
+
63
+ Env.editor
64
+ # => "vim"
65
+
66
+ Only access the common variables from your Class:
67
+
68
+ class Project
69
+
70
+ include Env::Variables
71
+
72
+ end
73
+
74
+ ## Install
75
+
76
+ $ gem install env
77
+
78
+ ## Copyright
79
+
80
+ Copyright (c) 2011 Hal Brodigan
81
+
82
+ See {file:LICENSE.txt} for details.
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ gem 'ore-tasks', '~> 0.3.0'
6
+ require 'ore/tasks'
7
+
8
+ Ore::Tasks.new
9
+ rescue LoadError => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
12
+ end
13
+
14
+ begin
15
+ gem 'rspec', '~> 2.4.0'
16
+ require 'rspec/core/rake_task'
17
+
18
+ RSpec::Core::RakeTask.new
19
+ rescue LoadError => e
20
+ task :spec do
21
+ abort "Please run `gem install rspec` to install RSpec."
22
+ end
23
+ end
24
+ task :default => :spec
25
+
26
+ begin
27
+ gem 'yard', '~> 0.6.0'
28
+ require 'yard'
29
+
30
+ YARD::Rake::YardocTask.new
31
+ rescue LoadError => e
32
+ task :yard do
33
+ abort "Please run `gem install yard` to install YARD."
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ begin
9
+ require 'ore/specification'
10
+ retry
11
+ rescue LoadError
12
+ STDERR.puts "The 'env.gemspec' file requires Ore."
13
+ STDERR.puts "Run `gem install ore-core` to install Ore."
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ name: env
2
+ summary: Provides a Ruby interface to common environment variables
3
+ description:
4
+ Provides a Ruby interface to common environment variables, used on
5
+ Windows, Linux, BSD and OSX.
6
+
7
+ license: MIT
8
+ authors: Postmodern
9
+ email: postmodern.mod3@gmail.com
10
+ homepage: http://github.com/postmodern/env
11
+ has_yard: true
12
+
13
+ development_dependencies:
14
+ ore-tasks: ~> 0.3.0
15
+ rspec: ~> 2.4.0
16
+ yard: ~> 0.6.0
@@ -0,0 +1,2 @@
1
+ require 'env/env'
2
+ require 'env/version'
@@ -0,0 +1,91 @@
1
+ require 'env/variables'
2
+
3
+ module Env
4
+ extend Variables
5
+
6
+ #
7
+ # Provides direct access to the environment variables.
8
+ #
9
+ # @param [String, Symbol] name
10
+ # The name of the environment variable.
11
+ #
12
+ # @return [String, nil]
13
+ # The value of the environment variable.
14
+ #
15
+ # @example
16
+ # Env['SHELL']
17
+ # # => "/bin/bash"
18
+ #
19
+ def Env.[](name)
20
+ env_hash[name.to_s]
21
+ end
22
+
23
+ #
24
+ # Sets an environment variable.
25
+ #
26
+ # @param [String, Symbol] name
27
+ # The name of the environment variable.
28
+ #
29
+ # @param [Object] value
30
+ # The value of the environment variable.
31
+ #
32
+ # @return [String]
33
+ # The String value of the environment variable.
34
+ #
35
+ def Env.[]=(name,value)
36
+ env_hash[name.to_s] = value.to_s
37
+ end
38
+
39
+ protected
40
+
41
+ #
42
+ # Provides transparent access to the environment variables.
43
+ #
44
+ # @param [Symbol] name
45
+ # The name of the environment variable.
46
+ #
47
+ # @return [String, nil]
48
+ # The value of the environment variable.
49
+ #
50
+ # @example
51
+ # Env::SHELL
52
+ # # => "/bin/bash"
53
+ #
54
+ def Env.const_missing(name)
55
+ Env[name.to_s]
56
+ end
57
+
58
+ #
59
+ # Provides transparent access to the environment variables.
60
+ #
61
+ # @param [Symbol] name
62
+ # The name of the environment variable.
63
+ #
64
+ # @return [String, nil]
65
+ # The value of the environment variable.
66
+ #
67
+ # @example
68
+ # Env.shell
69
+ # # => "/bin/bash"
70
+ #
71
+ # @example
72
+ # Env.shell = '/bin/zsh'
73
+ # # => "/bin/zsh"
74
+ #
75
+ def Env.method_missing(name,*arguments,&block)
76
+ name = name.to_s
77
+
78
+ if (arguments.length == 1 && name[-1..-1] == '=')
79
+ name.chop!
80
+ name.upcase!
81
+
82
+ return Env[name] = arguments.first
83
+ elsif arguments.empty?
84
+ name.upcase!
85
+
86
+ return Env[name]
87
+ end
88
+
89
+ super(name,*arguments,&block)
90
+ end
91
+ end
@@ -0,0 +1,145 @@
1
+ require 'pathname'
2
+
3
+ module Env
4
+ module Variables
5
+ #
6
+ # The environment variables.
7
+ #
8
+ # @return [Hash{String => String}]
9
+ # The Hash of environment variable names and values.
10
+ #
11
+ def env_hash
12
+ ENV
13
+ end
14
+
15
+ #
16
+ # The directories to search within for executables.
17
+ #
18
+ # @return [Array<Pathname>]
19
+ # The paths of the directories.
20
+ #
21
+ def paths
22
+ parse_paths(env_hash['PATH'])
23
+ end
24
+
25
+ #
26
+ # The directories to search within for libraries.
27
+ #
28
+ # @return [Array<Pathname>]
29
+ # The paths of the directories.
30
+ #
31
+ def ld_library_paths
32
+ parse_paths(env_hash['LD_LIBRARY_PATH'])
33
+ end
34
+
35
+ #
36
+ # The home directory.
37
+ #
38
+ # @return [Pathname, nil]
39
+ # The path of the home directory.
40
+ #
41
+ def home
42
+ if (home = (env_hash['HOME'] || env_hash['HOMEPATH']))
43
+ Pathname.new(home)
44
+ end
45
+ end
46
+
47
+ #
48
+ # The default language.
49
+ #
50
+ # @return [Array<String, String>]
51
+ # The language name and encoding.
52
+ #
53
+ def lang
54
+ env_hash['LANG'].split('.',2)
55
+ end
56
+
57
+ #
58
+ # The number of columns in the terminal.
59
+ #
60
+ # @return [Integer]
61
+ # The number of columns.
62
+ #
63
+ def columns
64
+ env_hash['COLUMNS'].to_i if env_hash['COLUMNS']
65
+ end
66
+
67
+ #
68
+ # The number of lines in the terminal.
69
+ #
70
+ # @return [Integer]
71
+ # The number of lines.
72
+ #
73
+ def lines
74
+ env_hash['LINES'].to_i if env_hash['LINES']
75
+ end
76
+
77
+ #
78
+ # The path of the default shell.
79
+ #
80
+ # @return [String, nil]
81
+ # The path to the default shell.
82
+ #
83
+ def shell
84
+ env_hash['SHELL']
85
+ end
86
+
87
+ #
88
+ # The name of the default shell.
89
+ #
90
+ # @return [String, nil]
91
+ # The program name of the shell.
92
+ #
93
+ def shell_name
94
+ File.basename(shell) if shell
95
+ end
96
+
97
+ #
98
+ # The default terminal to use.
99
+ #
100
+ # @return [String, nil]
101
+ # The name of the terminal program.
102
+ #
103
+ def terminal
104
+ env_hash['COLORTERM'] || env_hash['TERM']
105
+ end
106
+
107
+ #
108
+ # The default editor to use.
109
+ #
110
+ # @return [String, nil]
111
+ # The name of the editor program.
112
+ #
113
+ def editor
114
+ env_hash['EDITOR']
115
+ end
116
+
117
+ #
118
+ # The default browser to use.
119
+ #
120
+ # @return [String, nil]
121
+ # The name of the browser program.
122
+ #
123
+ def browser
124
+ env_hash['BROWSER']
125
+ end
126
+
127
+ protected
128
+
129
+ #
130
+ # Parses a String containing multiple paths.
131
+ #
132
+ # @return [Array<Pathname>]
133
+ # The multiple paths.
134
+ #
135
+ def parse_paths(paths)
136
+ if paths
137
+ paths.split(File::PATH_SEPARATOR).map do |path|
138
+ Pathname.new(path)
139
+ end
140
+ else
141
+ []
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,4 @@
1
+ module Env
2
+ # env version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require 'env'
3
+
4
+ describe Env do
5
+ let(:var_name) { 'PATH' }
6
+ let(:var_value) { ENV[var_name] }
7
+
8
+ it "should have a VERSION constant" do
9
+ subject.const_get('VERSION').should_not be_empty
10
+ end
11
+
12
+ it "should provide direct Hash access to env variables" do
13
+ subject[var_name].should == var_value
14
+ end
15
+
16
+ it "should allow directly setting env variables" do
17
+ subject['FOO'] = 'bar'
18
+
19
+ subject['FOO'].should == 'bar'
20
+ end
21
+
22
+ it "should provide transparent CONSTANT access to env variables" do
23
+ subject.const_get(var_name).should == var_value
24
+ end
25
+
26
+ it "should provide transparent method access to env variables" do
27
+ method_name = var_name.downcase
28
+
29
+ subject.send(method_name).should == var_value
30
+ end
31
+
32
+ it "should allow setting of environment variables via methods" do
33
+ subject.shell = '/bin/zsh'
34
+
35
+ subject['SHELL'].should == '/bin/zsh'
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ gem 'rspec', '~> 2.4.0'
2
+ require 'rspec'
3
+ require 'env/version'
4
+
5
+ include Env
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'env/variables'
3
+
4
+ describe Variables do
5
+ subject { Object.new.extend(Variables) }
6
+
7
+ let(:home) { '/home/alice' }
8
+ let(:term) { 'xterm' }
9
+ let(:shell) { '/bin/bash' }
10
+
11
+ before(:each) do
12
+ subject.stub!(:env_hash).and_return(
13
+ 'PATH' => '/usr/local/bin:/usr/bin:/bin',
14
+ 'HOME' => home,
15
+ 'TERM' => term,
16
+ 'LANG' => 'en_US.UTF8',
17
+ 'COLUMNS' => '80',
18
+ 'LINES' => '10',
19
+ 'SHELL' => '/bin/bash'
20
+ )
21
+ end
22
+
23
+ it "should parse the contents of the PATH variable" do
24
+ subject.paths.should == [
25
+ Pathname.new('/usr/local/bin'),
26
+ Pathname.new('/usr/bin'),
27
+ Pathname.new('/bin')
28
+ ]
29
+ end
30
+
31
+ it "should provide access to the HOME variable" do
32
+ subject.home.should == Pathname.new(home)
33
+ end
34
+
35
+ it "should check HOMEPATH if HOME is not set" do
36
+ subject.stub!(:env_hash).and_return('HOMEPATH' => home)
37
+
38
+ subject.home.should == Pathname.new(home)
39
+ end
40
+
41
+ it "should parse the LANG variable" do
42
+ name, encoding = subject.lang
43
+
44
+ name.should == 'en_US'
45
+ encoding.should == 'UTF8'
46
+ end
47
+
48
+ it "should parse the COLUMNS variable" do
49
+ subject.columns.should == 80
50
+ end
51
+
52
+ it "should parse the LINES variable" do
53
+ subject.lines.should == 10
54
+ end
55
+
56
+ it "should provide access to the SHELL variable" do
57
+ subject.shell.should == shell
58
+ end
59
+
60
+ it "should determine the program name of the current Shell" do
61
+ subject.shell_name.should == 'bash'
62
+ end
63
+
64
+ it "should determine the current Terminal" do
65
+ subject.terminal.should == term
66
+ end
67
+
68
+ it "should check COLORTERM before the TERM variable" do
69
+ subject.stub!(:env_hash).and_return(
70
+ 'COLORTERM' => 'gnome-terminal',
71
+ 'TERM' => term
72
+ )
73
+
74
+ subject.terminal.should == 'gnome-terminal'
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: env
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Postmodern
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-22 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ore-tasks
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 3
31
+ - 0
32
+ version: 0.3.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 4
46
+ - 0
47
+ version: 2.4.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: yard
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ - 6
61
+ - 0
62
+ version: 0.6.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: Provides a Ruby interface to common environment variables, used on Windows, Linux, BSD and OSX.
66
+ email: postmodern.mod3@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - README.md
73
+ - ChangeLog.md
74
+ - LICENSE.txt
75
+ files:
76
+ - .document
77
+ - .rspec
78
+ - .yardopts
79
+ - ChangeLog.md
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - env.gemspec
84
+ - gemspec.yml
85
+ - lib/env.rb
86
+ - lib/env/env.rb
87
+ - lib/env/variables.rb
88
+ - lib/env/version.rb
89
+ - spec/env_spec.rb
90
+ - spec/spec_helper.rb
91
+ - spec/variables_spec.rb
92
+ has_rdoc: yard
93
+ homepage: http://github.com/postmodern/env
94
+ licenses:
95
+ - MIT
96
+ post_install_message:
97
+ rdoc_options: []
98
+
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project: env
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Provides a Ruby interface to common environment variables
124
+ test_files:
125
+ - spec/variables_spec.rb
126
+ - spec/env_spec.rb