amberbit-config 1.0.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/MIT-LICENSE +1 -1
  2. data/README.rdoc +89 -0
  3. data/VERSION +1 -1
  4. data/lib/amberbit-config.rb +27 -3
  5. data/lib/amberbit-config/config.rb +60 -0
  6. data/lib/amberbit-config/engine.rb +12 -0
  7. data/lib/amberbit-config/errors.rb +4 -0
  8. data/lib/amberbit-config/hash_struct.rb +59 -0
  9. data/lib/amberbit-config/version.rb +3 -0
  10. data/lib/tasks/amberbit-config.rake +37 -0
  11. metadata +75 -127
  12. data/.gitignore +0 -1
  13. data/README +0 -68
  14. data/Rakefile +0 -36
  15. data/amberbit-config.gemspec +0 -122
  16. data/init.rb +0 -2
  17. data/install.rb +0 -1
  18. data/lib/amber_bit_app_config.rb +0 -74
  19. data/lib/hash_struct.rb +0 -9
  20. data/rails/init.rb +0 -2
  21. data/spec/library_spec.rb +0 -20
  22. data/spec/plugin_spec.rb +0 -31
  23. data/spec/spec.opts +0 -1
  24. data/spec/spec_helper.rb +0 -38
  25. data/spec/testapp/README +0 -243
  26. data/spec/testapp/Rakefile +0 -11
  27. data/spec/testapp/app/controllers/application_controller.rb +0 -10
  28. data/spec/testapp/app/helpers/application_helper.rb +0 -3
  29. data/spec/testapp/config/application/config.yml +0 -8
  30. data/spec/testapp/config/application/default.yml +0 -6
  31. data/spec/testapp/config/boot.rb +0 -110
  32. data/spec/testapp/config/environment.rb +0 -14
  33. data/spec/testapp/config/environments/development.rb +0 -17
  34. data/spec/testapp/config/environments/production.rb +0 -28
  35. data/spec/testapp/config/environments/test.rb +0 -28
  36. data/spec/testapp/config/initializers/backtrace_silencers.rb +0 -7
  37. data/spec/testapp/config/initializers/inflections.rb +0 -10
  38. data/spec/testapp/config/initializers/mime_types.rb +0 -5
  39. data/spec/testapp/config/initializers/new_rails_defaults.rb +0 -19
  40. data/spec/testapp/config/initializers/session_store.rb +0 -15
  41. data/spec/testapp/config/locales/en.yml +0 -5
  42. data/spec/testapp/config/routes.rb +0 -43
  43. data/spec/testapp/db/test.sqlite3 +0 -0
  44. data/spec/testapp/doc/README_FOR_APP +0 -2
  45. data/spec/testapp/log/development.log +0 -0
  46. data/spec/testapp/log/production.log +0 -0
  47. data/spec/testapp/log/server.log +0 -0
  48. data/spec/testapp/log/test.log +0 -0
  49. data/spec/testapp/public/404.html +0 -30
  50. data/spec/testapp/public/422.html +0 -30
  51. data/spec/testapp/public/500.html +0 -30
  52. data/spec/testapp/public/favicon.ico +0 -0
  53. data/spec/testapp/public/images/rails.png +0 -0
  54. data/spec/testapp/public/index.html +0 -275
  55. data/spec/testapp/public/javascripts/application.js +0 -2
  56. data/spec/testapp/public/javascripts/controls.js +0 -963
  57. data/spec/testapp/public/javascripts/dragdrop.js +0 -973
  58. data/spec/testapp/public/javascripts/effects.js +0 -1128
  59. data/spec/testapp/public/javascripts/prototype.js +0 -4320
  60. data/spec/testapp/public/robots.txt +0 -5
  61. data/spec/testapp/script/about +0 -4
  62. data/spec/testapp/script/console +0 -3
  63. data/spec/testapp/script/dbconsole +0 -3
  64. data/spec/testapp/script/destroy +0 -3
  65. data/spec/testapp/script/generate +0 -3
  66. data/spec/testapp/script/performance/benchmarker +0 -3
  67. data/spec/testapp/script/performance/profiler +0 -3
  68. data/spec/testapp/script/plugin +0 -3
  69. data/spec/testapp/script/runner +0 -3
  70. data/spec/testapp/script/server +0 -3
  71. data/spec/testapp/test/performance/browsing_test.rb +0 -9
  72. data/spec/testapp/test/test_helper.rb +0 -38
  73. data/tasks/amberbit_app_config_tasks.rake +0 -79
  74. data/uninstall.rb +0 -1
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 [name of plugin creator]
1
+ Copyright 2011 Wojciech Piekutowski, Hubert Łępicki, Piotr Tynecki
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ AmberBit Config
2
+ ===============
3
+
4
+ Provides simple global configuration for ruby applications using YAML files.
5
+
6
+ Installation:
7
+ =============
8
+
9
+ With bundler:
10
+ ---------------
11
+
12
+ 1. Edit Gemfile, located in your application and add the following line:
13
+ gem 'amberbit-config'
14
+ 2. Run bundle:
15
+ bundle install
16
+ 3. Use rake to generate configuration files:
17
+ rake amberbit:config:setup
18
+ 4. Make sure that ./config/app_config.yml stays in .gitignore
19
+
20
+ As a rails engine:
21
+ ------------------
22
+
23
+ It will work without any other actions as a rails engine. It takes environment name from:
24
+
25
+ ENV['RAILS_ENV'] or ENV['RACK_ENV'] or ENV['APP_CONFIG_ENV']
26
+
27
+ AppConfig should be initialized before environment configuration. If you need to use it within ./config/application.rb then
28
+ add after Bundler.require
29
+
30
+ require 'amberbit-config'
31
+ AmberbitConfig.initialize_within File.dirname(__FILE__)
32
+
33
+ Any ruby application
34
+ --------------------
35
+ For any other app require and initialize AppConfig as above, just make sure that there's an ENV variable. You can also
36
+ specify files to load, i.e.:
37
+
38
+ require 'amberbit-config'
39
+ ENV['RACK_ENV'] = 'development'
40
+ AmberbitConfig.initialize 'config/app_config_default.yml', 'config/app_config.yml'
41
+
42
+ Tested with Ruby on Rails, Sinatra, and plain old ruby.
43
+
44
+ Usage:
45
+ ======
46
+
47
+ Place your application configuration default values into ./config/app_config_default.yml:
48
+
49
+ default:
50
+ application:
51
+ name: SuperApp
52
+ url: http://localhost:3000/
53
+
54
+ test:
55
+ # overwrite application.url key only in test env
56
+ application:
57
+ url: http://testhost:3000/
58
+
59
+ And overwrite values you wish on per-installation basis in ./config/app_config.yml:
60
+
61
+ default:
62
+ application:
63
+ name: SuperApp specially for you
64
+
65
+ production:
66
+ application:
67
+ url: http://yoursuperhiperdomain.com
68
+
69
+ In your application Ruby code you can access the values in 2 following ways:
70
+
71
+ AppConfig["application"]["name"] #=> SuperApp specially for you
72
+
73
+ or
74
+
75
+ AppConfig.application.name #=> SuperApp specially for you
76
+
77
+ Keys are deep-merged, for example:
78
+ production env:
79
+
80
+ AppConfig.application.name #=> SuperApp specially for you
81
+ AppConfig.application.url #=> http://yoursuperhiperdomain.com
82
+
83
+ development env:
84
+
85
+ AppConfig.application.name #=> SuperApp specially for you
86
+ AppConfig.application.url #=> http://localhost:3000/
87
+
88
+
89
+ Copyright (c) 2008/2011 Wojciech Piekutowski, Hubert Łępicki, Piotr Tynecki released under the MIT license
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
@@ -1,3 +1,27 @@
1
- require 'yaml'
2
- require File.join(File.dirname(__FILE__), 'hash_struct')
3
- require File.join(File.dirname(__FILE__), 'amber_bit_app_config')
1
+ require 'amberbit-config/version'
2
+ require 'amberbit-config/errors'
3
+ require 'amberbit-config/config'
4
+ require 'amberbit-config/hash_struct'
5
+ require 'amberbit-config/engine' if defined?(::Rails::Engine)
6
+
7
+ module AmberbitConfig
8
+ # Initialize AppConfig variable with default file path and custom file path if it wasn't set yet.
9
+ def self.initialize(default_file, config_file)
10
+ return unless File.exist?(default_file)
11
+ return if defined?(AppConfig)
12
+
13
+ config = Config.new default_file, config_file
14
+ Object.const_set 'AppConfig', HashStruct.create(config.data)
15
+ end
16
+
17
+ # Run initialze with default file names within root_path
18
+ def self.initialize_within(root_path)
19
+ initialize path_from(root_path, 'app_config_default.yml'), path_from(root_path, 'app_config.yml')
20
+ end
21
+
22
+ # Gets path from root_path and file name, i.e.
23
+ # <tt>path_from File.dirname(__FILE__), 'app_config.yml'</tt>
24
+ def self.path_from(root_path, file)
25
+ File.expand_path File.join(File.dirname(root_path), 'config', file)
26
+ end
27
+ end
@@ -0,0 +1,60 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module AmberbitConfig
5
+ # Class responsible for loading, parsing and merging configuration from yaml files.
6
+ class Config
7
+ attr_reader :default, :custom, :data
8
+
9
+ def initialize(defaults_file, customs_file)
10
+ @default = parse_yaml defaults_file
11
+ @custom = parse_yaml customs_file
12
+
13
+ @data = deep_merge default, custom
14
+ end
15
+
16
+ private
17
+
18
+ # Parse configuration file with ERB so you can set keys like:
19
+ # <tt>redis_url: <%= ENV['REDIS_URL'] %></tt>
20
+ # then loads a hash and merges defaults with current environment
21
+ def parse_yaml(file)
22
+ file_content = File.exist?(file) ? ERB.new(File.read(file)).result : '---'
23
+
24
+ config = YAML.load(file_content) || {}
25
+ env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['APP_CONFIG_ENV']
26
+
27
+ config_from config['default'], config[env]
28
+ end
29
+
30
+ def config_from(default_config, env_config)
31
+ if present?(default_config) && present?(env_config)
32
+ deep_merge default_config, env_config
33
+ else
34
+ env_config || default_config || {}
35
+ end
36
+ end
37
+
38
+ def deep_merge(origin, from)
39
+ origin ||= {}
40
+ from ||= {}
41
+ deep_merge!(origin.dup, from)
42
+ end
43
+
44
+ def deep_merge!(origin, from)
45
+ from.each do |key, value|
46
+ origin[key] = value.is_a?(Hash) ? deep_merge(origin[key], value) : value
47
+ end
48
+
49
+ origin
50
+ end
51
+
52
+ def present?(object)
53
+ !blank?(object)
54
+ end
55
+
56
+ def blank?(object)
57
+ object.respond_to?(:empty?) ? object.empty? : !object
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,12 @@
1
+ require 'amberbit-config'
2
+
3
+ module AmberbitConfig
4
+ class Engine < ::Rails::Engine
5
+ engine_name :amberbit_config
6
+
7
+ # Try to initialize AppConfig as soon as possible
8
+ initializer 'amberbit_config.initialize_config', before: :load_environment_config do |app|
9
+ AmberbitConfig.initialize_within app.root.join('config')
10
+ end
11
+ end if defined?(::Rails)
12
+ end
@@ -0,0 +1,4 @@
1
+ module AmberbitConfig
2
+ class HashArgumentError < ::ArgumentError; end
3
+ class ConfigNotSetError < ::NoMethodError; end
4
+ end
@@ -0,0 +1,59 @@
1
+ require 'ostruct'
2
+
3
+ module AmberbitConfig
4
+ # Main class that holds whole configuration, acts as open struct, with few tune ups
5
+ class HashStruct < ::OpenStruct
6
+ # Initialize with check for conflicts within hash keys
7
+ def initialize(hash = nil)
8
+ check_hash_for_conflicts hash if hash
9
+ super
10
+ end
11
+
12
+ # Adds access to existing keys through hash operator
13
+ def [](key)
14
+ self.send key unless key == nil
15
+ end
16
+
17
+ # Generates a nested Hash object which is a copy of existing configuration
18
+ def to_hash
19
+ _copy = {}
20
+ @table.each { |key, value| _copy[key] = value.is_a?(HashStruct) ? value.to_hash : value }
21
+ _copy
22
+ end
23
+
24
+ # Raise an error if method/configuration isn't set yet. It should prevent typos, because normally it will just
25
+ # return a nil. With this check you can spot those earlier.
26
+ def method_missing(method, *args, &block)
27
+ if method =~ /=\z/ || self.respond_to?(method)
28
+ super
29
+ else
30
+ raise ConfigNotSetError, "Configuration option: '#{method}' was not set"
31
+ end
32
+ end
33
+
34
+ # Creates a deeply nested HashStruct from hash.
35
+ def self.create(object)
36
+ case object
37
+ when Array
38
+ object.map { |item| create(item) }
39
+ when Hash
40
+ mapped = {}
41
+ object.each { |key, value| mapped[key] = create(value) }
42
+ HashStruct.new mapped
43
+ else
44
+ object
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # Checks if provided option is a hash and if the keys are not in confict with OpenStruct public methods.
51
+ def check_hash_for_conflicts(hash)
52
+ raise HashArgumentError, 'It must be a hash' unless hash.is_a?(Hash)
53
+
54
+ unless (conflicts = self.public_methods & hash.keys.map(&:to_sym)).empty?
55
+ raise HashArgumentError, "Rename keys in order to avoid conflicts with internal calls: #{conflicts.join(', ')}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module AmberbitConfig
2
+ VERSION = '1.1.0'
3
+ end
@@ -0,0 +1,37 @@
1
+ if defined?(Rails)
2
+ namespace :amberbit do
3
+ namespace :config do
4
+
5
+ desc 'Creates config/app_config_default.yml and config/app_config.yml files'
6
+ task :setup => :environment do
7
+ if File.directory? Rails.root.join('config')
8
+ puts 'Directory config already exists, skipping.'
9
+ elsif !File.exists?(Rails.root.join('config'))
10
+ Dir.mkdir Rails.root.join('config')
11
+ puts 'Directory config was created.'
12
+ else
13
+ puts 'AmberBit Config requires config to be a directory and it appears to be a regular file!'
14
+ puts 'Cannot continue, stopping!'
15
+ exit(-1)
16
+ end
17
+
18
+ %w(app_config_default app_config).each do |fn|
19
+ if File.exists? Rails.root.join('config', "#{fn}.yml")
20
+ puts "File config/#{fn}.yml already exists, skipping."
21
+ else
22
+ File.open Rails.root.join('config', "#{fn}.yml"), 'w+:utf-8' do |file|
23
+ file.write "---\ndefault:\n\ndevelopment:\n\ntest:\n\nstaging:\n\nproduction:"
24
+ end
25
+
26
+ puts "File config/#{fn}.yml was created."
27
+ end
28
+ end
29
+
30
+ if (gitignore = Rails.root.join('.gitignore')) && File.exists?(gitignore)
31
+ File.open(gitignore, 'a') { |f| f << "\nconfig/app_config.yml" }
32
+ puts 'Added entry in .gitignore'
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,150 +1,98 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: amberbit-config
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 0
9
- - 2
10
- version: 1.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Wojciech Piekutowski
14
9
  - Hubert Lepicki
