stephencelis-app 0.1.1

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/History.txt ADDED
@@ -0,0 +1,12 @@
1
+ === 0.1.1 / 2009-04-02
2
+
3
+ * 1 major enhancement
4
+
5
+ * Gemified!
6
+
7
+
8
+ === 0.1.0 / 2009-04-02
9
+
10
+ * 1 major enhancement
11
+
12
+ * Birthday!
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ generators/app_config/app_config_generator.rb
6
+ generators/app_config/templates/app.yml
7
+ init.rb
8
+ install.rb
9
+ lib/app.rb
10
+ test/app_test.rb
11
+ uninstall.rb
data/README.rdoc ADDED
@@ -0,0 +1,95 @@
1
+ = App
2
+
3
+ http://github.com/stephencelis/app
4
+
5
+
6
+ == DESCRIPTION
7
+
8
+ Move the config out of your app, and into App.
9
+
10
+ Sure, it's been done before, and others will do it again, but this is my way,
11
+ and I like it.
12
+
13
+
14
+ == FEATURES/PROBLEMS
15
+
16
+ * Easy, environmentally-friendly configuration access.
17
+
18
+
19
+ For mutability, try acts_as_singleton or KVC:
20
+
21
+ * http://github.com/stephencelis/acts_as_singleton
22
+ * http://github.com/stephencelis/kvc
23
+
24
+
25
+ == SYNOPSIS
26
+
27
+ App looks for and loads configuration from "config/app.yml", providing a
28
+ namespaced API for access.
29
+
30
+ App.config # => {"apis"=>{"flickr"=>{ ... }}
31
+
32
+
33
+ Sugar is always sweeter:
34
+
35
+ App.config("apis", "flickr") # => App.config["apis"]["flickr"]
36
+
37
+
38
+ Who doesn't like sugar?
39
+
40
+ App["apis", "flickr"]
41
+
42
+
43
+ Sugar, sugar, sugar.
44
+
45
+ App.apis("flickr")
46
+
47
+
48
+ Let's not overdo it, though. <tt>App.apis.flickr</tt> just doesn't look right.
49
+
50
+
51
+ == REQUIREMENTS
52
+
53
+ * Rails 2.3.2 or greater.
54
+
55
+
56
+ == INSTALL
57
+
58
+ Install:
59
+
60
+ % script/plugin install git://github.com/stephencelis/app.git
61
+
62
+
63
+ Or submodule:
64
+
65
+ % git submodule add git://github.com/stephencelis/app.git vendor/plugins/app
66
+
67
+
68
+ And generate:
69
+
70
+ % script/generate app_config
71
+
72
+
73
+ == LICENSE
74
+
75
+ (The MIT License)
76
+
77
+ (c) 2009-* Stephen Celis, stephen@stephencelis.com.
78
+
79
+ Permission is hereby granted, free of charge, to any person obtaining a copy
80
+ of this software and associated documentation files (the "Software"), to deal
81
+ in the Software without restriction, including without limitation the rights
82
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
83
+ copies of the Software, and to permit persons to whom the Software is
84
+ furnished to do so, subject to the following conditions:
85
+
86
+ The above copyright notice and this permission notice shall be included in all
87
+ copies or substantial portions of the Software.
88
+
89
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
95
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the app plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the app plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'App'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,7 @@
1
+ class AppConfigGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file "app.yml", "config/app.yml"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ # Your app config, provided by the App plugin.
2
+ #
3
+ # See "vendor/plugins/app/README.txt" for details.
4
+ ---
5
+ development: &development
6
+ loaded_at: <%= Time.zone.now.iso8601 %>
7
+ apis:
8
+ braintree:
9
+ :login: testapi
10
+ :password: password1
11
+
12
+ test:
13
+ <<: *development
14
+
15
+ production:
16
+ loaded_at: <%= Time.zone.now.iso8601 %>
17
+ apis:
18
+ braintree:
19
+ :login: testapi
20
+ :password: password1
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ if !File.exist? Rails.root.join("config", "app.yml")
2
+ puts '** App: "config/app.yml" not found. Run `script/generate app_config`.'
3
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ STDOUT.puts 'Run `script/generate app_config` to generate "config/app.yml".'
data/lib/app.rb ADDED
@@ -0,0 +1,51 @@
1
+ # App is your app.
2
+ #
3
+ # What would your app be without it? Still an app, but without the App.
4
+ module App
5
+ VERSION = "0.1.1"
6
+
7
+ raw_config = File.read Rails.root.join("config", "app.yml")
8
+ @@config = YAML.load(ERB.new(raw_config).result)[Rails.env].freeze
9
+
10
+ class << self
11
+ # Returns the application configuration hash, as defined in
12
+ # "config/app.yml".
13
+ #
14
+ # Follows args through the hash tree. E.g.:
15
+ #
16
+ # App.config("apis", "flickr") # => config_hash["apis"]["flickr"]
17
+ #
18
+ # <tt>App.config</tt> is aliased to <tt>App.[]</tt>, so shorten things up:
19
+ #
20
+ # App["apis", "flickr"]
21
+ #
22
+ # Or rely on +method_missing+ to make it even shorter (and sweeter):
23
+ #
24
+ # App.apis("flickr")
25
+ def config(*args)
26
+ @@config if args.empty?
27
+ args.inject(@@config) { |config, arg| config[arg] }
28
+ end
29
+
30
+ alias [] config
31
+ alias __name__ name
32
+
33
+ # Returns the name of the web application, which can be overridden in
34
+ # "config/app.yml".
35
+ #
36
+ # To return the name of the module, use <tt>App.__name__</tt>.
37
+ def name
38
+ @@name ||= method_missing(:name) || File.basename(Rails.root)
39
+ end
40
+
41
+ def inspect
42
+ "#<App: #{config.inspect}>"
43
+ end
44
+
45
+ private
46
+
47
+ def method_missing(method, *args)
48
+ self[method.to_s, *args] || self[method, *args]
49
+ end
50
+ end
51
+ end
data/test/app_test.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_support'
4
+ require 'active_support/test_case'
5
+ require 'mocha'
6
+ require 'erb'
7
+
8
+ # Mock
9
+ module App
10
+ module Rails
11
+ def self.root
12
+ File
13
+ end
14
+ def self.env
15
+ "development"
16
+ end
17
+ end
18
+ end
19
+
20
+ File.stubs(:read).returns <<-YAML
21
+ ---
22
+ development:
23
+ loaded_at: <%= Time.now.iso8601 %>
24
+ username: Stephen
25
+ password: frobozz
26
+ apis:
27
+ braintree:
28
+ :login: testapi
29
+ :password: password1
30
+ YAML
31
+
32
+ require 'app'
33
+
34
+ class AppTest < ActiveSupport::TestCase
35
+ test "different ways of access should return same values" do
36
+ assert_equal "Stephen", App.config["username"]
37
+ assert_equal "Stephen", App.config("username")
38
+ assert_equal "Stephen", App["username"]
39
+ assert_equal "Stephen", App.username
40
+
41
+ assert_equal "testapi", App.config["apis"]["braintree"][:login]
42
+ assert_equal "testapi", App.config("apis", "braintree", :login)
43
+ assert_equal "testapi", App["apis", "braintree", :login]
44
+ assert_equal "testapi", App.apis("braintree", :login)
45
+ end
46
+
47
+ test "ERB should be parsed" do
48
+ assert_instance_of Time, App.loaded_at
49
+ end
50
+
51
+ test "App.name should be inferred" do
52
+ File.stubs(:basename).returns "root"
53
+ assert_equal "root", App.name
54
+ end
55
+ end
data/uninstall.rb ADDED
@@ -0,0 +1,7 @@
1
+ app_yaml = Rails.root.join("config", "app.yml")
2
+ if File.exist? app_yaml
3
+ print 'Also remove "config/app.yml"? [yN] '
4
+ if STDIN.gets.chomp =~ /^y)/i
5
+ File.delete app_yaml
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stephencelis-app
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.11.0
24
+ version:
25
+ description: Move the config out of your app, and into App. Sure, it's been done before, and others will do it again, but this is my way, and I like it.
26
+ email:
27
+ - stephen@stephencelis.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - generators/app_config/app_config_generator.rb
41
+ - generators/app_config/templates/app.yml
42
+ - init.rb
43
+ - install.rb
44
+ - lib/app.rb
45
+ - test/app_test.rb
46
+ - uninstall.rb
47
+ has_rdoc: true
48
+ homepage:
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --main
52
+ - README.txt
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: app
70
+ rubygems_version: 1.2.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: Move the config out of your app, and into App
74
+ test_files: []
75
+