appenv 1.0.0 → 1.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aef3777b394fe61bdbb200d3cbd929ca376545f5cbbf00974addc76fce4837ea
4
+ data.tar.gz: 2cc83fdca529d448a6082d908453fb636b0b4a5e0f8c5159a17e5cce3d571932
5
+ SHA512:
6
+ metadata.gz: 2e934242f875a5cd60263420648df862f6bf372e35300193869ce6a1c47e0ffa88bc9ea1408a9aa72799a636d4338e5636d2cf8569f7e5fad1334fa499cef278
7
+ data.tar.gz: eb853acb47eadc57301dd987692a534da7ba1fcd4d2e67942decbc442a283dab947e6595174a9bd979c4b0064dcc0476e9ac04a3669160de0c68dd5f8f129bf2
@@ -21,8 +21,8 @@ class AppEnv::Environment < ActiveSupport::OrderedOptions
21
21
  # }
22
22
  #
23
23
  def expand(&blk)
24
- source = _compile_source_env
25
- blk.call(self, source)
24
+ @source = _compile_source_env
25
+ blk.call(self, @source)
26
26
  end
27
27
 
28
28
 
@@ -32,7 +32,7 @@ class AppEnv::Environment < ActiveSupport::OrderedOptions
32
32
  # # Simple variable assignment:
33
33
  # env.var1 = src.var_one
34
34
  #
35
- # # Variable assignment with `set`:
35
+ # # Variable assignment with a block:
36
36
  # env.set(:var2) {
37
37
  # if abcd?
38
38
  # 'xyz'
@@ -41,8 +41,65 @@ class AppEnv::Environment < ActiveSupport::OrderedOptions
41
41
  # end
42
42
  # }
43
43
  #
44
- def set(property, &blk)
45
- self.send("#{property}=", blk.call)
44
+ def set(property, value = nil)
45
+ self[property] = block_given? ? yield : value
46
+ end
47
+
48
+
49
+ # Creates a property in the env, taking the value from the source hash.
50
+ # If you want to modify the value from the source before setting it in
51
+ # the env, supply a block that takes the src value and returns the env value.
52
+ #
53
+ # Options:
54
+ # * :from -- if the source property name differs, supply it
55
+ # * :required -- raise an error if the property is not assigned a value
56
+ #
57
+ def apply(property, options = {})
58
+ if options.kind_of?(String) || options.kind_of?(Symbol)
59
+ options = { :from => options }
60
+ end
61
+ val = @source[options[:from] || property]
62
+ self[property] = block_given? ? yield(val) : val
63
+ assert(property) if options[:required]
64
+ self[property]
65
+ end
66
+
67
+
68
+ # Collect all the source properties that match the pattern as a hash.
69
+ # If a block is given, yield the hash to the block, which can modify values.
70
+ # Then merge the hash into the env.
71
+ #
72
+ def splat(pattern)
73
+ hash = ActiveSupport::OrderedOptions.new
74
+ @source.each_pair { |k, v| hash[k] ||= v if k.to_s.match(pattern) }
75
+ yield(hash) if block_given?
76
+ self.update(hash)
77
+ end
78
+
79
+
80
+ # After setting all the environment properties, assert which
81
+ # properties should exist. The Configurator will raise an
82
+ # exception listing the missing properties, if any.
83
+ #
84
+ def assert(*properties)
85
+ missing = properties.collect { |prop|
86
+ prop.to_s.upcase unless self[prop]
87
+ }.compact
88
+ raise("AppEnv requires #{missing.join(', ')}") if missing.any?
89
+ end
90
+
91
+
92
+ # Collect a line-by-line list of strings in the form: 'property = value'.
93
+ #
94
+ def to_a
95
+ collect { |k, v| "#{k} = #{v.inspect}" }
96
+ end
97
+
98
+
99
+ # Print a line-by-line list of env values to STDOUT.
100
+ #
101
+ def print(pad = '')
102
+ puts pad+to_a.join("\n"+pad)
46
103
  end
47
104
 
48
105
 
@@ -58,7 +115,7 @@ class AppEnv::Environment < ActiveSupport::OrderedOptions
58
115
 
59
116
  def _source_env_from_file
60
117
  src = {}
