Class: SakaiInfo::ContentResource

Inherits:
Content show all
Defined in:
lib/sakai-info/content.rb

Direct Known Subclasses

DeletedContentResource

Instance Attribute Summary collapse

Attributes inherited from Content

#parent_id

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Content

all_serializations, #binary_entity, #child_summary_serialization, #effective_realm, #mod_details_serialization, #mod_serialization, #original_mod_details_serialization, #original_mod_serialization, #parent, #properties_serialization, #realm, #realm_serialization

Methods included from ModProps

included

Methods inherited from SakaiObject

all_serializations, #dbrow_only_serialization, #dbrow_serialization, descendants, #object_type_serialization, #serialize, #shell_serialization, #to_csv, #to_json, #to_yaml

Constructor Details

#initialize(dbrow) ⇒ ContentResource

Returns a new instance of ContentResource.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sakai-info/content.rb', line 164

def initialize(dbrow)
  @dbrow = dbrow

  @id = @dbrow[:resource_id]
  @parent_id = @dbrow[:in_collection]
  @file_path = @dbrow[:file_path]
  @uuid = @dbrow[:resource_uuid]
  @file_size = @dbrow[:file_size].to_i
  @context = @dbrow[:context]
  @resource_type_id = @dbrow[:resource_type_id]

  @table_name = "content_resource"
  @id_column = "resource_id"
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



156
157
158
# File 'lib/sakai-info/content.rb', line 156

def context
  @context
end

#dbrowObject (readonly)

Returns the value of attribute dbrow.



156
157
158
# File 'lib/sakai-info/content.rb', line 156

def dbrow
  @dbrow
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



156
157
158
# File 'lib/sakai-info/content.rb', line 156

def file_path
  @file_path
end

#resource_type_idObject (readonly)

Returns the value of attribute resource_type_id.



156
157
158
# File 'lib/sakai-info/content.rb', line 156

def resource_type_id
  @resource_type_id
end

#uuidObject (readonly)

Returns the value of attribute uuid.



156
157
158
# File 'lib/sakai-info/content.rb', line 156

def uuid
  @uuid
end

Class Method Details

.clear_cacheObject



158
159
160
161
# File 'lib/sakai-info/content.rb', line 158

def self.clear_cache
  @@cache = {}
  @@uuid_cache = {}
end

.count_by_parent(parent_id) ⇒ Object



220
221
222
# File 'lib/sakai-info/content.rb', line 220

def self.count_by_parent(parent_id)
  ContentResource.query_by_parent(parent_id).count
end

.find(id) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/sakai-info/content.rb', line 179

def self.find(id)
  if @@cache[id].nil?
    row = DB.connect[:content_resource].where(:resource_id => id).first
    if row.nil?
      raise ObjectNotFoundException.new(ContentResource, id)
    end
    @@cache[id] = ContentResource.new(row)
    @@uuid_cache[row[:uuid]] = @@cache[id]
  end
  @@cache[id]
end

.find_by_parent(parent_id) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/sakai-info/content.rb', line 211

def self.find_by_parent(parent_id)
  resources = []
  ContentResource.query_by_parent(parent_id).all.each do |row|
    @@cache[row[:resource_id]] = ContentResource.new(row)
    resources << @@cache[row[:resource_id]]
  end
  resources
end

.find_by_uuid(uuid) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/sakai-info/content.rb', line 191

def self.find_by_uuid(uuid)
  if @@uuid_cache[uuid].nil?
    row = DB.connect[:content_resource].where(:resource_uuid => uuid).first
    if row.nil?
      raise ObjectNotFoundException.new(ContentResource, uuid)
    end
    @@uuid_cache[uuid] = ContentResource.new(row)
    @@cache[row[:resource_id]] = @@uuid_cache[uuid]
  end
  @@uuid_cache[uuid]
end

.query_by_parent(parent_id) ⇒ Object



207
208
209
# File 'lib/sakai-info/content.rb', line 207

def self.query_by_parent(parent_id)
  DB.connect[:content_resource].where(:in_collection => parent_id)
end

Instance Method Details

#default_serializationObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/sakai-info/content.rb', line 224

def default_serialization
  result = {
    "id" => self.id,
    "uuid" => self.uuid,
    "file_path" => self.file_path,
    "size_on_disk" => self.size_on_disk,
    "context" => self.context,
    "resource_type" => self.resource_type_id,
  }
  if result["uuid"].nil?
    result.delete("uuid")
  end
  if result["context"].nil?
    result.delete("context")
  end
  result
end

#detailed_summary_serializationObject



249
250
251
252
253
254
255
# File 'lib/sakai-info/content.rb', line 249

def detailed_summary_serialization
  {
    "id" => self.id,
    "size" => self.size_on_disk,
    "file_path" => self.file_path,
  }
end

#size_on_diskObject



203
204
205
# File 'lib/sakai-info/content.rb', line 203

def size_on_disk
  @file_size
end

#summary_serializationObject



242
243
244
245
246
247
# File 'lib/sakai-info/content.rb', line 242

def summary_serialization
  {
    "id" => self.id,
    "size" => self.size_on_disk,
  }
end