Module: Megar::CatalogItem

Includes:
Enumerable
Included in:
File, Files, Folder, Folders
Defined in:
lib/megar/catalog/catalog_item.rb

Overview

This module defines the basic naming interface for catalog objects Override these methods as required

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

The ID (Mega handle)



20
21
22
# File 'lib/megar/catalog/catalog_item.rb', line 20

def id
  @id
end

#keyObject

The decrypted node key



35
36
37
# File 'lib/megar/catalog/catalog_item.rb', line 35

def key
  @key
end

#nameObject

The folder name



23
24
25
# File 'lib/megar/catalog/catalog_item.rb', line 23

def name
  @name
end

#parent_folder_idObject

The parent folder id



30
31
32
# File 'lib/megar/catalog/catalog_item.rb', line 30

def parent_folder_id
  @parent_folder_id
end

#payloadObject

The literal mega node descriptor (as received from API)



27
28
29
# File 'lib/megar/catalog/catalog_item.rb', line 27

def payload
  @payload
end

#sessionObject

A soft-reference to the owning session



17
18
19
# File 'lib/megar/catalog/catalog_item.rb', line 17

def session
  @session
end

#typeObject

The folder type id

0: File
1: Directory
2: Special node: Root (“Cloud Drive”)
3: Special node: Inbox
4: Special node: Trash Bin


43
44
45
# File 'lib/megar/catalog/catalog_item.rb', line 43

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Equality based on ID



109
110
111
# File 'lib/megar/catalog/catalog_item.rb', line 109

def ==(other)
  self.id == other.id
end

#[](*args) ⇒ Object

Returns indexed elements from the collection



104
105
106
# File 'lib/megar/catalog/catalog_item.rb', line 104

def [](*args)
  collection[*args]
end

#add(attributes) ⇒ Object

Adds an item to the local cached collection given attributes hash.



81
82
83
84
# File 'lib/megar/catalog/catalog_item.rb', line 81

def add(attributes)
  return false unless resource_class
  collection << resource_class.new(attributes.merge(session: self.session))
end

#attributes=(value) ⇒ Object

Assigns the attribute values splitting out separate attribute assignments from value if a hash



66
67
68
69
70
71
72
73
# File 'lib/megar/catalog/catalog_item.rb', line 66

def attributes=(value)
  return unless value.respond_to?(:keys)
  value.keys.each do |key|
    if respond_to?(assignment = "#{key}=".to_sym)
      send(assignment,value[key])
    end
  end
end

#collectionObject

Generic interface to return the currently applicable collection



76
77
78
# File 'lib/megar/catalog/catalog_item.rb', line 76

def collection
  @collection ||= []
end

#default_parent_folderObject

Returns the default parent folder (default is nil)



46
47
# File 'lib/megar/catalog/catalog_item.rb', line 46

def default_parent_folder
end

#eachObject

Implements Enumerable#each



99
100
101
# File 'lib/megar/catalog/catalog_item.rb', line 99

def each
  collection.each { |item| yield item }
end

#find_all_by_parent_folder_id(parent_folder_id) ⇒ Object

Returns all records matching parent_folder_id



135
136
137
# File 'lib/megar/catalog/catalog_item.rb', line 135

def find_all_by_parent_folder_id(parent_folder_id)
  find_all { |r| r.parent_folder_id == parent_folder_id }
end

#find_all_by_type(type) ⇒ Object

Returns all records matching type



125
126
127
# File 'lib/megar/catalog/catalog_item.rb', line 125

def find_all_by_type(type)
  find_all { |r| r.type == type }
end

#find_by_id(id) ⇒ Object

Returns the first record matching id



115
116
117
# File 'lib/megar/catalog/catalog_item.rb', line 115

def find_by_id(id)
  find { |r| r.id == id }
end

#find_by_name(name) ⇒ Object

Returns the first record matching name



130
131
132
# File 'lib/megar/catalog/catalog_item.rb', line 130

def find_by_name(name)
  find { |r| r.name == name }
end

#find_by_type(type) ⇒ Object

Returns the first record matching type



120
121
122
# File 'lib/megar/catalog/catalog_item.rb', line 120

def find_by_type(type)
  find { |r| r.type == type }
end

#initialize(attributes = {}) ⇒ Object

Adds an item to the local cached collection given attributes hash:

id:         id / mega node handle
payload:    the literal mega node descriptor
type:       the folder type
key:        the decrypted folder key
attributes: the decrypted attributes collection


12
13
14
# File 'lib/megar/catalog/catalog_item.rb', line 12

def initialize(attributes={})
  self.attributes = attributes
end

#parent_folderObject

Returns a handle to the enclosing folder (if any)



50
51
52
53
54
55
56
57
# File 'lib/megar/catalog/catalog_item.rb', line 50

def parent_folder
  return unless session
  if parent_folder_id
    session.folders.find_by_id(parent_folder_id)
  else
    default_parent_folder
  end
end

#reset!Object

Command: clears/re-initialises the collection



94
95
96
# File 'lib/megar/catalog/catalog_item.rb', line 94

def reset!
  @collection = []
end

#resource_classObject

Returns the expected class of items in the collection



87
88
89
90
91
# File 'lib/megar/catalog/catalog_item.rb', line 87

def resource_class
  "#{self.class.name}".chomp('s').constantize
rescue
  nil
end