Module: RTunesU::Persistence

Defined in:
lib/rtunesu/persistence.rb

Defined Under Namespace

Modules: Finder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
# File 'lib/rtunesu/persistence.rb', line 21

def self.included(base)
  base.extend(Finder)
end

Instance Method Details

#create(connection = nil) ⇒ Object

called when .save is called on an object that has no Handle (i.e. does not already exist in iTunes U)

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/rtunesu/persistence.rb', line 39

def create(connection = nil)
  connection ||= self.base_connection
  
  response = Hpricot.XML(connection.process(Document::Add.new(self).xml))
  raise CannotSave, response.at('error').innerHTML if response.at('error')
  @handle = response.at('AddedObjectHandle').innerHTML
  edits.clear
  self
end

#delete(connection = nil) ⇒ Object

Deletes the entity from iTunes U. This cannot be undone.

Raises:

  • (Exception)


63
64
65
66
67
68
69
70
# File 'lib/rtunesu/persistence.rb', line 63

def delete(connection = nil)
  connection ||= self.base_connection
  
  response = Hpricot.XML(connection.process(Document::Delete.new(self).xml))
  raise Exception, response.at('error').innerHTML if response.at('error')
  @handle = nil
  self
end

#load_from_xml(xml_or_entity) ⇒ Object



25
26
27
# File 'lib/rtunesu/persistence.rb', line 25

def load_from_xml(xml_or_entity)
  self.source_xml = Hpricot.XML(xml_or_entity).at("//ITunesUResponse//#{self.class_name}//Handle[text()=#{self.handle}]..")
end

#save(connection = nil) ⇒ Object

Saves the entity to iTunes U. Save takes single argument (an iTunes U connection object).

If the entity is unsaved this will create the entity and populate its handle attribte.

If the entity has already been saved it will send the updated data (if any) to iTunes U.



52
53
54
55
# File 'lib/rtunesu/persistence.rb', line 52

def save(connection = nil)
  connection ||= self.base_connection
  saved? ? update(connection) : create(connection)
end

#saved?Boolean

Has the entity be previously saved in iTunes U

Returns:

  • (Boolean)


58
59
60
# File 'lib/rtunesu/persistence.rb', line 58

def saved?
  self.handle ? true : false
end

#update(connection = nil) ⇒ Object

called when .save is called on an object that is already stored in iTunes U



30
31
32
33
34
35
36
# File 'lib/rtunesu/persistence.rb', line 30

def update(connection = nil)
  connection ||= self.base_connection
  
  connection.process(Document::Merge.new(self).xml)
  edits.clear
  self
end