Module: GetUrl::FileManager
Overview
The FileManager deals with the local (cached) files.
Instance Method Summary collapse
-
#all_items_cache_exist? ⇒ TrueClass|FalseClass
Returns true if the all-items-cache file exists.
-
#cache_all_items(items) ⇒ Object
Cache the given items to the all-items-cache.
-
#clean_and_save_items_to_yaml_file(str, file) ⇒ Integer
Loads the clean items in the given Yaml string and stores it in the given file.
-
#clear_all_items_cache ⇒ Object
Clears the all-items-cache.
-
#get_all_items_cache_file ⇒ String
Returns the path of the all-items-cache file.
-
#get_all_items_from_cache ⇒ Object
Returns all items from the all-items-cache.
-
#get_local_source_file(id) ⇒ String
Returns the local file name for the given source id.
-
#load_items_from_yaml_file(file) ⇒ Array
Loads the items from the given local file.
-
#load_items_from_yaml_string(str) ⇒ Array
Loads the items from the given string.
-
#save_items_to_yaml_file(items, file) ⇒ Integer
Save the given items to the given file in Yaml.
Instance Method Details
#all_items_cache_exist? ⇒ TrueClass|FalseClass
Returns true if the all-items-cache file exists.
81 82 83 |
# File 'lib/geturl/geturl-file-manager.rb', line 81 def all_items_cache_exist? return File.exists?(get_all_items_cache_file) end |
#cache_all_items(items) ⇒ Object
Cache the given items to the all-items-cache.
88 89 90 |
# File 'lib/geturl/geturl-file-manager.rb', line 88 def cache_all_items(items) save_items_to_yaml_file(items, get_all_items_cache_file) end |
#clean_and_save_items_to_yaml_file(str, file) ⇒ Integer
Loads the clean items in the given Yaml string and stores it in the given file
57 58 59 60 61 |
# File 'lib/geturl/geturl-file-manager.rb', line 57 def clean_and_save_items_to_yaml_file(str, file) clear_all_items_cache cleaned_items = load_items_from_yaml_string(str) save_items_to_yaml_file(cleaned_items, file) end |
#clear_all_items_cache ⇒ Object
Clears the all-items-cache.
102 103 104 |
# File 'lib/geturl/geturl-file-manager.rb', line 102 def clear_all_items_cache File::unlink(get_all_items_cache_file) if all_items_cache_exist? end |
#get_all_items_cache_file ⇒ String
Returns the path of the all-items-cache file.
74 75 76 |
# File 'lib/geturl/geturl-file-manager.rb', line 74 def get_all_items_cache_file File.join(Dir.home, '.geturl', 'all-items-cache.yaml') end |
#get_all_items_from_cache ⇒ Object
Returns all items from the all-items-cache.
95 96 97 |
# File 'lib/geturl/geturl-file-manager.rb', line 95 def get_all_items_from_cache return load_items_from_yaml_file(get_all_items_cache_file) end |
#get_local_source_file(id) ⇒ String
Returns the local file name for the given source id.
67 68 69 |
# File 'lib/geturl/geturl-file-manager.rb', line 67 def get_local_source_file(id) return File.join(@sources_path, "#{id}.yaml") end |
#load_items_from_yaml_file(file) ⇒ Array
Loads the items from the given local file.
17 18 19 20 |
# File 'lib/geturl/geturl-file-manager.rb', line 17 def load_items_from_yaml_file(file) str = File.read(file) rescue '' return load_items_from_yaml_string(str) end |
#load_items_from_yaml_string(str) ⇒ Array
Loads the items from the given string.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/geturl/geturl-file-manager.rb', line 27 def load_items_from_yaml_string(str) items = [] yaml = YAML.load(str).to_a || [] rescue [] yaml.each {|raw| items << { 'url' => raw['url'], 'name' => raw['name'], 'description' => raw['description'], 'tags' => raw['tags'].to_a } if (raw.include?('url')) && (raw.include?('name')) } return items end |
#save_items_to_yaml_file(items, file) ⇒ Integer
Save the given items to the given file in Yaml.
46 47 48 49 50 |
# File 'lib/geturl/geturl-file-manager.rb', line 46 def save_items_to_yaml_file(items, file) str = "# This file is generated by geturl.\n" str += items.to_yaml File.write(file, str) end |