Class: CineSync::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/cinesync/xml.rb,
lib/cinesync/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



7
8
9
10
11
12
13
# File 'lib/cinesync/session.rb', line 7

def initialize
  @file_version = SessionV3XMLFileVersion
  @user_data = ''
  @media = []
  @groups = []
  @notes = ''
end

Instance Attribute Details

#chat_elemObject

Returns the value of attribute chat_elem.



5
6
7
# File 'lib/cinesync/session.rb', line 5

def chat_elem
  @chat_elem
end

#file_versionObject (readonly)

Returns the value of attribute file_version.



3
4
5
# File 'lib/cinesync/session.rb', line 3

def file_version
  @file_version
end

#groupsObject

Returns the value of attribute groups.



4
5
6
# File 'lib/cinesync/session.rb', line 4

def groups
  @groups
end

#mediaObject

Returns the value of attribute media.



4
5
6
# File 'lib/cinesync/session.rb', line 4

def media
  @media
end

#notesObject

Returns the value of attribute notes.



4
5
6
# File 'lib/cinesync/session.rb', line 4

def notes
  @notes
end

#stereo_elemObject

Returns the value of attribute stereo_elem.



5
6
7
# File 'lib/cinesync/session.rb', line 5

def stereo_elem
  @stereo_elem
end

#user_dataObject

Returns the value of attribute user_data.



4
5
6
# File 'lib/cinesync/session.rb', line 4

def user_data
  @user_data
end

Class Method Details

.load(str_or_io, silent = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cinesync/xml.rb', line 43

def self.load(str_or_io, silent = false)
  doc = REXML::Document.new(str_or_io)

  # Do a few checks (the user should have already confirmed that the
  # document conforms to the schema, but we'll try to fail early in case)
  fail 'Expected to find root <session> element' unless doc.root.name == 'session'
  fail %Q[Root <session> element must have attribute xmlns="#{SessionV3Namespace}"] unless doc.root.attribute('xmlns').value == SessionV3Namespace

  doc_version = doc.root.attribute('version').value.to_i
  if doc_version > SessionV3XMLFileVersion
    $stderr.puts("Warning: Loading session file with a newer version (#{doc_version}) than this library (#{SessionV3XMLFileVersion})") unless silent
  end

  elem = doc.root

  returning self.new do |s|
    s.instance_variable_set(:@file_version, doc_version)

    s.user_data = elem.attribute('userData').andand.value || ''
    s.groups = elem.get_elements('group').map {|g_elem| g_elem.text }
    s.notes = elem.elements['notes'].andand.text || ''
    s.chat_elem = elem.get_elements('chat').andand[0]
    s.stereo_elem = elem.get_elements('stereo').andand[0]
    s.media = elem.get_elements('media').map {|e| MediaBase.load(e) }
  end
end

Instance Method Details

#session_featuresObject



15
16
17
# File 'lib/cinesync/session.rb', line 15

def session_features
  (stereo_elem or media.any? {|m| m.uses_pro_features? }) ? :pro : :standard
end

#to_xmlObject

eSession = element session {

attribute version { xsd:integer { minInclusive = "3" } } &
attribute sessionFeatures { "standard" | "pro" } &
aUserData? &
eGroup* &
eNotes? &
eChat? &
eMedia* }


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cinesync/xml.rb', line 20

def to_xml
  fail "#{self.inspect}: Invalid" unless valid?

  x = Builder::XmlMarkup.new(:indent => 4)
  x.instruct!

  attrs = {}
  attrs['xmlns'] = SessionV3Namespace
  # Always write as the version we know, not the version we loaded
  attrs['version'] = SessionV3XMLFileVersion
  attrs['sessionFeatures'] = session_features
  attrs['userData'] = user_data unless user_data.empty?

  x.session(attrs) {
    groups.each {|g| x.group(g) }
    x.notes(notes) unless notes.empty?
    x << chat_elem.to_s if chat_elem
    x << stereo_elem.to_s if stereo_elem
    media.each {|m| m.to_xml(x) }
  }
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/cinesync/session.rb', line 19

def valid?
  file_version == SessionV3XMLFileVersion and
  media.all? {|m| m.valid? }
end