anyway_config 0.3.0 → 0.4.0

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: 19177bb68f7f250550ee553fb36c41f33fe181b7
4
- data.tar.gz: f6fa6a2cb3f29c3434a0c8f660c97adc74a3f6d1
3
+ metadata.gz: 458041a187fa4057159e72a80b71b2f5c2110b18
4
+ data.tar.gz: e96284b36eea6da6390ea0ebf2e029f7ebda186c
5
5
  SHA512:
6
- metadata.gz: e1b58e60a43d24867f9a6e81a4ea032ac94b373592638b150001aa8e105e51dea2289bc84acd2edece19a1956f369c1ec522b62adcd81600b0001a8190e5a936
7
- data.tar.gz: 164ea5df4fe0444e6f3e64df243c7602c2c0dbaf5615442c19246e20f75bedf3ba0a662028831eb11852f726109aa3e1fd5546570e3cee0889f3282551c1a8c7
6
+ metadata.gz: 4b677bda47bc679d8d50b80a930904f13678ffb36533fc9ccbc05f1e5dd39d549173995abd532dc47b7a70958fe7f381e423a6b77524a0fbcec69ebaa649c481
7
+ data.tar.gz: a9ca0a93fbfc54679a06665a0b67ce9b9fa8be644a62c81be196c392cf511d01a4e3a54eaf68ea07de7591eb5e3bace8acb6a221761225e191c64c243c329047
data/README.md CHANGED
@@ -15,7 +15,7 @@ Configure your gemspec
15
15
  ```ruby
16
16
  Gem::Specification.new do |s|
17
17
  ...
18
- s.add_dependancy 'anyway_config', "~>0.1"
18
+ s.add_dependancy 'anyway_config', "~>0.4"
19
19
  ...
20
20
  end
21
21
  ```
