figaro 0.4.1 → 0.5.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.
data/.travis.yml CHANGED
@@ -8,7 +8,6 @@ rvm:
8
8
  - 1.8.7
9
9
  - 1.9.2
10
10
  - 1.9.3
11
- - ruby-head
12
11
  before_script:
13
12
  - unset RAILS_ENV
14
13
  - unset RACK_ENV
data/README.md CHANGED
@@ -44,6 +44,16 @@ Pusher.key = ENV["PUSHER_KEY"]
44
44
  Pusher.secret = ENV["PUSHER_SECRET"]
45
45
  ```
46
46
 
47
+ In addition, you can access these same configuration values through Figaro itself:
48
+
49
+ ```ruby
50
+ Pusher.app_id = Figaro.env.pusher_app_id
51
+ Pusher.key = Figaro.env.pusher_key
52
+ Pusher.secret = Figaro.env.pusher_secret
53
+ ```
54
+
55
+ But wait… I thought configuration via constant was bad! Well, this is different. Rather than storing a _copy_ of `ENV` internally, `Figaro.env` passes directly through to `ENV`, making it just like using `ENV` itself. So why two approaches? Having your configurations available via method calls makes it easy to stub them out in tests. Either way is fine. The choice is yours!
56
+
47
57
  If your app requires Rails-environment-specific configuration, you can also namespace your configuration under a key for `Rails.env`.
48
58
 
49
59
  ```yaml
@@ -82,6 +92,8 @@ Optionally, you can pass in the name of the Heroku app:
82
92
  rake figaro:heroku[my-awesome-app]