10
+ - Piotr Tynecki
11
+ - Leszek Zalewski
15
12
  autorequire:
16
13
  bindir: bin
17
14
  cert_chain: []
18
-
19
- date: 2010-08-22 00:00:00 +02:00
20
- default_executable:
21
- dependencies: []
22
-
23
- description: Reads YAML files with configuration. Allows you to specify default configuration file you can store in repository and overwrite it with custom configuration file for each application instance and environment.
15
+ date: 2013-02-26 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rspec
19
+ requirement: !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec-rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ description: Reads YAML files with configuration. Allows you to specify default configuration
50
+ file you can store in repository and overwrite it with custom configuration file
51
+ for each application instance and environment.
24
52
  email: hubert.lepicki@amberbit.com
25
53
  executables: []
26
-
27
54
  extensions: []
28
-
29
- extra_rdoc_files:
30
- - README
31
- files:
32
- - .gitignore
55
+ extra_rdoc_files: []
56
+ files:
57
+ - lib/amberbit-config/version.rb
58
+ - lib/amberbit-config/hash_struct.rb
59
+ - lib/amberbit-config/engine.rb
60
+ - lib/amberbit-config/errors.rb
61
+ - lib/amberbit-config/config.rb
62
+ - lib/amberbit-config.rb
63
+ - lib/tasks/amberbit-config.rake
33
64
  - MIT-LICENSE
