jcnetdev-app_config 1.2 → 1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
File without changes
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'app_config'
3
- s.version = '1.2'
4
- s.date = '2008-07-24'
3
+ s.version = '1.5'
4
+ s.date = '2008-11-18'
5
5
 
6
6
  s.summary = "Application level configuration"
7
7
  s.description = "Allow application wide configuration settings via YML files"
@@ -13,14 +13,14 @@ Gem::Specification.new do |s|
13
13
  s.has_rdoc = true
14
14
  s.rdoc_options = ["--main", "README"]
15
15
  s.extra_rdoc_files = ["README"]
16
-
17
- s.add_dependency 'rails', ['>= 2.1']
18
16
 
19
17
  s.files = ["README",
20
18
  "README.rdoc",
21
19
  "app_config.gemspec",
22
20
  "init.rb",
23
21
  "lib/app_config.rb",
22
+ "lib/application_config/config_builder.rb",
23
+ "lib/application_config/view_helpers.rb",
24
24
  "rails/init.rb"]
25
25
 
26
26
  s.test_files = ["test/app_config.yml",
@@ -0,0 +1,132 @@
1
+ require 'ostruct'
2
+ require 'yaml'
3
+ require 'erb'
4
+
5
+ module ApplicationConfig
6
+ # == Summary
7
+ # This is API documentation, NOT documentation on how to use this plugin. For that, see the README.
8
+ class ConfigBuilder
9
+ @@load_paths = []
10
+ @@expand_keys = []
11
+ @@root_path = ""
12
+
13
+ # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
14
+ # if the first file if they exist in the first file.
15
+ def self.load_files(options = {})
16
+ config = OpenStruct.new
17
+
18
+ @@load_paths = [options[:paths]].flatten.compact.uniq
19
+ @@expand_keys = [options[:expand_keys]].flatten.compact.uniq
20
+ @@root_path = options[:root_path]
21
+
22
+ # add singleton method to our AppConfig that reloads its settings from the load_paths options
23
+ def config.reload!
24
+ conf = {}
25
+ ConfigBuilder.load_paths.to_a.each do |path|
26
+ file_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
27
+ conf.merge!(file_conf) if file_conf
28
+ end
29
+
30
+ # expand the javascripts config to handle *.* paths
31
+ ConfigBuilder.expand_keys.to_a.each do |expand_path|
32
+ expand_path = expand_path.to_s
33
+ if conf[expand_path]
34
+ conf[expand_path] = ApplicationConfig::ConfigBuilder.expand(conf[expand_path], "#{ConfigBuilder.root_path}/public/#{expand_path}")
35
+ end
36
+ end
37
+
38
+ # load all the new values into the openstruct
39
+ marshal_load(ApplicationConfig::ConfigBuilder.convert(conf).marshal_dump)
40
+
41
+ return self
42
+ end
43
+
44
+ config.reload!
45
+ return config
46
+ end
47
+
48
+ def self.load_paths
49
+ @@load_paths
50
+ end
51
+
52
+ def self.expand_keys
53
+ @@expand_keys
54
+ end
55
+
56
+ def self.root_path
57
+ @@root_path
58
+ end
59
+
60
+ # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
61
+ def self.convert(h) #:nodoc:
62
+ s = OpenStruct.new
63
+ h.each do |k, v|
64
+ s.new_ostruct_member(k)
65
+ if v.is_a?(Hash)
66
+ s.send( (k+'=').to_sym, convert(v))
67
+ elsif v.is_a?(Array)
68
+ converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
69
+ s.send("#{k}=".to_sym, converted_array)
70
+ else
71
+ s.send("#{k}=".to_sym, v)
72
+ end
73
+ end
74
+ s
75
+ end
76
+
77
+ # expand a config val
78
+ def self.expand(config, base_path)
79
+ case config.class.to_s
80
+ when "Hash"
81
+ return expand_hash(config, base_path)
82
+ when "Array"
83
+ return expand_array(config, base_path)
84
+ when "String"
85
+ return expand_string(config, base_path)
86
+ end
87
+ return config
88
+ end
89
+
90
+ # expand a string and returns a list
91
+ def self.expand_string(config, base_path)
92
+ # puts "Expanding String: #{config.inspect}"
93
+ if config.include?("*")
94
+ results = Dir["#{base_path}/#{config}"].map{|i| i.to_s.gsub("#{base_path}/", "") }
95
+
96
+ # puts "EXPANDED PATH: #{base_path}/#{config}"
97
+ # puts results.inspect
98
+ return results
99
+ else
100
+ return config
101
+ end
102
+ end
103
+
104
+ # expand a hash by cycling throw all the hash values
105
+ def self.expand_hash(config, base_path)
106
+ # puts "Expanding Hash: #{config.inspect}"
107
+ new_config = {}
108
+ config.each do |key, val|
109
+ new_config[key] = expand(val, base_path)
110
+ end
111
+ return new_config
112
+ end
113
+
114
+ # expand an array by cycling through all the values
115
+ def self.expand_array(config, base_path)
116
+ # puts "Expanding Array: #{config.inspect}"
117
+ new_config = []
118
+ config.each do |val|
119
+ new_val = expand(val, base_path)
120
+ if new_val.is_a?(Array)
121
+ new_val.each do |inner|
122
+ new_config << inner
123
+ end
124
+ else
125
+ new_config << new_val
126
+ end
127
+ end
128
+ return new_config.uniq
129
+ end
130
+
131
+ end
132
+ end
@@ -0,0 +1,67 @@
1
+ module ApplicationConfig
2
+ module ViewHelpers
3
+ def javascripts_from_config(options = {})
4
+ html = ""
5
+ if defined?(AppConfig) and AppConfig.javascripts
6
+ AppConfig.javascripts.each do |javascript|
7
+ if javascript.is_a?(OpenStruct)
8
+ javascript = javascript.marshal_dump
9
+ end
10
+
11
+ if javascript.is_a? Hash
12
+ javascript.each do |key, val|
13
+ args = [val].flatten
14
+ if defined?(Merb)
15
+ args << options.merge(:bundle => key.to_sym)
16
+ html << js_include_tag(*args)
17
+ elsif defined?(Rails)
18
+ args << options.merge(:cache => key.to_s)
19
+ html << javascript_include_tag(*args)
20
+ end
21
+ html << "\n"
22
+ end
23
+ else
24
+ args = [javascript].flatten
25
+ args << options
26
+ if defined?(Merb)
27
+ html << js_include_tag(*args)
28
+ elsif defined?(Rails)
29
+ html << javascript_include_tag(*args)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ return html
35
+ end
36
+
37
+ def stylesheets_from_config(options = {})
38
+ html = ""
39
+ if defined?(AppConfig) and AppConfig.stylesheets
40
+ AppConfig.stylesheets.each do |stylesheet|
41
+ if stylesheet.is_a?(OpenStruct)
42
+ stylesheet = stylesheet.marshal_dump
43
+ end
44
+
45
+ if stylesheet.is_a? Hash
46
+ stylesheet.each do |key, val|
47
+ args = [val].flatten
48
+ if defined?(Merb)
49
+ args << options.merge(:bundle => key.to_sym)
50
+ html << css_include_tag(*args)
51
+ elsif defined?(Rails)
52
+ args << options.merge(:cache => key.to_s)
53
+ html << stylesheet_link_tag(*args)
54
+ end
55
+ end
56
+ else
57
+ args = [stylesheet].flatten
58
+ args << options
59
+ html << stylesheet_link_tag(*args)
60
+ end
61
+ html << "\n"
62
+ end
63
+ end
64
+ return html
65
+ end
66
+ end
67
+ end
@@ -1,9 +1,18 @@
1
- require 'app_config'
1
+ require 'application_config/config_builder'
2
+ require 'application_config/view_helpers'
2
3
 
3
- ::AppConfig = ApplicationConfig.load_files(RAILS_ROOT+"/config/app_config.yml",
4
- RAILS_ROOT+"/config/environments/#{RAILS_ENV}.yml")
4
+ ::AppConfig = ApplicationConfig::ConfigBuilder.load_files(
5
+ :paths => [
6
+ "#{Rails.root}/config/app_config.yml",
7
+ "#{Rails.root}/config/app_config/settings.yml",
8
+ "#{Rails.root}/config/app_config/#{Rails.env}.yml",
9
+ "#{Rails.root}/config/environments/#{Rails.env}.yml",
10
+ "#{Rails.root}/config/assets.yml",
11
+ "#{Rails.root}/config/javascripts.yml",
12
+ "#{Rails.root}/config/stylesheets.yml"
13
+ ],
14
+ :expand_keys => [:javascripts, :stylesheets],
15
+ :root_path => Rails.root
16
+ )
5
17
 
6
- def AppConfig.reload!
7
- AppConfig.marshal_load(ApplicationConfig.reload.marshal_dump)
8
- return AppConfig
9
- end
18
+ ActionView::Base.send :include, ApplicationConfig::ViewHelpers
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jcnetdev-app_config
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.2"
4
+ version: "1.5"
5
5
  platform: ruby
6
6
  authors:
7
7
  - RailsJedi
@@ -10,18 +10,10 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-07-24 00:00:00 -07:00
13
+ date: 2008-11-18 00:00:00 -08:00
14
14
  default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: rails
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "2.1"
24
- version:
15
+ dependencies: []
16
+
25
17
  description: Allow application wide configuration settings via YML files
26
18
  email: railsjedi@gmail.com
27
19
  executables: []
@@ -36,6 +28,8 @@ files:
36
28
  - app_config.gemspec
37
29
  - init.rb
38
30
  - lib/app_config.rb
31
+ - lib/application_config/config_builder.rb
32
+ - lib/application_config/view_helpers.rb
39
33
  - rails/init.rb
40
34
  has_rdoc: true
41
35
  homepage: http://github.com/jcnetdev/app_config
@@ -1,47 +0,0 @@
1
- require 'ostruct'
2
- require 'yaml'
3
- require 'erb'
4
-
5
- # == Summary
6
- # This is API documentation, NOT documentation on how to use this plugin. For that, see the README.
7
- class ApplicationConfig
8
-
9
- cattr_accessor :conf_paths
10
-
11
- # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
12
- # if the first file if they exist in the first file.
13
- def self.load_files(*conf_load_paths)
14
- self.conf_paths = []
15
- self.conf_paths += conf_load_paths
16
- self.conf_paths.uniq!
17
-
18
- reload
19
- end
20
-
21
- def self.reload
22
- conf = {}
23
- conf_paths.each do |path|
24
- new_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
25
- conf.merge!(new_conf) if new_conf
26
- end
27
- return convert(conf)
28
- end
29
-
30
- # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
31
- def self.convert(h) #:nodoc:
32
- s = OpenStruct.new
33
- h.each do |k, v|
34
- s.new_ostruct_member(k)
35
- if v.instance_of?(Hash)
36
- s.send( (k+'=').to_sym, convert(v))
37
- elsif v.instance_of?(Array)
38
- converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
39
- s.send( (k+'=').to_sym, converted_array)
40
- else
41
- s.send( (k+'=').to_sym, v)
42
- end
43
- end
44
- s
45
- end
46
-
47
- end
@@ -1,2 +0,0 @@
1
- size: 1
2
- server: google.com
@@ -1,46 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'active_support'
4
- require 'lib/app_config'
5
-
6
- class AppConfigTest < Test::Unit::TestCase
7
-
8
- def test_missing_files
9
- config = ApplicationConfig.load_files('not_here1', 'not_here2')
10
- assert_equal OpenStruct.new, config
11
- end
12
-
13
- def test_empty_files
14
- config = ApplicationConfig.load_files('test/empty1.yml', 'test/empty2.yml')
15
- assert_equal OpenStruct.new, config
16
- end
17
-
18
- def test_common
19
- config = ApplicationConfig.load_files('test/app_config.yml')
20
- assert_equal 1, config.size
21
- assert_equal 'google.com', config.server
22
- end
23
-
24
- def test_environment_override
25
- config = ApplicationConfig.load_files('test/app_config.yml', 'test/development.yml')
26
- assert_equal 2, config.size
27
- assert_equal 'google.com', config.server
28
- end
29
-
30
- def test_nested
31
- config = ApplicationConfig.load_files('test/development.yml')
32
- assert_equal 3, config.section.size
33
- end
34
-
35
- def test_array
36
- config = ApplicationConfig.load_files('test/development.yml')
37
- assert_equal 'yahoo.com', config.section.servers[0].name
38
- assert_equal 'amazon.com', config.section.servers[1].name
39
- end
40
-
41
- def test_erb
42
- config = ApplicationConfig.load_files('test/development.yml')
43
- assert_equal 6, config.computed
44
- end
45
-
46
- end
@@ -1,5 +0,0 @@
1
- size: 2
2
- computed: <%= 1 + 2 + 3 %>
3
- section:
4
- size: 3
5
- servers: [ {name: yahoo.com}, {name: amazon.com} ]
File without changes
File without changes