Module: Megar::CatalogItem
- Includes:
- Enumerable
- 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
-
#id ⇒ Object
The ID (Mega handle).
-
#key ⇒ Object
The decrypted node key.
-
#name ⇒ Object
The folder name.
-
#payload ⇒ Object
The literal mega node descriptor (as received from API).
-
#type ⇒ Object
The folder type id 0: File 1: Directory 2: Special node: Root (“Cloud Drive”) 3: Special node: Inbox 4: Special node: Trash Bin.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Equality based on ID.
-
#[](*args) ⇒ Object
Returns indexed elements from the collection.
-
#attributes=(value) ⇒ Object
Assigns the attribute values splitting out separate attribute assignments from
value
if a hash. -
#collection ⇒ Object
Generic interface to return the currently applicable collection.
-
#each ⇒ Object
Implements Enumerable#each.
-
#find_by_type(type) ⇒ Object
Returns the first record matching
type
. -
#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. -
#reset! ⇒ Object
Command: clears/re-initialises the collection.
-
#resource_class ⇒ Object
Returns the expected class of items in the collection.
Instance Attribute Details
#id ⇒ Object
The ID (Mega handle)
17 18 19 |
# File 'lib/megar/catalog/catalog_item.rb', line 17 def id @id end |
#key ⇒ Object
The decrypted node key
43 44 45 |
# File 'lib/megar/catalog/catalog_item.rb', line 43 def key @key end |
#name ⇒ Object
The folder name
20 21 22 |
# File 'lib/megar/catalog/catalog_item.rb', line 20 def name @name end |
#payload ⇒ Object
The literal mega node descriptor (as received from API)
24 25 26 |
# File 'lib/megar/catalog/catalog_item.rb', line 24 def payload @payload end |
#type ⇒ Object
The folder type id
0: File
1: Directory
2: Special node: Root (“Cloud Drive”)
3: Special node: Inbox
4: Special node: Trash Bin
51 52 53 |
# File 'lib/megar/catalog/catalog_item.rb', line 51 def type @type end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Equality based on ID
80 81 82 |
# File 'lib/megar/catalog/catalog_item.rb', line 80 def ==(other) self.id == other.id end |
#[](*args) ⇒ Object
Returns indexed elements from the collection
75 76 77 |
# File 'lib/megar/catalog/catalog_item.rb', line 75 def [](*args) collection[*args] end |
#attributes=(value) ⇒ Object
Assigns the attribute values splitting out separate attribute assignments from value
if a hash
33 34 35 36 37 38 39 40 |
# File 'lib/megar/catalog/catalog_item.rb', line 33 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 |
#collection ⇒ Object
Generic interface to return the currently applicable collection
55 56 57 |
# File 'lib/megar/catalog/catalog_item.rb', line 55 def collection @collection ||= [] end |
#each ⇒ Object
Implements Enumerable#each
70 71 72 |
# File 'lib/megar/catalog/catalog_item.rb', line 70 def each collection.each { |item| yield item } end |
#find_by_type(type) ⇒ Object
Returns the first record matching type
86 87 88 |
# File 'lib/megar/catalog/catalog_item.rb', line 86 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 |
#reset! ⇒ Object
Command: clears/re-initialises the collection
65 66 67 |
# File 'lib/megar/catalog/catalog_item.rb', line 65 def reset! @collection = [] end |
#resource_class ⇒ Object
Returns the expected class of items in the collection
60 61 62 |
# File 'lib/megar/catalog/catalog_item.rb', line 60 def resource_class "#{self.class.name}".chomp('s').constantize end |