34
- - README
35
- - Rakefile
65
+ - README.rdoc
36
66
  - VERSION
37
- - amberbit-config.gemspec
38
- - init.rb
39
- - install.rb
40
- - lib/amber_bit_app_config.rb
41
- - lib/amberbit-config.rb
42
- - lib/hash_struct.rb
43
- - rails/init.rb
44
- - spec/library_spec.rb
45
- - spec/plugin_spec.rb
46
- - spec/spec.opts
47
- - spec/spec_helper.rb
48
- - spec/testapp/README
49
- - spec/testapp/Rakefile
50
- - spec/testapp/app/controllers/application_controller.rb
51
- - spec/testapp/app/helpers/application_helper.rb
52
- - spec/testapp/config/application/config.yml
53
- - spec/testapp/config/application/default.yml
54
- - spec/testapp/config/boot.rb
55
- - spec/testapp/config/environment.rb
56
- - spec/testapp/config/environments/development.rb
57
- - spec/testapp/config/environments/production.rb
58
- - spec/testapp/config/environments/test.rb
59
- - spec/testapp/config/initializers/backtrace_silencers.rb
60
- - spec/testapp/config/initializers/inflections.rb
61
- - spec/testapp/config/initializers/mime_types.rb
62
- - spec/testapp/config/initializers/new_rails_defaults.rb
63
- - spec/testapp/config/initializers/session_store.rb
64
- - spec/testapp/config/locales/en.yml
65
- - spec/testapp/config/routes.rb
66
- - spec/testapp/db/test.sqlite3
67
- - spec/testapp/doc/README_FOR_APP
68
- - spec/testapp/log/development.log
69
- - spec/testapp/log/production.log
70
- - spec/testapp/log/server.log
71
- - spec/testapp/log/test.log
72
- - spec/testapp/public/404.html
73
- - spec/testapp/public/422.html
74
- - spec/testapp/public/500.html
75
- - spec/testapp/public/favicon.ico
76
- - spec/testapp/public/images/rails.png
77
- - spec/testapp/public/index.html
78
- - spec/testapp/public/javascripts/application.js
79
- - spec/testapp/public/javascripts/controls.js
80
- - spec/testapp/public/javascripts/dragdrop.js
81
- - spec/testapp/public/javascripts/effects.js
82
- - spec/testapp/public/javascripts/prototype.js
83
- - spec/testapp/public/robots.txt
84
- - spec/testapp/script/about
85
- - spec/testapp/script/console
86
- - spec/testapp/script/dbconsole
87
- - spec/testapp/script/destroy
88
- - spec/testapp/script/generate
89
- - spec/testapp/script/performance/benchmarker
90
- - spec/testapp/script/performance/profiler
91
- - spec/testapp/script/plugin
92
- - spec/testapp/script/runner
93
- - spec/testapp/script/server
94
- - spec/testapp/test/performance/browsing_test.rb
95
- - spec/testapp/test/test_helper.rb
96
- - tasks/amberbit_app_config_tasks.rake
97
- - uninstall.rb
98
- has_rdoc: true
99
67
  homepage: http://github.com/amberbit/amberbit-config
