Class: Box::Item

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

Overview

Represents a folder or file stored on Box. Any attributes or actions typical to a Box item can be accessed through this class. The Item class contains only methods shared by Folder and File, and should not be instanciated directly.

Direct Known Subclasses

File, Folder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, parent, info) ⇒ Item

Create a new item representing either a file or folder.

Parameters:

  • api (Api)

    The Api instance used to generate requests.

  • parent (Folder)

    The Folder parent of this item.

  • info (Hash)

    The hash of initial info for this item.



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.

Examples:

item.name # returns @data['name'] or fetches it if not cached


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

#apiApi

Returns The Api used by this item.

Returns:

  • (Api)

    The Api used by this item.



12
13
14
# File 'lib/box/item.rb', line 12

def api
  @api
end

#dataHash

Returns The hash of info for this item.

Returns:

  • (Hash)

    The hash of info for this item.



9
10
11
# File 'lib/box/item.rb', line 9

def data
  @data
end

#parentFolder

Returns The parent of this item.

Returns:

  • (Folder)

    The parent of this item.



15
16
17
# File 'lib/box/item.rb', line 15

def parent
  @parent
end

Class Method Details

.typeString

Returns The string representation of this item.

Returns:

  • (String)

    The string representation of this item.



31
# File 'lib/box/item.rb', line 31

def self.type; raise "Overwrite this method"; end

.typesString

Returns The plural string representation of this item.

Returns:

  • (String)

    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

Note:

Copying folders is not currently supported.

Copy this item to the destination folder.

Parameters:

  • destination (Folder)

    The parent folder to copy the item to.

Returns:

  • (Item)

    The new copy of this item.



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

#deleteItem

Delete this item and all sub-items.

TODO: Return nil instead

Returns:



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.

Parameters:

  • message (String)

    The description message to use.

Returns:



122
123
124
125
126
# File 'lib/box/item.rb', line 122

def description(message)
  @api.set_description(type, id, message)

  self
end

#force_cached_infoObject

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

#idString

Returns The id of this item.

Returns:

  • (String)

    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.

Parameters:

  • refresh (Boolean) (defaults to: false)

    Does not use the cached copy if true.

Returns:



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.

Parameters:

  • destination (Folder)

    The new parent folder to use.

Returns:



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

#pathString

Returns The path of this item. This starts with a ‘/’.

Returns:

  • (String)

    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.

Parameters:

  • new_name (String)

    The new name for the item.

Returns:



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

#typeString

Returns The string representation of this item.

Returns:

  • (String)

    The string representation of this item.



39
# File 'lib/box/item.rb', line 39

def type; self.class.type; end

#typesString

Returns The plural string representation of this item.

Returns:

  • (String)

    The plural string representation of this item.



42
# File 'lib/box/item.rb', line 42

def types; self.class.types; end