file_resources_manager 1.0 → 1.0.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.
@@ -9,10 +9,10 @@
9
9
  # first get or set or if you call FileResourcesManager.ensure_main_file_resources_file_exists
10
10
 
11
11
  # Usage:
12
- # FileResourcesManager.set("business_database",{:adapter => "mysql",:host => "database",...})
12
+ # FileResourcesManager.set("business_database",{:adapter => "mysql",:host => "database",...}.to_yaml)
13
13
  # saves the business database file_resources to the business_database.yml file
14
14
  # in the ~/.app_file_resources directory
15
- # FileResourcesManager.get("business_database")[:adapter] => "mysql
15
+ # YAML.load(FileResourcesManager.get("business_database"))[:adapter] => "mysql
16
16
  # retrieves all the file_resources from the file
17
17
  #
18
18
  # FileResourcesManager.set_file_resources_location("business_database","/var/myapp/values.yml")
@@ -23,10 +23,10 @@
23
23
  # require 'rubygems' #if not in path and ruby version before 1.9.*
24
24
  # require 'file_resources_manager'
25
25
  # FileResourcesManager.set_file_resources_location("my_login","~/.login_info")
26
- # FileResourcesManager.set("my_login",{ :username => "JockOfCode",:password => "1235711"})
26
+ # FileResourcesManager.set("my_login",{ :username => "JockOfCode",:password => "1235711"}.to_yaml)
27
27
  # ...
28
28
  # #later in program, or in another program...
29
- # info = FileResourcesManager.get("my_login")
29
+ # info = YAML::load(FileResourcesManager.get("my_login"))
30
30
  # SomeWebbyThingie.login(info[:username],info[:password])
31
31
 
32
32
 
@@ -47,7 +47,7 @@ class FileResourcesManager
47
47
  end
48
48
 
49
49
  if File.exists?(file_resources_file)
50
- return YAML::load(File.read(file_resources_file))
50
+ return File.read(file_resources_file)
51
51
  else
52
52
  # a NO_SETTINGS_ERROR could be raised here
53
53
  # but I will let the implementer decide on
@@ -65,18 +65,18 @@ class FileResourcesManager
65
65
  get_file_resources_location(file_resources_name)
66
66
  end
67
67
 
68
- File.open(file_resources_file,"w"){|f| f << file_resources.to_yaml }
68
+ File.open(file_resources_file,"w"){|f| f << file_resources }
69
69
  return file_resources
70
70
  end
71
71
  # gets the full path and filename that is loaded when you read <file_resources_name>
72
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")
73
+ YAML::load(get("main_file_resources"))[file_resources_name] || File.join(main_file_resources_file_location, file_resources_name)
74
74
  end
75
75
  # sets the full path and filename that is referred to by <setting_name> to <location>
76
76
  def self.set_file_resources_location(file_resources_name,location)
77
- main_file_resources = get("main_file_resources")
77
+ main_file_resources = YAML::load(get("main_file_resources"))
78
78
  main_file_resources[file_resources_name] = location
79
- set("main_file_resources",main_file_resources)
79
+ set("main_file_resources",main_file_resources.to_yaml)
80
80
  self
81
81
  end
82
82
 
@@ -95,7 +95,7 @@ class FileResourcesManager
95
95
  end
96
96
  # gets the full path and filename of the main file_resources file
97
97
  def self.main_file_resources_filename
98
- File.join(main_file_resources_file_location,".main_file_resources.yaml")
98
+ File.join(main_file_resources_file_location,".main_file_resources.yml")
99
99
  end
100
100
 
101
101
  # guarantees the directory exists and a file is present at the location the main file_resources file should be at
@@ -0,0 +1,87 @@
1
+ require 'file_resources_manager'
2
+
3
+ class SettingsManager
4
+ attr_accessor :name,:file_type,:file_location,:filename,:data
5
+
6
+ def initialize(settings_name)
7
+ @name = settings_name
8
+ @data,@file_type,@file_location,@filename = nil
9
+ load_data
10
+ end
11
+
12
+ def load_data
13
+ smd = SettingsManager.settings_manager_data[@name]
14
+ @file_location = Dir.pwd
15
+ @filename = @name
16
+ @file_type = :yaml
17
+ if smd then
18
+ @file_type = smd["file_type"].to_sym
19
+ @file_location = smd["file_location"] || Dir.pwd
20
+ @filename = smd["filename"] || @name
21
+
22
+ data = File.read(File.join(@file_location,@filename))
23
+ @data=self.send("decode_" + @file_type.to_s,data)
24
+ else
25
+ @data={}
26
+ end
27
+ self
28
+ end
29
+
30
+ def save_data
31
+ File.open(File.join(@file_location,@filename),"w"){|f|
32
+ case @file_type
33
+ when nil
34
+ @file_type = :yaml
35
+ f << @data.to_yaml
36
+ else
37
+ f << self.send("encode_" + @file_type.to_s,data)
38
+ end
39
+ }
40
+ file_data = {}
41
+ file_data["file_type"] = @file_type.to_s if @file_type
42
+ file_data["file_location"] = @file_location if @file_location
43
+ file_data["filename"] = @filename if @filename
44
+ smd = SettingsManager.settings_manager_data
45
+ smd[@name] = file_data
46
+ SettingsManager.settings_manager_data=smd
47
+ end
48
+ def self.create_encoder(datatype,&encode_method)
49
+ define_method("encode_" + datatype.to_s,&encode_method)
50
+ end
51
+
52
+ def self.create_decoder(datatype,&encode_method)
53
+ define_method("decode_" + datatype.to_s,&encode_method)
54
+ end
55
+
56
+ def []=(key,value)
57
+ @data[key]=value
58
+ end
59
+
60
+ def [](key)
61
+ @data[key]
62
+ end
63
+
64
+ def self.settings(settings_name)
65
+ self.new(settings_name)
66
+ end
67
+
68
+ def self.settings_manager_data
69
+ YAML::load(FileResourcesManager::get(".settings")||{}.to_yaml)
70
+ end
71
+
72
+ def self.settings_manager_data=(data)
73
+ FileResourcesManager::set(".settings",data.to_yaml)
74
+ self
75
+ end
76
+ end
77
+
78
+ SettingsManager.create_encoder(:yaml){|data| data.to_yaml }
79
+ SettingsManager.create_decoder(:yaml){|data| YAML::load(data) }
80
+
81
+ # I think my encoder for csv is wonked, will fix someother time, dinner time!!
82
+ SettingsManager.create_encoder(:csv){|data| lines = []; data.each{|k,v| lines << [k,v].join(",") }; lines.join("\n") }
83
+ SettingsManager.create_decoder(:csv){|data| data.split.inject({}){|hash,line| hash[line.split(",").first] = line.split(",").last } }
84
+
85
+
86
+ SettingsManager.create_encoder(:csv){|data| data.to_yaml }
87
+ SettingsManager.create_decoder(:csv){|data| YAML::load(data) }
@@ -1,28 +1,39 @@
1
- require 'settings_manager'
2
- require 'test/unit'
1
+ require 'file_resources_manager'
2
+ require 'minitest/unit'
3
+
4
+ class TestFileResourcesManager < Test::Unit::TestCase
5
+ @@test_directory_location = File.join(Dir.pwd,"file_resources_location_test")
3
6
 
4
- class TestSettingsManager < Test::Unit::TestCase
5
7
  def test_get
6
- assert_nil(SettingsManager::get("some_non_existing_settings"),"There is none so it should most definately fail")
8
+ assert_nil(FileResourcesManager::get("some_non_existing_file_resources"),"There is none so it should most definately fail")
7
9
  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)
10
+ def remove_test_directory
11
+ if File.directory?(@@test_directory_location)
12
+ (Dir.entries(@@test_directory_location) - [".",".."]).each{|f| File.delete(File.join(@@test_directory_location,f)) }
13
+ Dir.rmdir(@@test_directory_location)
13
14
  end
15
+ end
16
+ def setup
17
+ remove_test_directory
18
+ end
19
+ def teardown
20
+ remove_test_directory
21
+ end
22
+
23
+ def test_system_file_resources
14
24
  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")
25
+ assert_equal(FileResourcesManager::main_file_resources_file_location,File.expand_path("~/.app_file_resources"),"if none exist then this should be $HOME/.app_file_resources")
26
+ ENV["APP_SETTING"] = @@test_directory_location
27
+ assert_equal(FileResourcesManager::main_file_resources_file_location,@@test_directory_location,"Main Setting File Location Should be set to the current directory + file_resources_location_test")
28
+ FileResourcesManager::ensure_main_file_resources_file_exists
29
+ assert_equal(File.exists?(File.join(@@test_directory_location,".main_file_resources.yml")),true,"file_resources file should have been created")
30
+ assert_equal(FileResourcesManager::get_file_resources_location("test_kv"),File.join(@@test_directory_location,"test_kv"),"test file_resources file location should match")
31
+ FileResourcesManager::set("test_kv",{:name => "jockofcode",:occupation => "rubyist" }.to_yaml)
32
+ assert_equal(YAML::load(FileResourcesManager::get("test_kv"))[:name],"jockofcode","should have retreived 'jockofcode' from file_resources")
33
+ FileResourcesManager::set_file_resources_location("test_kv",File.join(@@test_directory_location,"not_the_default_kv.yml"))
34
+ assert_equal(FileResourcesManager::get_file_resources_location("test_kv"),File.join(@@test_directory_location,"not_the_default_kv.yml"),"test file_resources file location should match not_the_default_kv.yml")
35
+ assert_nil(FileResourcesManager::get("test_kv"),"It should not be getting the value from test_kv anymore")
36
+ FileResourcesManager::set("test_kv",{:name => "john",:occupation => "kneeder"}.to_yaml)
37
+ assert_equal(YAML::load(FileResourcesManager::get("test_kv"))[:name],"john","should now be john, not jockofcode")
27
38
  end