@@ -72,7 +72,7 @@ You can use `clear` and `reload` functions on your config (which do exactly what
72
72
  In your Gemfile
73
73
 
74
74
  ```ruby
75
- require 'anyway_config', "~>0.2"
75
+ require 'anyway_config', "~>0.4"
76
76
  ```
77
77
 
78
78
  In your code
@@ -106,9 +106,29 @@ Environmental variables for your config should start with your module name (or c
106
106
 
107
107
  For example, if your module is called "MyCoolGem" then your env var "MYCOOLGEM_PASSWORD" is used as `config.password`.
108
108
 
109
+ Environment variables are type-casted (case-insensitive).
110
+ Examples:
111
+ - "True", "T" and "yes" to `true`;
112
+ - "False", "f" and "no" to `false`;
113
+ - "nil" and "null" to `nil` (do you really need it?);
114
+ - "123" to 123 and "3.14" to 3.14.
115
+
109
116
  *Anyway Config* supports nested (_hashed_) environmental variables. Just separate keys with double-underscore.
110
117
  For example, "MYCOOLGEM_OPTIONS__VERBOSE" is transformed to `config.options.verbose`.
111
118
 
119
+ Array values are also supported:
120
+
121
+ ```ruby
122
+ # Suppose ENV["MYCOOLGEM_IDS"] = '1,2,3'
123
+ config.ids #=> [1,2,3]
124
+ ```
125
+
126
+ If you want to provide text-like env variable which contain commas then wrap it into quotes:
127
+
128
+ ```ruby
129
+ MYCOOLGEM="Nif-Nif, Naf-Naf and Nouf-Nouf"
130
+ ```
131
+
112
132
  ## Using without Rails
113
133
 
114
134
  AnywayConfig can be used without Rails too.
data/lib/anyway/config.rb CHANGED
@@ -26,7 +26,7 @@ module Anyway
26
26
 
27
27
  private
28
28
  def extract_name
29
- self.name[/^(\w+)/].underscore
29
+ name[/^(\w+)/].underscore
30
30
  end
31
31
  end
32
32
 
@@ -43,7 +43,7 @@ module Anyway
43
43
 
44
44
  def clear
45
45
  self.class.config_attributes.each do |attr|
46
- self.send("#{attr}=", nil)
46
+ send("#{attr}=", nil)
47
47
  end
48
48
  self
49
49
  end
@@ -51,7 +51,7 @@ module Anyway
51
51
  def load
52
52
  config = load_from_sources self.class.defaults.deep_dup
53
53
  config.each do |key, val|
54
- self.send("#{key}=",val)
54
+ set_value(key, val)
55
55
  end
56
56
  end
57
57
 
@@ -73,5 +73,13 @@ module Anyway
73
73
  config.deep_merge! (Anyway.env.send(@config_name) || {})
74
74
  config
75
75
  end
76
+
77
+ private
78
+
79
+ # safe way to assing config value
80
+ # checks that key exists in config
81
+ def set_value(key, val)
82
+ send("#{key}=", val) if self.class.config_attributes.include?(key.to_sym)
83
+ end
76
84
  end
77
85
  end
data/lib/anyway/env.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  module Anyway
2
2
  class Env
3
+
4
+ # Regexp to detect array values
5
+ # Array value is a values that contains at least one comma
6
+ # and doesn't start/end with quote
7
+
8
+ ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/
9
+
3
10
  def initialize
4
11
  @data = {}
5
12
  load
@@ -28,7 +35,7 @@ module Anyway
28
35
  ENV.each_pair do |key, val|
29
36
  if config_key?(key)
30
37
  mod, path = extract_module_path(key)
31
- set_by_path(get_hash(@data, mod), path, val)
38
+ set_by_path(get_hash(@data, mod), path, serialize_val(val))
32
39
  end
33
40
  end
34
41
  end
@@ -55,5 +62,26 @@ module Anyway
55
62
  def get_hash(from, name)
56
63
  (from[name] ||= {}.with_indifferent_access)
57
64
  end
65
+
66
+ def serialize_val(value)
67
+ case value
68
+ when ARRAY_RXP
69
+ value.split(/\s*,\s*/).map(&method(:serialize_val))
70
+ when /\A(true|t|yes|y)\z/i
71
+ true
72
+ when /\A(false|f|no|n)\z/i
73
+ false
74
+ when /\A(nil|null)\z/i
75
+ nil
76
+ when /\A\d+\z/
77
+ value.to_i
78
+ when /\A\d*\.\d+\z/
79
+ value.to_f
80
+ when /\A['"].*['"]\z/
81
+ value.gsub(/(\A['"]|['"]\z)/,'')
82
+ else
83
+ value
84
+ end
85
+ end
58
86
  end
59
87
  end
@@ -1,3 +1,3 @@
1
1
  module Anyway
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/spec/config_spec.rb CHANGED
@@ -48,7 +48,7 @@ describe Anyway::Config do
48
48
  ENV['COOL_PORT'] = '80'
49
49
  ENV['COOL_USER__NAME'] = 'john'
50
50
  Anyway.env.reload
51
- expect(conf.port).to eq '80'
51
+ expect(conf.port).to eq 80
52
52
  expect(conf.user[:name]).to eq 'john'
53
53
  end
54
54
  end
@@ -73,7 +73,7 @@ describe Anyway::Config do
73
73
  ENV['COOL_USER__NAME'] = 'john'
74
74
  Anyway.env.reload
75
75
  conf.reload
76
- expect(conf.port).to eq '80'
76
+ expect(conf.port).to eq 80
77
77
  expect(conf.user[:name]).to eq 'john'
78
78
  end
79
79
  end
@@ -86,7 +86,7 @@ describe Anyway::Config do
86
86
  ENV['MYAPP_NAME'] = 'my_app'
87
87
  Anyway.env.reload
88
88
  data = Anyway::Config.for(:my_app)
89
- expect(data[:test]).to eq '1'
89
+ expect(data[:test]).to eq 1
90
90
  expect(data[:name]).to eq 'my_app'
91
91
  if Rails.application.respond_to?(:secrets)
92
92
  expect(data[:secret]).to eq 'my_secret'
@@ -19,9 +19,11 @@ test:
19
19
  user:
20
20
  name: test
21
21
  password: test
22
+ bullshit: 'mooo'
22
23
  my_app:
23
24
  name: 'app'
24
25
  secret: 'my_secret'
26
+ elshit: 'UUU'
25
27
 
26
28
  # Do not keep production secrets in the repository,
27
29
  # instead read values from the environment.
data/spec/env_spec.rb CHANGED
@@ -13,9 +13,20 @@ describe Anyway::Env do
13
13
  it "should load hash values" do
14
14
  ENV['TESTO_DATA__ID'] = '1'
15
15
  ENV['TESTO_DATA__META__NAME'] = 'meta'
16
- ENV['TESTO_DATA__META__VAL'] = '2'
17
- expect(env.testo[:data][:id]).to eq '1'
16
+ ENV['TESTO_DATA__META__VAL'] = 'true'
17
+ expect(env.testo[:data][:id]).to eq 1
18
18
  expect(env.testo[:data][:meta][:name]).to eq 'meta'
19
- expect(env.testo[:data][:meta][:val]).to eq '2'
19
+ expect(env.testo[:data][:meta][:val]).to be_truthy
20
+ end
21
+
22
+ it "should load array values" do
23
+ ENV['TESTO_DATA__IDS'] = '1,2, 3'
24
+ ENV['TESTO_DATA__META__NAMES'] = 'meta, kotleta'
25
+ ENV['TESTO_DATA__META__SIZE'] = '2'
26
+ ENV['TESTO_DATA__TEXT'] = '"C\'mon, everybody"'
27
+ expect(env.testo[:data][:ids]).to include(1,2,3)
28
+ expect(env.testo[:data][:meta][:names]).to include('meta','kotleta')
29
+ expect(env.testo[:data][:meta][:size]).to eq 2
30
+ expect(env.testo[:data][:text]).to eq "C'mon, everybody"
20
31
  end
21
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyway_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vlad Dem
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-23 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport