Class: CloudDoor::FileList
- Inherits:
-
Object
- Object
- CloudDoor::FileList
- Defined in:
- lib/cloud_door/file_list.rb
Instance Attribute Summary collapse
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#list_file ⇒ Object
Returns the value of attribute list_file.
-
#list_name ⇒ Object
Returns the value of attribute list_name.
Instance Method Summary collapse
- #add_list(items, file_id, file_name) ⇒ Object
- #add_list_top(items) ⇒ Object
- #convert_name_to_id(mode, file_name = '') ⇒ Object
- #delete_file ⇒ Object
-
#initialize(list_name, data_path, id = nil) ⇒ FileList
constructor
A new instance of FileList.
- #load_list ⇒ Object
- #pull_current_dir ⇒ Object
- #pull_file_properties(file_name) ⇒ Object
- #pull_parent_id ⇒ Object
- #remove_list(back) ⇒ Object
- #set_locate(id) ⇒ Object
- #top?(file_name) ⇒ Boolean
- #update_list(items) ⇒ Object
- #write_file_list(items, file_id = '', file_name = '') ⇒ Object
Constructor Details
#initialize(list_name, data_path, id = nil) ⇒ FileList
Returns a new instance of FileList.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cloud_door/file_list.rb', line 6 def initialize(list_name, data_path, id = nil) @list_name = list_name @data_path = data_path if (id.nil?) @list_file = @data_path + @list_name else list_dir = @data_path + "#{id}" unless File.exists?(list_dir) Dir.mkdir(list_dir) end @list_file = "#{list_dir}/#{@list_name}" end @list = nil end |
Instance Attribute Details
#list ⇒ Object (readonly)
Returns the value of attribute list.
4 5 6 |
# File 'lib/cloud_door/file_list.rb', line 4 def list @list end |
#list_file ⇒ Object
Returns the value of attribute list_file.
3 4 5 |
# File 'lib/cloud_door/file_list.rb', line 3 def list_file @list_file end |
#list_name ⇒ Object
Returns the value of attribute list_name.
3 4 5 |
# File 'lib/cloud_door/file_list.rb', line 3 def list_name @list_name end |
Instance Method Details
#add_list(items, file_id, file_name) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/cloud_door/file_list.rb', line 63 def add_list(items, file_id, file_name) return false if items.nil? || !items.is_a?(Hash) return false if file_id.nil? || file_id.empty? return false if file_name.nil? || file_name.empty? return false if load_list == false @list << {'id' => file_id, 'name' => file_name, 'items' => items} write_file end |
#add_list_top(items) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/cloud_door/file_list.rb', line 56 def add_list_top(items) return false if items.nil? || !items.is_a?(Hash) @list = [] @list << {'id' => 'top', 'name' => 'top', 'items' => items} write_file end |
#convert_name_to_id(mode, file_name = '') ⇒ Object
118 119 120 121 122 |
# File 'lib/cloud_door/file_list.rb', line 118 def convert_name_to_id(mode, file_name = '') return false if load_list == false return false unless %w(current parent target).include?(mode) send("convert_#{mode}_id", file_name) end |
#delete_file ⇒ Object
89 90 91 92 93 94 |
# File 'lib/cloud_door/file_list.rb', line 89 def delete_file File.delete(@list_file) if File.exist?(@list_file) true rescue false end |
#load_list ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cloud_door/file_list.rb', line 29 def load_list list = [] if File.exist?(@list_file) marshal = File.open(@list_file).read list = Marshal.load(marshal) return false unless list.is_a?(Array) end @list = list true end |
#pull_current_dir ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/cloud_door/file_list.rb', line 100 def pull_current_dir return false if load_list == false return '/top' if @list.size < 2 files = [] @list.each { |part| files << part['name'] unless part['name'].nil? } '/' + files.join('/') end |
#pull_file_properties(file_name) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/cloud_door/file_list.rb', line 108 def pull_file_properties(file_name) if @list.nil? return false if load_list == false end return false if @list.empty? items = @list.last['items'] return false if items.empty? || !items.key?(file_name) items[file_name] end |
#pull_parent_id ⇒ Object
96 97 98 |
# File 'lib/cloud_door/file_list.rb', line 96 def pull_parent_id convert_name_to_id('current') end |
#remove_list(back) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/cloud_door/file_list.rb', line 80 def remove_list(back) return false if load_list == false size = @list.size return false if back > size last = size - back @list = @list[0..last] write_file end |
#set_locate(id) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/cloud_door/file_list.rb', line 21 def set_locate(id) list_dir = @data_path + "#{id}" unless File.exists?(list_dir) Dir.mkdir(list_dir) end @list_file = "#{list_dir}/#{@list_name}" end |
#top?(file_name) ⇒ Boolean
124 125 126 127 |
# File 'lib/cloud_door/file_list.rb', line 124 def top?(file_name) back = (file_name.scan(CloudStorage::PARENT_DIR_PAT).size) + 1 @list.count < back end |
#update_list(items) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/cloud_door/file_list.rb', line 72 def update_list(items) return false if items.nil? || !items.is_a?(Hash) return false if load_list == false last_node = @list[-1] @list[-1] = {'id' => last_node['id'], 'name' => last_node['name'], 'items' => items} write_file end |
#write_file_list(items, file_id = '', file_name = '') ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cloud_door/file_list.rb', line 40 def write_file_list(items, file_id = '', file_name = '') return false if load_list == false if @list.empty? add_list_top(items) elsif file_name =~ CloudStorage::PARENT_DIR_PAT back = file_name.scan(CloudStorage::PARENT_DIR_PAT).size + 1 remove_list(back) else if file_name.nil? || file_name.empty? update_list(items) else add_list(items, file_id, file_name) end end end |