28
39
  end
@@ -0,0 +1,54 @@
1
+ require 'settings_manager'
2
+ require 'minitest/unit'
3
+ require 'fileutils'
4
+
5
+
6
+ class TestSettingsManager < Test::Unit::TestCase
7
+ @@test_directory_location = File.join(Dir.pwd,"file_resources_location_test")
8
+ def setup
9
+ puts "setup called"
10
+ remove_test_directory
11
+ FileUtils.mkdir_p(@@test_directory_location)
12
+ end
13
+ def remove_test_directory
14
+ if File.directory?(@@test_directory_location) #if the directory already exists then delete all the files out
15
+ (Dir.entries(@@test_directory_location) - [".",".."]).each{|f|
16
+ File.delete(File.join(@@test_directory_location,f))
17
+ }
18
+ Dir.rmdir(@@test_directory_location) # then remove the directory
19
+ end
20
+ end
21
+ def test_initialization
22
+ ENV["APP_SETTING"] = @@test_directory_location
23
+ settings_name = "test_database_info"
24
+ settings = SettingsManager.settings(settings_name)
25
+ settings.file_location = @@test_directory_location
26
+ assert_not_equal(settings,nil,"an empty settings file should not be nil")
27
+ assert_equal(settings.name,settings_name, "just the settings name is set")
28
+ assert_equal(settings.file_type,:yaml,"should be defaulted to yaml")
29
+ settings["server"] = "127.0.0.1"
30
+ assert_equal(settings["server"],"127.0.0.1","server address should match 127.0.0.1")
31
+ assert_equal(settings.file_location,@@test_directory_location ,"should be equal to the current directory")
32
+ assert_equal(settings.filename,settings.name,"should equal the name test_database_info")
33
+ settings.save_data
34
+
35
+ settings = nil
36
+ settings = SettingsManager.settings(settings_name)
37
+ assert_equal(settings["file_type"],nil,"file_type should still equal nil")
38
+ assert_equal(settings["server"],"127.0.0.1","it should still equal home")
39
+
40
+ settings2 = SettingsManager.settings(settings_name+"2")
41
+ settings2.file_location = @@test_directory_location
42
+ settings2.file_type = :csv
43
+ settings2["server"] = "1.1.1.1"
44
+ settings2.save_data
45
+ settings2 = nil
46
+ settings2 = SettingsManager.settings(settings_name+"2")
47
+ assert_equal(settings2["server"],"1.1.1.1","should equal 1.1.1.1")
48
+
49
+ end
50
+ def teardown
51
+ puts "teardown called"
52
+ remove_test_directory
53
+ end
54
+ end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_resources_manager
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease:
5
- version: "1.0"
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
6
11
  platform: ruby
7
12
  authors:
8
13
  - Branden Giacoletto
@@ -10,7 +15,7 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-05-16 00:00:00 -06:00
18
+ date: 2011-06-13 00:00:00 -06:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
@@ -21,10 +26,13 @@ dependencies:
21
26
  requirements:
22
27
  - - ">="
23
28
  - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
24
32
  version: "0"
25
33
  type: :development
26
34
  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!
35
+ 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 s = SettingsManager::settings('database');puts s["my_setting"] to get your file resources and the rest is automagically managed for you!
28
36
  email:
29
37
  - branden@carbondatacomputers.com
30
38
  executables: []
@@ -35,9 +43,10 @@ extra_rdoc_files: []
35
43
 
36
44
  files:
37
45
  - lib/file_resources_manager.rb
46
+ - lib/settings_manager.rb
38
47
  - tests/test_file_resources_manager.rb
48
+ - tests/test_settings_manager.rb
39
49
  - LICENSE
40
- - README
41
50
  has_rdoc: true
42
51
  homepage: http://JockOfCode.com/FileResourcesManager
43
52
  licenses: []
@@ -52,17 +61,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
61
  requirements:
53
62
  - - ">="
54
63
  - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
55
67
  version: "0"
56
68
  required_rubygems_version: !ruby/object:Gem::Requirement
57
69
  none: false
58
70
  requirements:
59
71
  - - ">="
60
72
  - !ruby/object:Gem::Version
61
- version: 1.3.6
73
+ hash: 23
74
+ segments:
75
+ - 1
76
+ - 0
77
+ - 0
78
+ version: 1.0.0
62
79
  requirements: []
63
80
 
64
81
  rubyforge_project: file_resources_manager
65
- rubygems_version: 1.6.2
82
+ rubygems_version: 1.5.3
66
83
  signing_key:
67
84
  specification_version: 3
68
85
  summary: Keeps your app file_resources in a central location
data/README DELETED
@@ -1,31 +0,0 @@
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
-