100
68
  licenses: []
101
-
102
69
  post_install_message:
103
- rdoc_options:
104
- - --charset=UTF-8
105
- require_paths:
106
- - lib
107
- required_ruby_version: !ruby/object:Gem::Requirement
70
+ rdoc_options: []
71
+ require_paths:
72
+ - - .
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
108
75
  none: false
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- hash: 3
113
- segments:
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ segments:
114
81
  - 0
115
- version: "0"
116
- required_rubygems_version: !ruby/object:Gem::Requirement
82
+ hash: -2357817230340208494
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
84
  none: false
118
- requirements:
119
- - - ">="
120
- - !ruby/object:Gem::Version
121
- hash: 3
122
- segments:
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
123
90
  - 0
124
- version: "0"
91
+ hash: -2357817230340208494
125
92
  requirements: []
126
-
127
93
  rubyforge_project:
128
- rubygems_version: 1.3.7
94
+ rubygems_version: 1.8.24
129
95
  signing_key:
130
96
  specification_version: 3
131
- summary: Yet Another AppConfig for Rails but not only
132
- test_files:
133
- - spec/testapp/config/boot.rb
134
- - spec/testapp/config/environment.rb
135
- - spec/testapp/config/environments/development.rb
136
- - spec/testapp/config/environments/production.rb
137
- - spec/testapp/config/environments/test.rb
138
- - spec/testapp/config/initializers/backtrace_silencers.rb
139
- - spec/testapp/config/initializers/mime_types.rb
140
- - spec/testapp/config/initializers/new_rails_defaults.rb
141
- - spec/testapp/config/initializers/session_store.rb
142
- - spec/testapp/config/initializers/inflections.rb
143
- - spec/testapp/config/routes.rb
144
- - spec/testapp/test/test_helper.rb
145
- - spec/testapp/test/performance/browsing_test.rb
146
- - spec/testapp/app/controllers/application_controller.rb
147
- - spec/testapp/app/helpers/application_helper.rb
148
- - spec/library_spec.rb
149
- - spec/plugin_spec.rb
150
- - spec/spec_helper.rb
97
+ summary: Yet Another AppConfig for Rails but not only.
98
+ test_files: []