Class: RTunesU::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/rtunesu/entity.rb

Overview

deimos.apple.com/rsrc/xsd/iTunesURequest-1.1.3.xsd A Base class reprenseting the various entities seen in iTunes U. Subclassed into the actual entity classes (Course, Division, Track, etc). Entity is mostly an object oriented interface to the underlying XML data returned from iTunes U. Most attributes of an Entity are read by searching the souce XML returned from iTunes U

Reading and Writing Attributes

c = Course.find(12345, rtunes_connection_object) # finds the Course in iTunes U and stores its XML data c.handle # finds the <Handle> element in the XML data and returns its value (in this case 12345) c.name # finds the <Name> element in the XML data and returns its value (e.g. ‘My Example Course’) c.name = ‘Podcasting: a Revolution’ # writes a hash of unsaved data that will be sent to iTunes U.

Accessing related entities

Related Entity objects are accessed with the pluralized form of their class name.

To access a Course’s related Group entities, you would use c.groups. This will return an array of Group objects (or an empty Array object if there are no associated Groups) You can set the array of associated entities by using the ‘=’ form of the accessor and add another element to the end of an array of related entities with ‘<<’ Examples: c = Course.find(12345, rtunes_connection_object) # finds the Course in iTunes U and stores its XML data c.groups # returns an array of Group entities or an empty array of there are no Group entities c.groups = [Group.new(:name => ‘Lectures’)] # assigns the Groups related entity array to an existing array (overwriting any local data about Groups) c.groups << Group.new(:name => ‘Videos’) # Adds the new Group object to the end of hte Groups array c.groups.collect {|g| g.name} # [‘Lectures’, ‘Videos’]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Entity

Creates a new Entity object with attributes based on the hash argument Some of these attributes are assgined to instance variables of the obect (if there is an attr_accessor for it), the rest will be written to a hash of edits that will be saved to iTunes U using method_missing



111
112
113
114
# File 'lib/rtunesu/entity.rb', line 111

def initialize(attrs = {})
  attrs.each {|attribute, value| self.send("#{attribute}=", value)}
  self.source_xml ||= Hpricot.XML('')
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



27
28
29
# File 'lib/rtunesu/entity.rb', line 27

def connection
  @connection
end

#handleObject (readonly) Also known as: id

Returns the value of attribute handle.



28
29
30
# File 'lib/rtunesu/entity.rb', line 28

def handle
  @handle
end

#parentObject

Returns the parent of the entity



146
147
148
# File 'lib/rtunesu/entity.rb', line 146

def parent
  @parent
end

#parent_handleObject

Returns the handle of the entitiy’s parent. This can either be set directly as a string or interger or will access the parent entity. Sometimes you know the parent_handle without the parent object (for example, stored locally from an earlier request). This allows you to add a new Entity to iTunes U without first firing a request for a parent entity (For example, if your institution places all courses inside the same Section, you want to add a new Section to your Site, or a new Group to a course tied to your institution’s LMS).



178
179
180
# File 'lib/rtunesu/entity.rb', line 178

def parent_handle
  @parent_handle
end

#savedObject

Returns the value of attribute saved.



27
28
29
# File 'lib/rtunesu/entity.rb', line 27

def saved
  @saved
end

#source_xmlObject

Returns the value of attribute source_xml.



27
28
29
# File 'lib/rtunesu/entity.rb', line 27

def source_xml
  @source_xml
end

Class Method Details

.attributesObject



30
31
32
# File 'lib/rtunesu/entity.rb', line 30

def self.attributes
  @attributes ||= Set.new
end

.base_connectionObject



38
39
40
# File 'lib/rtunesu/entity.rb', line 38

def self.base_connection 
  Entity.get_base_connection
end

.base_connection=(connection) ⇒ Object



46
47
48
# File 'lib/rtunesu/entity.rb', line 46

def self.base_connection=(connection)
  self.set_base_connection(connection)
end

.composed_of(*names) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rtunesu/entity.rb', line 54

def self.composed_of(*names)
  options = names.extract_options!
  self.attributes.merge(names)
  names.each do |name|
    storage_name = options[:as] || name.to_s.camelize
    
    define_method(name) do
      value_from_edits_or_store(storage_name)
    end
    
    unless options[:readonly]
      define_method(:"#{name}=") do |arg|
        edits[storage_name] = arg
      end
    end
  end