61
- return src unless File.exists?(@file)
118
+ return src unless File.exist?(@file)
62
119
  File.open(@file, 'r').each_line { |ln|
63
120
  cln = ln.strip.sub(/^export\s+/, '')
64
121
  next if cln.match(/^#/)
@@ -1,5 +1,5 @@
1
1
  module AppEnv
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,85 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: appenv
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 0
9
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
10
5
  platform: ruby
11
- authors:
6
+ authors:
12
7
  - Joseph Pearson
13
8
  autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
-
17
- date: 2013-06-05 00:00:00 +10:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: activesupport
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
25
17
  - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
30
20
  type: :runtime
31
- version_requirements: *id001
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
32
27
  description: Environment-variable compatible application configuration.
33
- email:
34
- - joseph@booki.sh
28
+ email: joseph@users.noreply.github.com
35
29
  executables: []
36
-
37
30
  extensions: []
38
-
39
31
  extra_rdoc_files: []
40
-
41
- files:
42
- - .gitignore
43
- - README.md
44
- - Rakefile
45
- - appenv.gemspec
32
+ files:
46
33
  - lib/appenv.rb
47
34
  - lib/appenv/environment.rb
48
35
  - lib/appenv/version.rb
49
- - test/data/env
50
- - test/test_helper.rb
51
- - test/unit/test_environment.rb
52
- has_rdoc: true
53
- homepage: ""
54
- licenses: []
55
-
36
+ homepage: https://github.com/joseph/appenv
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ github_repo: ssh://github.com/bksh/appenv
56
41
  post_install_message:
57
42
  rdoc_options: []
58
-
59
- require_paths:
43
+ require_paths:
60
44
  - lib
61
- required_ruby_version: !ruby/object:Gem::Requirement
62
- requirements:
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
63
47
  - - ">="
64
- - !ruby/object:Gem::Version
65
- segments:
66
- - 0
67
- version: "0"
68
- required_rubygems_version: !ruby/object:Gem::Requirement
69
- requirements:
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
70
52
  - - ">="
71
- - !ruby/object:Gem::Version
72
- segments:
73
- - 0
74
- version: "0"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
75
55
  requirements: []
76
-
77
- rubyforge_project:
78
- rubygems_version: 1.3.6
56
+ rubygems_version: 3.0.3.1
79
57
  signing_key:
80
- specification_version: 3
58
+ specification_version: 4
81
59
  summary: Application environment configuration
82
- test_files:
83
- - test/data/env
84
- - test/test_helper.rb
85
- - test/unit/test_environment.rb
60
+ test_files: []
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- .screenrc
data/README.md DELETED
@@ -1,35 +0,0 @@
1
- # APPENV
2
-
3
- A simple gem that gives you an application environment configuration object.
4
- Values are loaded from ENV or from an environment file such as `.env`.
5
-
6
- Similar to the `.env` feature in Foreman, but these values will be available
7
- to your application no matter how you launch it (script/rails, Passenger,
8
- Unicorn, rake, etc).
9
-
10
- For convenience, there is also compatibility with Bash-style environment
11
- declarations in your `.env` file; ie: `export ENV_VAR_NAME="foo"`.
12
-
13
- ## Example
14
-
15
- You'd typically create your configuration object in `config/application.rb`.
16
-
17
- ```ruby
18
- module ExampleProjectName
19
-
20
- Env = AppEnv::Environment.new { |env, src|
21
- env.domain = src.my_domain_variable || 'example.com'
22
- env.set(:access_codes) {
23
- src.access_codes ? src.access_codes.split : nil
24
- }
25
- }
26
-
27
- # class Application < Rails::Application
28
- # ... etc ...
29
- #
30
- # Your config is available globally in ExampleProjectName::Env.
31
- #
32
-
33
- end
34
-
35
- ```
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new { |t|
4
- t.libs << 'test'
5
- t.test_files = FileList['test/unit/test*.rb']
6
- t.verbose = true
7
- }
8
-
9
- desc("Run tests")
10
- task(:default => :test)
data/appenv.gemspec DELETED
@@ -1,18 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/appenv/version', __FILE__)
3
-
4
- Gem::Specification.new do |gem|
5
- gem.authors = ['Joseph Pearson']
6
- gem.email = ['joseph@booki.sh']
7
- gem.description = 'Environment-variable compatible application configuration.'
8
- gem.summary = 'Application environment configuration'
9
- gem.homepage = ''
10
-
11
- gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = 'appenv'
15
- gem.require_paths = ['lib']
16
- gem.version = AppEnv::VERSION
17
- gem.add_dependency('activesupport')
18
- end
data/test/data/env DELETED
@@ -1,3 +0,0 @@
1
- VAR_ONE="foo"
2
- export VAR_TWO="bar"
3
- # VAR_THREE="garply"
data/test/test_helper.rb DELETED
@@ -1,2 +0,0 @@
1
- require 'appenv'
2
- require 'test/unit'
@@ -1,30 +0,0 @@
1
- require 'test_helper'
2
-
3
- class AppEnv::TestEnvironment < Test::Unit::TestCase
4
-
5
- def test_sanity
6
- assert_nothing_raised {
7
- @env = AppEnv::Environment.new('test/data/env') { |env, src|
8
- env.var1 = src.var_one
9
- env.set(:var2) { src.var_two }
10
- env.var3 = src.var_three || 'grault'
11
- }
12
- }
13
- assert_equal('foo', @env.var1)
14
- assert_equal('bar', @env.var2)
15
- assert_equal('grault', @env.var3)
16
- end
17
-
18
-
19
- def test_expand
20
- @env = AppEnv::Environment.new('test/data/env') { |env, src|
21
- env.var1 = src.var_one
22
- }
23
- assert_equal('foo', @env.var1)
24
- @env.expand { |env, src|
25
- env.var2 = src.var_two
26
- }
27
- assert_equal('bar', @env.var2)
28
- end
29
-
30
- end