file_resources_manager 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Branden Giacoletto
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README ADDED
@@ -0,0 +1,31 @@
1
+ # FileResourcesManager helps manage global application file_resources stored in
2
+ # a file by giving the application a central location and
3
+ # consistent interface no matter where the data is stored
4
+
5
+ # FileResourcesManager needs a central place to store it's file, the default
6
+ # is in the ~/.app_file_resources directory, but it will use the
7
+ # APP_SETTINGS environment variable for the directory instead
8
+ # if the directory does not exist it will be created on the
9
+ # first get or set or if you call FileResourcesManager.ensure_main_file_resources_file_exists
10
+
11
+ # Usage:
12
+ # FileResourcesManager.set("business_database",{:adapter => "mysql",:host => "database",...})
13
+ # saves the business database file_resources to the business_database.yml file
14
+ # in the ~/.app_file_resources directory
15
+ # FileResourcesManager.get("business_database")[:adapter] => "mysql
16
+ # retrieves all the file_resources from the file
17
+ #
18
+ # FileResourcesManager.set_file_resources_location("business_database","/var/myapp/values.yml")
19
+ # sets the location for future file_resources to the file "values.yml" at /var/myapp
20
+ #
21
+ #
22
+ # Full Example:
23
+ # require 'rubygems' #if not in path and ruby version before 1.9.*
24
+ # require 'file_resources_manager'
25
+ # FileResourcesManager.set_file_resources_location("my_login","~/.login_info")
26
+ # FileResourcesManager.set("my_login",{ :username => "JockOfCode",:password => "1235711"})
27
+ # ...
28
+ # #later in program, or in another program...
29
+ # info = FileResourcesManager.get("my_login")
30
+ # SomeWebbyThingie.login(info[:username],info[:password])
31
+
@@ -0,0 +1,121 @@
1
+ # FileResourcesManager helps manage global application file_resources stored in
2
+ # a file by giving the application a central location and
3
+ # consistent interface no matter where the data is stored
4
+
5
+ # FileResourcesManager needs a central place to store it's file, the default
6
+ # is in the ~/.app_file_resources directory, but it will use the
7
+ # APP_SETTINGS environment variable for the directory instead
8
+ # if the directory does not exist it will be created on the
9
+ # first get or set or if you call FileResourcesManager.ensure_main_file_resources_file_exists
10
+
11
+ # Usage:
12
+ # FileResourcesManager.set("business_database",{:adapter => "mysql",:host => "database",...})
13
+ # saves the business database file_resources to the business_database.yml file
14
+ # in the ~/.app_file_resources directory
15
+ # FileResourcesManager.get("business_database")[:adapter] => "mysql
16
+ # retrieves all the file_resources from the file
17
+ #
18
+ # FileResourcesManager.set_file_resources_location("business_database","/var/myapp/values.yml")
19
+ # sets the location for future file_resources to the file "values.yml" at /var/myapp
20
+ #
21
+ #
22
+ # Full Example:
23
+ # require 'rubygems' #if not in path and ruby version before 1.9.*
24
+ # require 'file_resources_manager'
25
+ # FileResourcesManager.set_file_resources_location("my_login","~/.login_info")
26
+ # FileResourcesManager.set("my_login",{ :username => "JockOfCode",:password => "1235711"})
27
+ # ...
28
+ # #later in program, or in another program...
29
+ # info = FileResourcesManager.get("my_login")
30
+ # SomeWebbyThingie.login(info[:username],info[:password])
31
+
32
+
33
+
34
+ require 'fileutils'
35
+ require 'yaml'
36
+
37
+ class FileResourcesManager
38
+ # Loads the file that is referred to by <file_resources_name>
39
+ def self.get file_resources_name
40
+ ensure_main_file_resources_file_exists
41
+
42
+ file_resources_file = if file_resources_name == "main_file_resources"
43
+ main_file_resources_filename
44
+ else
45
+ # BTW example of a-b-c recursion
46
+ get_file_resources_location(file_resources_name)
47
+ end
48
+
49
+ if File.exists?(file_resources_file)
50
+ return YAML::load(File.read(file_resources_file))
51
+ else
52
+ # a NO_SETTINGS_ERROR could be raised here
53
+ # but I will let the implementer decide on
54
+ # the validation
55
+ return nil
56
+ end
57
+ end
58
+ # Saves the file that is referred to by <file_resources_name>
59
+ def self.set(file_resources_name,file_resources)
60
+ ensure_main_file_resources_file_exists
61
+
62
+ file_resources_file = if file_resources_name == "main_file_resources"
63
+ main_file_resources_filename
64
+ else
65
+ get_file_resources_location(file_resources_name)
66
+ end
67
+
68
+ File.open(file_resources_file,"w"){|f| f << file_resources.to_yaml }
69
+ return file_resources
70
+ end
71
+ # gets the full path and filename that is loaded when you read <file_resources_name>
72
+ def self.get_file_resources_location(file_resources_name)
73
+ get("main_file_resources")[file_resources_name] || File.join(main_file_resources_file_location, file_resources_name + ".yml")
74
+ end
75
+ # sets the full path and filename that is referred to by <setting_name> to <location>
76
+ def self.set_file_resources_location(file_resources_name,location)
77
+ main_file_resources = get("main_file_resources")
78
+ main_file_resources[file_resources_name] = location
79
+ set("main_file_resources",main_file_resources)
80
+ self
81
+ end
82
+
83
+ class << self
84
+ alias file_resources_location get_file_resources_location
85
+ end
86
+
87
+ def self.file_resources_location=(args)
88
+ set_file_resources_location(*args)
89
+ end
90
+
91
+
92
+ # gets the directory where the main file_resources file is saved
93
+ def self.main_file_resources_file_location
94
+ ENV["APP_SETTING"] || File.join(Dir.home,".app_file_resources")
95
+ end
96
+ # gets the full path and filename of the main file_resources file
97
+ def self.main_file_resources_filename
98
+ File.join(main_file_resources_file_location,".main_file_resources.yaml")
99
+ end
100
+
101
+ # guarantees the directory exists and a file is present at the location the main file_resources file should be at
102
+ def self.ensure_main_file_resources_file_exists
103
+ filename = main_file_resources_filename
104
+ if !File.file?(filename) then
105
+ FileUtils.mkdir_p(File.dirname(filename))
106
+ File.open(filename,"w"){|f| f << {}.to_yaml }
107
+ end
108
+ end
109
+ end
110
+
111
+ #turns out ruby < 1.9 doesn't have Dir::home
112
+ #here is an 80% solution, (Checking for $HOME on a rescue would maybe
113
+ #cover more edge cases, next would be username in a /home or /User
114
+ #directory
115
+ if Dir.respond_to?(:home) == false
116
+ class Dir
117
+ def self.home
118
+ File.expand_path("~/")
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,28 @@
1
+ require 'settings_manager'
2
+ require 'test/unit'
3
+
4
+ class TestSettingsManager < Test::Unit::TestCase
5
+ def test_get
6
+ assert_nil(SettingsManager::get("some_non_existing_settings"),"There is none so it should most definately fail")
7
+ end
8
+ def test_system_settings
9
+ test_directory_location = File.join(Dir.pwd,"settings_location_test")
10
+ if File.directory?(test_directory_location)
11
+ (Dir.entries(test_directory_location) - [".",".."]).each{|f| File.delete(File.join(test_directory_location,f)) }
12
+ Dir.rmdir(test_directory_location)
13
+ end
14
+ ENV.delete "APP_SETTING"
15
+ assert_equal(SettingsManager::main_settings_file_location,File.expand_path("~/.app_settings"),"if none exist then this should be $HOME/.app_settings")
16
+ ENV["APP_SETTING"] = test_directory_location
17
+ assert_equal(SettingsManager::main_settings_file_location,test_directory_location,"Main Setting File Location Should be set to the current directory + settings_location_test")
18
+ SettingsManager::ensure_main_settings_file_exists
19
+ assert_equal(File.exists?(File.join(test_directory_location,".main_settings.yaml")),true,"settings file should have been created")
20
+ assert_equal(SettingsManager::get_settings_location("test_kv"),File.join(test_directory_location,"test_kv.yml"),"test settings file location should match")
21
+ SettingsManager::set("test_kv",{:name => "jockofcode",:occupation => "rubyist" })
22
+ assert_equal(SettingsManager::get("test_kv")[:name],"jockofcode","should have retreived 'jockofcode' from settings")
23
+ SettingsManager::set_settings_location("test_kv",File.join(test_directory_location,"not_the_default_kv.yml"))
24
+ assert_nil(SettingsManager::get("test_kv"),"It should not be getting the value from test_kv.yml anymore")
25
+ SettingsManager::set("test_kv",{:name => "john",:occupation => "kneeder"})
26
+ assert_equal(SettingsManager::get("test_kv")[:name],"john","should now be john, not jockofcode")
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_resources_manager
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "1.0"
6
+ platform: ruby
7
+ authors:
8
+ - Branden Giacoletto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-16 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: yaml
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: Instead of having to constantly use YAML::load(File.read('some/dir/with/file_resources/database_blah.yml')) in every app you write, now you can simply use FileResourcesManager::get('database') to get your file resources and the rest is automagically managed for you!
28
+ email:
29
+ - branden@carbondatacomputers.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - lib/file_resources_manager.rb
38
+ - tests/test_file_resources_manager.rb
39
+ - LICENSE
40
+ - README
41
+ has_rdoc: true
42
+ homepage: http://JockOfCode.com/FileResourcesManager
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.6
62
+ requirements: []
63
+
64
+ rubyforge_project: file_resources_manager
65
+ rubygems_version: 1.6.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Keeps your app file_resources in a central location
69
+ test_files: []
70
+