env_bang 0.2.8.1 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec44b0eec6674b6a0cdf6f16989121a5bda0d79b
4
- data.tar.gz: 58a68baf89c28c817f8b543cb5ec485aa83bef6f
3
+ metadata.gz: dfe99ce78ee95e20f85bd9467d2131b39550dd78
4
+ data.tar.gz: a24392f5e44fbcc89903e5c73d365eefd74d86d1
5
5
  SHA512:
6
- metadata.gz: a92587c42bda2d877ecb4fe61b7358579ec7869c01afd064c722dc87e46f278b6a4ad94cec923180d0a98e2ef13ba7383eb583a26738a54e8744d4bea8e20764
7
- data.tar.gz: 7fd541f39092230c9a3e84775b0be6aac4a4c9fd345efb4ff5659535732fa7450c2f67bc6db707a640c1c32b5cfb0f02fdb6c54d9930a3cb8645e3d2dd698f2e
6
+ metadata.gz: 9081e3b9ee0253b58ba4f1eb6561e1afc4d988c468b0b4a68a98b86442afde16c97a847b32b028cbc03a987763fb8f5f390f045b8c44231604603d172115df92
7
+ data.tar.gz: 180d9a2a0d823caec0f48dbb4f15e99329b82d69d058b6b703d6a865ddb01ae8a44bd14ed6969ab216b2cfc70e4a544076abd4624b0983905177ba30205f0765
data/README.md CHANGED
@@ -56,9 +56,6 @@ ENV!.config do
56
56
  end
57
57
  ```
58
58
 
59
- A single variable can also be configured with `ENV!.use MY_VAR`, but the `ENV!.config` block
60
- will typically be a cleaner place for all the variables for your app.
61
-
62
59
  Once a variable is specified with the `use` method, access it with
63
60
 
64
61
  ```ruby
@@ -126,7 +123,7 @@ end
126
123
  #### Default type conversion behavior
127
124
 
128
125
  If you don’t specify a `:class` option for a variable, ENV! defaults to a special
129
- type conversion called `:StringUnlessFalsey`. This coversion returns a string, unless
126
+ type conversion called `:StringUnlessFalsey`. This conversion returns a string, unless
130
127
  the value is a "falsey" string ('false', 'no', 'off', '0', 'disable', or 'disabled').
131
128
  To turn off this magic for one variable, pass in `class: String`. To disable it globally,
132
129
  set
@@ -168,6 +165,39 @@ ENV!['NUMBER_SET']
168
165
  #=> #<Set: {1, 3, 5, 7, 9}>
169
166
  ```
170
167
 
168
+ ## Implementation Notes
169
+
170
+ 1. ENV! is simply a method that returns ENV_BANG. In certain contexts
171
+ (like defining a class), the exclamation mark notation is not allowed,
172
+ so we use an alias to get this shorthand.
173
+
174
+ 2. Any method that can be run within an `ENV!.config` block can also be run
175
+ as a method directly on `ENV!`. For instance, instead of
176
+
177
+ ```ruby
178
+ ENV!.config do
179
+ add_class Set do
180
+ ...
181
+ end
182
+
183
+ use :NUMBER_SET, class: Set
184
+ end
185
+ ```
186
+
187
+ It would also work to run
188
+
189
+ ```ruby
190
+ ENV!.add_class Set do
191
+ ...
192
+ end
193
+
194
+ ENV!.use :NUMBER_SET, class: Set
195
+ ```
196
+
197
+ While the `config` block is designed to provide a cleaner configuration
198
+ file, calling the methods directly can occasionally be handy, such as when
199
+ trying things out in an IRB/Pry session.
200
+
171
201
  ## Contributing
172
202
 
173
203
  1. Fork it
data/lib/env_bang.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "env_bang/version"
2
+ require "env_bang/classes"
2
3
  require "env_bang/formatter"
3
4
 
4
5
  class ENV_BANG
@@ -7,11 +8,6 @@ class ENV_BANG
7
8
  instance_eval(&block)
8
9
  end
9
10
 
10
- def clear_config
11
- @vars = {}
12
- default_class nil
13
- end
14
-
15
11
  def use(var, *args)
16
12
  var = var.to_s
17
13
  description = args.first.is_a?(String) && args.shift
@@ -63,43 +59,6 @@ class ENV_BANG
63
59
  end
64
60
  end
65
61
  end
66
-
67
- module Classes
68
- class << self
69
- attr_writer :default_class
70
-
71
- def default_class
72
- @default_class ||= :StringUnlessFalsey
73
- end
74
-
75
- def cast(value, options = {})
76
- public_send(:"#{options.fetch(:class, default_class)}", value, options)
77
- end
78
-
79
- def boolean(value, options)
80
- !(value =~ /^(|0|disabled?|false|no|off)$/i)
81
- end
82
-
83
- def Array(value, options)
84
- options.delete(:class)
85
- options[:class] = options[:of] if options[:of]
86
- value.split(',').map { |value| cast(value.strip, options) }
87
- end
88
-
89
- def Symbol(value, options)
90
- value.to_sym
91
- end
92
-
93
- def StringUnlessFalsey(value, options)
94
- boolean(value, options) && value
95
- end
96
-
97
- # Delegate methods like Integer(), Float(), String(), etc. to the Kernel module
98
- def method_missing(klass, value, options, &block)
99
- Kernel.send(klass, value)
100
- end
101
- end
102
- end
103
62
  end
104
63
 
105
64
  def ENV!
@@ -0,0 +1,38 @@
1
+ class ENV_BANG
2
+ module Classes
3
+ class << self
4
+ attr_writer :default_class
5
+
6
+ def default_class
7
+ @default_class ||= :StringUnlessFalsey
8
+ end
9
+
10
+ def cast(value, options = {})
11
+ public_send(:"#{options.fetch(:class, default_class)}", value, options)
12
+ end
13
+
14
+ def boolean(value, options)
15
+ !(value =~ /^(|0|disabled?|false|no|off)$/i)
16
+ end
17
+
18
+ def Array(value, options)
19
+ options.delete(:class)
20
+ options[:class] = options[:of] if options[:of]
21
+ value.split(',').map { |value| cast(value.strip, options) }
22
+ end
23
+
24
+ def Symbol(value, options)
25
+ value.to_sym
26
+ end
27
+
28
+ def StringUnlessFalsey(value, options)
29
+ boolean(value, options) && value
30
+ end
31
+
32
+ # Delegate methods like Integer(), Float(), String(), etc. to the Kernel module
33
+ def method_missing(klass, value, options, &block)
34
+ Kernel.send(klass, value)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  class ENV_BANG
2
- VERSION = "0.2.8.1"
2
+ VERSION = "0.2.9"
3
3
  end
@@ -2,7 +2,8 @@ require_relative 'test_helper'
2
2
 
3
3
  describe ENV_BANG do
4
4
  before do
5
- ENV!.clear_config
5
+ ENV_BANG.instance_eval { @vars = nil }
6
+ ENV_BANG::Classes.default_class = nil
6
7
  end
7
8
 
8
9
  it "Raises exception if unconfigured ENV var requested" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_bang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8.1
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Camenisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-01 00:00:00.000000000 Z
11
+ date: 2014-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -56,6 +56,7 @@ files:
56
56
  - env_bang.gemspec
57
57
  - lib/env_bang-rails.rb
58
58
  - lib/env_bang.rb
59
+ - lib/env_bang/classes.rb
59
60
  - lib/env_bang/formatter.rb
60
61
  - lib/env_bang/version.rb
61
62
  - test/env_bang_test.rb