Class: FileResourcesManager

Inherits:
Object
  • Object
show all
Defined in:
lib/file_resources_manager.rb

Class Method Summary collapse

Class Method Details

.ensure_main_file_resources_file_existsObject

guarantees the directory exists and a file is present at the location the main file_resources file should be at



102
103
104
105
106
107
108
# File 'lib/file_resources_manager.rb', line 102

def self.ensure_main_file_resources_file_exists
	filename = main_file_resources_filename
	if !File.file?(filename) then 
		FileUtils.mkdir_p(File.dirname(filename))
		File.open(filename,"w"){|f| f << {}.to_yaml }
	end
end

.file_resources_location=(args) ⇒ Object



87
88
89
# File 'lib/file_resources_manager.rb', line 87

def self.file_resources_location=(args)
	set_file_resources_location(*args)
end

.get(file_resources_name) ⇒ Object

Loads the file that is referred to by <file_resources_name>



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/file_resources_manager.rb', line 39

def self.get file_resources_name
	ensure_main_file_resources_file_exists

	file_resources_file = if file_resources_name == "main_file_resources"
				main_file_resources_filename
			else
				# BTW example of a-b-c recursion
				get_file_resources_location(file_resources_name)
			end

	if File.exists?(file_resources_file)
		return File.read(file_resources_file)
	else
		# a NO_SETTINGS_ERROR could be raised here
		# but I will let the implementer decide on
		# the validation
		return nil
	end
end

.get_file_resources_location(file_resources_name) ⇒ Object Also known as: file_resources_location

gets the full path and filename that is loaded when you read <file_resources_name>



72
73
74
# File 'lib/file_resources_manager.rb', line 72

def self.get_file_resources_location(file_resources_name)
	YAML::load(get("main_file_resources"))[file_resources_name] || File.join(main_file_resources_file_location, file_resources_name)
end

.main_file_resources_file_locationObject

gets the directory where the main file_resources file is saved



93
94
95
# File 'lib/file_resources_manager.rb', line 93

def self.main_file_resources_file_location
	ENV["APP_SETTING"] || File.join(Dir.home,".app_file_resources")
end

.main_file_resources_filenameObject

gets the full path and filename of the main file_resources file



97
98
99
# File 'lib/file_resources_manager.rb', line 97

def self.main_file_resources_filename
	File.join(main_file_resources_file_location,".main_file_resources.yml")
end

.set(file_resources_name, file_resources) ⇒ Object

Saves the file that is referred to by <file_resources_name>



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/file_resources_manager.rb', line 59

def self.set(file_resources_name,file_resources)
	ensure_main_file_resources_file_exists

	file_resources_file = if file_resources_name == "main_file_resources"
				main_file_resources_filename
			else
				get_file_resources_location(file_resources_name)
			end

	File.open(file_resources_file,"w"){|f| f << file_resources }
	return file_resources
end

.set_file_resources_location(file_resources_name, location) ⇒ Object

sets the full path and filename that is referred to by <setting_name> to <location>



76
77
78
79
80
81
# File 'lib/file_resources_manager.rb', line 76

def self.set_file_resources_location(file_resources_name,location)
	main_file_resources = YAML::load(get("main_file_resources"))
	main_file_resources[file_resources_name] = location
	set("main_file_resources",main_file_resources.to_yaml)
	self
end