83
93
  ```
84
94
 
95
+ Additionally, if `RAILS_ENV` is configured on your Heroku server, Figaro will use that environment automatically in determining your proper configuration.
96
+
85
97
  ## What if I'm not using Heroku?
86
98
 
87
99
  No problem. Just add `config/application.yml` to your production app on the server.
@@ -8,6 +8,14 @@ Feature: Rails
8
8
  task :hello => :environment do
9
9
  puts ["Hello", ENV["HELLO"]].compact.join(", ") << "!"
10
10
  end
11
+
12
+ task :nena => :environment do
13
+ puts [ENV["LUFTBALLOONS"], "Luftballoons"].compact.join(" ")
14
+ end
15
+
16
+ task :greet => :environment do
17
+ puts ([Figaro.env.greeting] * 3).join(", ") << "!"
18
+ end
11
19
  """
12
20
 
13
21
  Scenario: Has no application.yml
@@ -71,6 +79,14 @@ Feature: Rails
71
79
  When I run "rake hello RAILS_ENV=production"
72
80
  Then the output should be "Hello, users!"
73
81
 
82
+ Scenario: Has application.yml with non-string values
83
+ Given I create "config/application.yml" with:
84
+ """
85
+ LUFTBALLOONS: 99
86
+ """
87
+ When I run "rake nena"
88
+ Then the output should be "99 Luftballoons"
89
+
74
90
  Scenario: Generator creates and ignores application.yml file
75
91
  When I run "rails generate figaro:install"
76
92
  Then "config/application.yml" should exist
@@ -85,3 +101,11 @@ Feature: Rails
85
101
  Scenario: Includes Heroku Rake task
86
102
  When I run "rake --tasks figaro:heroku"
87
103
  Then the output should be "rake figaro:heroku[app] # Configure Heroku according to application.yml"
104
+
105
+ Scenario: Accessing values through the Figaro.env proxy
106
+ Given I create "config/application.yml" with:
107
+ """
108
+ GREETING: Figaro
109
+ """
110
+ When I run "rake greet"
111
+ Then the output should be "Figaro, Figaro, Figaro!"
data/figaro.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "figaro"
5
- gem.version = "0.4.1"
5
+ gem.version = "0.5.0"
6
6
 
7
7
  gem.authors = ["Steve Richert"]
8
8
  gem.email = ["steve.richert@gmail.com"]
data/lib/figaro.rb CHANGED
@@ -1,22 +1,28 @@
1
+ require "figaro/env"
1
2
  require "figaro/railtie"
2
3
 
3
4
  module Figaro
4
5
  extend self
5
6
 
6
- def env
7
- flatten(raw).merge(raw.fetch(environment, {}))
7
+ def vars(custom_environment = nil)
8
+ env(custom_environment).map{|k,v| "#{k}=#{v}" }.sort.join(" ")
9
+ end
10
+
11
+ def env(custom_environment = nil)
12
+ environment = (custom_environment || self.environment).to_s
13
+ Figaro::Env.from(stringify(flatten(raw).merge(raw.fetch(environment, {}))))
8
14
  end
9
15
 
10
16
  def raw
11
- yaml && YAML.load(yaml) || {}
17
+ @raw ||= yaml && YAML.load(yaml) || {}
12
18
  end
13
19
 
14
20
  def yaml
15
- File.exist?(path) ? File.read(path) : nil
21
+ @yaml ||= File.exist?(path) ? File.read(path) : nil
16
22
  end
17
23
 
18
24
  def path
19
- Rails.root.join("config/application.yml")
25
+ @path ||= Rails.root.join("config/application.yml")
20
26
  end
21
27
 
22
28
  def environment
@@ -28,4 +34,8 @@ module Figaro
28
34
  def flatten(hash)
29
35
  hash.reject{|_,v| Hash === v }
30
36
  end
37
+
38
+ def stringify(hash)
39
+ hash.inject({}){|h,(k,v)| h[k.to_s] = v.to_s; h }
40
+ end
31
41
  end
data/lib/figaro/env.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Figaro
2
+ class Env < Hash
3
+ def self.from(hash)
4
+ new.replace(hash)
5
+ end
6
+
7
+ def method_missing(method, *)
8
+ ENV.fetch(method.to_s.upcase){ super }
9
+ end
10
+
11
+ def respond_to?(method)
12
+ ENV.key?(method.to_s.upcase)
13
+ end
14
+ end
15
+ end
@@ -1,9 +1,9 @@
1
1
  namespace :figaro do
2
2
  desc "Configure Heroku according to application.yml"
3
3
  task :heroku, [:app] => :environment do |_, args|
4
- vars = Figaro.env.map{|k,v| "#{k}=#{v}" }.sort.join(" ")
5
- command = "heroku config:add #{vars}"
6
- command << " --app #{args[:app]}" if args[:app]
7
- Kernel.system(command)
4
+ app = args[:app] ? " --app #{args[:app]}" : ""
5
+ rails_env = Kernel.system("heroku config:get RAILS_ENV#{app}").presence
6
+ Rails.env = rails_env if rails_env
7
+ Kernel.system("heroku config:add #{Figaro.vars}#{app}")
8
8
  end
9
9
  end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe Figaro::Env do
4
+ before do
5
+ ENV["HELLO"] = "world"
6
+ end
7
+
8
+ after do
9
+ ENV.delete("HELLO")
10
+ end
11
+
12
+ it "makes ENV values accessible as methods" do
13
+ subject.HELLO.should == "world"
14
+ end
15
+
16
+ it "makes lowercase ENV values accessible as methods" do
17
+ subject.hello.should == "world"
18
+ end
19
+
20
+ it "raises an error if no ENV key matches" do
21
+ expect{ subject.goodbye }.to raise_error(NoMethodError)
22
+ end
23
+ end
@@ -2,16 +2,42 @@ require "spec_helper"
2
2
 
3
3
  describe "Figaro Rake tasks", :rake => true do
4
4
  describe "figaro:heroku" do
5
+ before do
6
+ @original_rails_env = ENV["RAILS_ENV"]
7
+ ENV["RAILS_ENV"] = "development"
8
+ end
9
+
10
+ after do
11
+ ENV["RAILS_ENV"] = @original_rails_env
12
+ Rails.send(:remove_instance_variable, :@_env)
13
+ end
14
+
5
15
  it "configures Heroku" do
6
16
  Figaro.stub(:env => {"HELLO" => "world", "FOO" => "bar"})
17
+ Kernel.stub(:system).with("heroku config:get RAILS_ENV").and_return("development")
7
18
  Kernel.should_receive(:system).once.with("heroku config:add FOO=bar HELLO=world")
8
19
  task.invoke
9
20
  end
10
21
 
11
22
  it "configures a specific Heroku app" do
12
23
  Figaro.stub(:env => {"HELLO" => "world", "FOO" => "bar"})
24
+ Kernel.stub(:system).with("heroku config:get RAILS_ENV --app my-app").and_return("development")
13
25
  Kernel.should_receive(:system).once.with("heroku config:add FOO=bar HELLO=world --app my-app")
14
26
  task.invoke("my-app")
15
27
  end
28
+
29
+ it "respects the Heroku's remote Rails environment" do
30
+ Figaro.stub(:raw => {"development" => {"HELLO" => "developers"}, "production" => {"HELLO" => "world"}})
31
+ Kernel.stub(:system).with("heroku config:get RAILS_ENV").and_return("production")
32
+ Kernel.should_receive(:system).once.with("heroku config:add HELLO=world")
33
+ task.invoke
34
+ end
35
+
36
+ it "defaults to the local Rails environment if not set remotely" do
37
+ Figaro.stub(:raw => {"development" => {"HELLO" => "developers"}, "production" => {"HELLO" => "world"}})
38
+ Kernel.stub(:system).with("heroku config:get RAILS_ENV").and_return("\n")
39
+ Kernel.should_receive(:system).once.with("heroku config:add HELLO=developers")
40
+ task.invoke
41
+ end
16
42
  end
17
43
  end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Figaro do
4
+ describe ".vars" do
5
+ it "dumps and sorts env" do
6
+ Figaro.stub(:raw => {"HELLO" => "world", "FOO" => "bar"})
7
+ Figaro.vars.should == "FOO=bar HELLO=world"
8
+ end
9
+
10
+ it "allows access to a particular environment" do
11
+ Figaro.stub(:raw => {"development" => {"HELLO" => "developers"}, "production" => {"HELLO" => "world"}})
12
+ Figaro.vars(:development).should == "HELLO=developers"
13
+ Figaro.vars(:production).should == "HELLO=world"
14
+ end
15
+ end
16
+
17
+ describe ".env" do
18
+ it "makes ENV values accessible" do
19
+ ENV["HELLO"] = "world"
20
+ Figaro.stub(:raw => {})
21
+ Figaro.env.hello.should == "world"
22
+ ENV.delete("HELLO")
23
+ end
24
+
25
+ it "allows access to a particular environment" do
26
+ Figaro.stub(:raw => {"development" => {"HELLO" => "developers"}, "production" => {"HELLO" => "world"}})
27
+ Figaro.env(:development).should == {"HELLO" => "developers"}
28
+ Figaro.env(:production).should == {"HELLO" => "world"}
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figaro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-25 00:00:00.000000000 Z
12
+ date: 2012-10-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -133,11 +133,14 @@ files:
133
133
  - gemfiles/rails31.gemfile
134
134
  - gemfiles/rails32.gemfile
135
135
  - lib/figaro.rb
136
+ - lib/figaro/env.rb
136
137
  - lib/figaro/railtie.rb
137
138
  - lib/figaro/tasks.rake
138
139
  - lib/generators/figaro/install/install_generator.rb
139
140
  - lib/generators/figaro/install/templates/application.yml
141
+ - spec/figaro/env_spec.rb
140
142
  - spec/figaro/tasks_spec.rb
143
+ - spec/figaro_spec.rb
141
144
  - spec/spec_helper.rb
142
145
  - spec/support/rake.rb
143
146
  homepage: https://github.com/laserlemon/figaro
@@ -152,15 +155,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
155
  - - ! '>='
153
156
  - !ruby/object:Gem::Version
154
157
  version: '0'
158
+ segments:
159
+ - 0
160
+ hash: 3428766527844493391
155
161
  required_rubygems_version: !ruby/object:Gem::Requirement
156
162
  none: false
157
163
  requirements:
158
164
  - - ! '>='
159
165
  - !ruby/object:Gem::Version
160
166
  version: '0'
167
+ segments:
168
+ - 0
169
+ hash: 3428766527844493391
161
170
  requirements: []
162
171
  rubyforge_project:
163
- rubygems_version: 1.8.21
172
+ rubygems_version: 1.8.24
164
173
  signing_key:
165
174
  specification_version: 3
166
175
  summary: Simple Rails app configuration