Class: Box::Item
- Inherits:
-
Object
- Object
- Box::Item
- Defined in:
- lib/box/item.rb
Overview
Instance Attribute Summary collapse
-
#api ⇒ Api
The Api used by this item.
-
#data ⇒ Hash
The hash of info for this item.
-
#parent ⇒ Folder
The parent of this item.
Class Method Summary collapse
-
.type ⇒ String
The string representation of this item.
-
.types ⇒ String
The plural string representation of this item.
Instance Method Summary collapse
-
#copy(destination) ⇒ Item
Copy this item to the destination folder.
-
#delete ⇒ Item
Delete this item and all sub-items.
-
#force_cached_info ⇒ Object
Consider the item cached.
-
#id ⇒ String
The id of this item.
-
#info(refresh = false) ⇒ Item
Get the info for this item.
-
#initialize(api, parent, info) ⇒ Item
constructor
Create a new item representing either a file or folder.
-
#method_missing(sym, *args, &block) ⇒ Object
Provides an easy way to access this item’s info.
-
#move(destination) ⇒ Item
Move this item to the destination folder.
-
#path ⇒ String
The path of this item.
-
#rename(new_name) ⇒ Item
Rename this item.
-
#respond_to?(sym) ⇒ Boolean
Handles some cases in method_missing, but won’t always be accurate.
-
#set_description(message) ⇒ Item
Set the description of this item.
-
#share_private(emails, options = Hash.new) ⇒ Object
Shares this item privately with the given email addresses.
-
#share_public(options = Hash.new) ⇒ Object
Generates a share link for this item.
-
#type ⇒ String
The string representation of this item.
-
#types ⇒ String
The plural string representation of this item.
-
#unshare ⇒ Object
Unshares this item.
Constructor Details
#initialize(api, parent, info) ⇒ Item
Create a new item representing either a file or folder.
24 25 26 27 28 29 30 |
# File 'lib/box/item.rb', line 24 def initialize(api, parent, info) @api = api @parent = parent @data = Hash.new update_info(info) # merges with the info hash, and renames some fields end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Provides an easy way to access this item’s info.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/box/item.rb', line 139 def method_missing(sym, *args, &block) # TODO: Why not symbols? # convert to a string str = sym.to_s # determine whether to refresh the cache refresh = args ? args.first : false # return the value if it already exists return @data[str] if not refresh and @data.key?(str) # value didn't exist, so try to update the info # TODO: Figure out a good way to work with multiple sources. case str when 'comments' self.get_comments when self.info(refresh) end # try returning the value again return @data[str] if @data.key?(str) # we didn't find a value, so it must be invalid # call the normal method_missing function super end |
Instance Attribute Details
#api ⇒ Api
Returns The Api used by this item.
14 15 16 |
# File 'lib/box/item.rb', line 14 def api @api end |
#data ⇒ Hash
Returns The hash of info for this item.
11 12 13 |
# File 'lib/box/item.rb', line 11 def data @data end |
#parent ⇒ Folder
Returns The parent of this item.
17 18 19 |
# File 'lib/box/item.rb', line 17 def parent @parent end |
Class Method Details
.type ⇒ String
Returns The string representation of this item.
33 |
# File 'lib/box/item.rb', line 33 def self.type; raise "Overwrite this method"; end |
.types ⇒ String
Returns The plural string representation of this item.
36 |
# File 'lib/box/item.rb', line 36 def self.types; type + 's'; end |
Instance Method Details
#copy(destination) ⇒ Item
Copying folders is not currently supported.
Copy this item to the destination folder.
87 88 89 90 91 92 93 |
# File 'lib/box/item.rb', line 87 def copy(destination) @api.copy(type, id, destination.id) destination.delete_info(self.types) self.class.new(api, destination, @data) end |
#delete ⇒ Item
Delete this item and all sub-items.
TODO: Return nil instead
111 112 113 114 115 116 117 118 |
# File 'lib/box/item.rb', line 111 def delete @api.delete(type, id) parent.delete_info(self.types) @parent = nil self end |
#force_cached_info ⇒ Object
Consider the item cached. This prevents an additional api when we know the item is fully fetched.
174 175 176 |
# File 'lib/box/item.rb', line 174 def force_cached_info @cached_info = true end |
#id ⇒ String
Returns The id of this item.
47 48 49 50 |
# File 'lib/box/item.rb', line 47 def id # overloads Object#id @data['id'] end |
#info(refresh = false) ⇒ Item
Get the info for this item. Uses a cached copy if avaliable, or else it is fetched from the api.
57 58 59 60 61 62 63 64 |
# File 'lib/box/item.rb', line 57 def info(refresh = false) return self if @cached_info and not refresh @cached_info = true update_info(get_info) self end |
#move(destination) ⇒ Item
Move this item to the destination folder.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/box/item.rb', line 70 def move(destination) @api.move(type, id, destination.id) parent.delete_info(self.types) destination.delete_info(self.types) @parent = destination self end |
#path ⇒ String
Returns The path of this item. This starts with a ‘/’, unless it is root.
131 132 133 |
# File 'lib/box/item.rb', line 131 def path "#{ parent.path + '/' if parent }#{ name }" end |
#rename(new_name) ⇒ Item
Rename this item.
99 100 101 102 103 104 105 |
# File 'lib/box/item.rb', line 99 def rename(new_name) @api.rename(type, id, new_name) update_info('name' => new_name) self end |
#respond_to?(sym) ⇒ Boolean
Handles some cases in method_missing, but won’t always be accurate.
168 169 170 |
# File 'lib/box/item.rb', line 168 def respond_to?(sym) @data.key?(sym.to_s) or super end |
#set_description(message) ⇒ Item
Set the description of this item.
124 125 126 127 128 |
# File 'lib/box/item.rb', line 124 def set_description() @api.set_description(type, id, ) self end |
#share_private(emails, options = Hash.new) ⇒ Object
Shares this item privately with the given email addresses.
195 196 197 198 199 |
# File 'lib/box/item.rb', line 195 def share_private(emails, = Hash.new) @api.share_private(type, id, emails, ) true end |
#share_public(options = Hash.new) ⇒ Object
Generates a share link for this item.
TODO: Document the options.
186 187 188 |
# File 'lib/box/item.rb', line 186 def share_public( = Hash.new) @api.share_public(type, id, )['public_name'] end |
#type ⇒ String
Returns The string representation of this item.
41 |
# File 'lib/box/item.rb', line 41 def type; self.class.type; end |
#types ⇒ String
Returns The plural string representation of this item.
44 |
# File 'lib/box/item.rb', line 44 def types; self.class.types; end |
#unshare ⇒ Object
Unshares this item.
202 203 204 |
# File 'lib/box/item.rb', line 202 def unshare @api.unshare(type, id) end |