Class: StorageRoom::Collection

Inherits:
Model show all
Defined in:
lib/storage_room/models/collection.rb

Overview

A collection is used to define the structure of a data set.

Instance Attribute Summary

Attributes inherited from Model

#skip_webhooks

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

all, create, #create, #destroy, #errors, find, #initialize, #new_record?, #reset!, #save, #to_hash, #update, #valid?

Methods inherited from Resource

handle_critical_response_errors, #loaded?, meta_data?, #reload

Methods included from Plugins

#included, #plugin, #plugins

Methods included from Accessors

#[], #as_json, #attributes, #attributes=, #eql?, #hash, #initialize, #inspect, #loaded?, #proxy?, #reset!, #response_data, #response_data=, #set_from_response_data, #to_hash

Constructor Details

This class inherits a constructor from StorageRoom::Model

Class Method Details

.index_pathObject

:nodoc:



13
14
15
# File 'lib/storage_room/models/collection.rb', line 13

def index_path # :nodoc:
  "#{Resource.base_uri}/collections"
end

.json_nameObject

:nodoc:



21
22
23
# File 'lib/storage_room/models/collection.rb', line 21

def json_name # :nodoc:
  'collection'
end

.show_path(collection_id) ⇒ Object

:nodoc:



17
18
19
# File 'lib/storage_room/models/collection.rb', line 17

def show_path(collection_id) # :nodoc:
  "#{index_path}/#{collection_id}"
end

Instance Method Details

#association_fieldsObject

All fields of type AssociationField



62
63
64
# File 'lib/storage_room/models/collection.rb', line 62

def association_fields
  fields.select{|f| f.is_a?(AssociationField)}
end

#deleted_entriesObject

Load all deleted entries of a collection



46
47
48
# File 'lib/storage_room/models/collection.rb', line 46

def deleted_entries
  Array.load(self[:@deleted_entries_url])
end

#entriesObject

Load all the entries of a collection



39
40
41
42
43
# File 'lib/storage_room/models/collection.rb', line 39

def entries
  require_initialized_entry_class do 
    Array.load(self[:@entries_url])
  end
end

#entry_classObject

The class for the collection’s entries



32
33
34
35
36
# File 'lib/storage_room/models/collection.rb', line 32

def entry_class
  require_initialized_entry_class do 
    self.entry_class_name.constantize
  end
end

#entry_class_nameObject

The class name of the collection’s entries, can be overridden with a mapping



27
28
29
# File 'lib/storage_room/models/collection.rb', line 27

def entry_class_name
  ensure_loaded { StorageRoom.entry_class_for_name(entry_type)}
end

#field(identifier) ⇒ Object

The field with a specific identifier



51
52
53
54
55
56
57
58
59
# File 'lib/storage_room/models/collection.rb', line 51

def field(identifier)
  ensure_loaded do
    fields.each do |f|
      return f if f.identifier == identifier
    end
  end
  
  nil
end

#load_associated_collectionsObject

Load all Collections that are related to the current one through AssociationFields



67
68
69
70
71
72
73
74
# File 'lib/storage_room/models/collection.rb', line 67

def load_associated_collections
  array = association_fields

  if array.map{|f| f.collection_loaded?}.include?(false)
    StorageRoom.log("Fetching associated collections for '#{name}'")
    array.each{|f| f.collection}
  end
end