end

.get_base_connectionObject

:nodoc:



34
35
36
# File 'lib/rtunesu/entity.rb', line 34

def self.get_base_connection # :nodoc:
  @base_connection
end

.has_a(*names) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rtunesu/entity.rb', line 72

def self.has_a(*names)
  options = names.extract_options!
  self.attributes.merge(names)
  names.each do |name|
    define_method(name) do
      entity_name = options[:as] || name.to_s.camelize
      edits[entity_name] || RTunesU::HasAEntityCollectionProxy.new_or_nil(self.source_xml./(entity_name), self, entity_name)
    end
    
    unless options[:readonly]
      define_method(:"#{name}=") do |arg|
        entity_name = options[:as] || name.to_s.camelize
        RTunesU::HasAEntityCollectionProxy.new_from_target(arg, self, entity_name)
        #edits[options[:as] || name.to_s.camelize] = arg
      end
    end
  end
end

.has_n(*names) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rtunesu/entity.rb', line 91

def self.has_n(*names)
  options = names.extract_options!
  self.attributes.merge(names)
  names.each do |name|
    define_method(name) do
      entity_name = options[:as] || name.to_s.chop.camelize
      instance_variable_get("@#{name}") || instance_variable_set("@#{name}", RTunesU::HasNEntityCollectionProxy.new(self.source_xml./(entity_name), self, entity_name))
    end
    
    unless options[:readonly]
      define_method(:"#{name}=") do |arg|
        edits[options[:as] || name.to.camelize] = arg
      end
    end
  end
end

.set_base_connection(connection) ⇒ Object



42
43
44
# File 'lib/rtunesu/entity.rb', line 42

def self.set_base_connection(connection)
  @base_connection = connection
end

.validates!(name, values) ⇒ Object



50
51
52
# File 'lib/rtunesu/entity.rb', line 50

def self.validates!(name, values)
  return
end

Instance Method Details

#base_connectionObject



168
169
170
# File 'lib/rtunesu/entity.rb', line 168

def base_connection
  self.class.base_connection
end

#class_nameObject

Returns the name of the object’s class ignoring namespacing.

Use:

course = RTunesU::Course.new course.class #=> ‘RTunesU::Course’ course.class_name #=> ‘Course’



164
165
166
# File 'lib/rtunesu/entity.rb', line 164

def class_name
  self.class.to_s.split('::').last
end

#edited?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/rtunesu/entity.rb', line 135

def edited?
  self.edits.any?
end

#editsObject

Edits stores the changes made to an entity



131
132
133
# File 'lib/rtunesu/entity.rb', line 131

def edits
  @edits ||= {}
end

#handle_from_sourceObject



121
122
123
124
125
126
127
128
# File 'lib/rtunesu/entity.rb', line 121

def handle_from_source
  return nil unless self.source_xml
  if (handle_elem = self.source_xml % 'Handle')
    handle_elem.innerHTML
  else
    nil
  end
end

#inspectObject



192
193
194
195
196
197
198
# File 'lib/rtunesu/entity.rb', line 192

def inspect
  inspected = "#<#{self.class.to_s} handle:#{@handle.inspect} "
  self.class.attributes.each do |attribute|
    inspected << "#{attribute}:#{self.send(attribute).inspect} "
  end
  inspected   << '>'
end

#reloadObject

Clear the edits and restores the loaded object to its original form



140
141
142
143
# File 'lib/rtunesu/entity.rb', line 140

def reload
  self.edits.clear
  self
end

#to_xml(xml_builder = Builder::XmlMarkup.new) ⇒ Object

Converts the entities changed attributes and subentities to XML. Called by Document when building documents to transfer to iTunes U.



184
185
186
187
188
189
190
# File 'lib/rtunesu/entity.rb', line 184

def to_xml(xml_builder = Builder::XmlMarkup.new)
  xml_builder.tag!(self.class_name) do
    self.edits.each do |attribute,edit|
      edit.is_a?(SubentityAssociationProxy) ? edit.to_xml(xml_builder) : xml_builder.tag!(attribute, edit)
    end
  end
end

#value_from_edits_or_store(name) ⇒ Object



152
153
154
155
156
# File 'lib/rtunesu/entity.rb', line 152

def value_from_edits_or_store(name)
  self.edits[name] ||  (self.source_xml % name).innerHTML
rescue NoMethodError
  nil
end