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.
-
#description(message) ⇒ Item
Set the description of this item.
-
#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.
-
#type ⇒ String
The string representation of this item.
-
#types ⇒ String
The plural string representation of this item.
Constructor Details
#initialize(api, parent, info) ⇒ Item
Create a new item representing either a file or folder.
22 23 24 25 26 27 28 |
# File 'lib/box/item.rb', line 22 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.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/box/item.rb', line 137 def method_missing(sym, *args, &block) # TODO: Why not symbols? # convert to a string str = sym.to_s # return the value if it already exists return @data[str] if @data.key?(str) # value didn't exist, so try to update the info self.info # 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.
12 13 14 |
# File 'lib/box/item.rb', line 12 def api @api end |
#data ⇒ Hash
Returns The hash of info for this item.
9 10 11 |
# File 'lib/box/item.rb', line 9 def data @data end |
#parent ⇒ Folder
Returns The parent of this item.
15 16 17 |
# File 'lib/box/item.rb', line 15 def parent @parent end |
Class Method Details
.type ⇒ String
Returns The string representation of this item.
31 |
# File 'lib/box/item.rb', line 31 def self.type; raise "Overwrite this method"; end |
.types ⇒ String
Returns The plural string representation of this item.
34 |
# File 'lib/box/item.rb', line 34 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.
85 86 87 88 89 90 91 |
# File 'lib/box/item.rb', line 85 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
109 110 111 112 113 114 115 116 |
# File 'lib/box/item.rb', line 109 def delete @api.delete(type, id) parent.delete_info(self.types) @parent = nil self end |
#description(message) ⇒ Item
Set the description of this item.
122 123 124 125 126 |
# File 'lib/box/item.rb', line 122 def description() @api.set_description(type, id, ) self end |
#force_cached_info ⇒ Object
Consider the item cached. This prevents an additional api when we know the item is fully fetched.
158 159 160 |
# File 'lib/box/item.rb', line 158 def force_cached_info @cached_info = true end |
#id ⇒ String
Returns The id of this item.
45 46 47 48 |
# File 'lib/box/item.rb', line 45 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.
55 56 57 58 59 60 61 62 |
# File 'lib/box/item.rb', line 55 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.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/box/item.rb', line 68 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 ‘/’.
129 130 131 |
# File 'lib/box/item.rb', line 129 def path "#{ parent.path + '/' if parent }#{ name }" end |
#rename(new_name) ⇒ Item
Rename this item.
97 98 99 100 101 102 103 |
# File 'lib/box/item.rb', line 97 def rename(new_name) @api.rename(type, id, new_name) update_info('name' => new_name) self end |
#type ⇒ String
Returns The string representation of this item.
39 |
# File 'lib/box/item.rb', line 39 def type; self.class.type; end |
#types ⇒ String
Returns The plural string representation of this item.
42 |
# File 'lib/box/item.rb', line 42 def types; self.class.types; end |