Class: SakaiInfo::Calendar

Inherits:
SakaiXMLEntity show all
Defined in:
lib/sakai-info/calendar.rb

Direct Known Subclasses

MissingCalendar

Instance Attribute Summary collapse

Attributes inherited from SakaiXMLEntity

#attributes, #properties, #xmldoc

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SakaiXMLEntity

#attributes_serialization, #dbrow_serialization, #parse_xml, #properties_serialization

Methods included from ModProps

included

Methods inherited from SakaiObject

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

Constructor Details

#initialize(dbrow) ⇒ Calendar

Returns a new instance of Calendar.



21
22
23
24
25
26
27
# File 'lib/sakai-info/calendar.rb', line 21

def initialize(dbrow)
  @dbrow = dbrow
  @id = dbrow[:calendar_id]
  @next_id = dbrow[:next_id]

  parse_xml
end

Instance Attribute Details

#dbrowObject (readonly)

Returns the value of attribute dbrow.



14
15
16
# File 'lib/sakai-info/calendar.rb', line 14

def dbrow
  @dbrow
end

#next_idObject (readonly)

Returns the value of attribute next_id.



14
15
16
# File 'lib/sakai-info/calendar.rb', line 14

def next_id
  @next_id
end

Class Method Details

.all_serializationsObject



109
110
111
112
113
114
115
116
# File 'lib/sakai-info/calendar.rb', line 109

def self.all_serializations
  [
   :default,
   :xml,
   :mod,
   :events,
  ]
end

.clear_cacheObject



16
17
18
# File 'lib/sakai-info/calendar.rb', line 16

def self.clear_cache
  @@cache = {}
end

.find(id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sakai-info/calendar.rb', line 29

def self.find(id)
  # if there's no slash in the id string, assume it's a site id
  if id !~ /\//
    id = "/calendar/calendar/#{id}/main"
  end

  if @@cache[id].nil?
    xml = ""
    row = DB.connect[:calendar_calendar].where(:calendar_id => id).first
    if row.nil?
      raise ObjectNotFoundException.new(Calendar, id)
    end
    @@cache[id] = Calendar.new(row)
  end
  @@cache[id]
end

.find!(id) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/sakai-info/calendar.rb', line 46

def self.find!(id)
  begin
    Calendar.find(id)
  rescue ObjectNotFoundException => e
    if e.classname == Calendar.name
      MissingCalendar.find(id)
    end
  end
end

Instance Method Details

#default_serializationObject

serialization



82
83
84
85
86
87
88
89
# File 'lib/sakai-info/calendar.rb', line 82

def default_serialization
  {
    "id" => self.id,
    "site" => self.site.serialize(:summary),
    "next_id" => self.next_id,
    "event_count" => self.event_count,
  }
end

#event_countObject



61
62
63
# File 'lib/sakai-info/calendar.rb', line 61

def event_count
  @event_count ||= CalendarEvent.count_by_calendar_id(@id)
end

#eventsObject

lazy properties



57
58
59
# File 'lib/sakai-info/calendar.rb', line 57

def events
  @events ||= CalendarEvent.find_by_calendar_id(@id)
end

#events_serializationObject



103
104
105
106
107
# File 'lib/sakai-info/calendar.rb', line 103

def events_serialization
  {
    "events" => self.events.collect{|e|e.serialize(:summary)}
  }
end

#siteObject



69
70
71
# File 'lib/sakai-info/calendar.rb', line 69

def site
  @site ||= Site.find(self.site_id)
end

#site_idObject



65
66
67
# File 'lib/sakai-info/calendar.rb', line 65

def site_id
  @site_id ||= @id.split("/")[3]
end

#summary_serializationObject



91
92
93
94
95
# File 'lib/sakai-info/calendar.rb', line 91

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

#xmlObject



73
74
75
76
77
78
79
# File 'lib/sakai-info/calendar.rb', line 73

def xml
  if @xml.nil?
    @xml = ""
    REXML::Document.new(@dbrow[:xml].read).write(@xml, 2)
  end
  @xml
end

#xml_serializationObject



97
98
99
100
101
# File 'lib/sakai-info/calendar.rb', line 97

def xml_serialization
  {
    "xml" => self.xml
  }
end