Class: Atomic::Atompub::Workspace

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/atomic/atompub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#deserialize, included

Constructor Details

#initialize(params = {}) ⇒ Workspace

Returns a new instance of Workspace.



98
99
100
101
102
103
# File 'lib/atomic/atompub.rb', line 98

def initialize(params = {})
  params.symbolize_keys!
  params.assert_valid_keys(:title, :collections)
  self.title = params[:title]
  self.collections = params[:collections] || []
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



96
97
98
# File 'lib/atomic/atompub.rb', line 96

def collections
  @collections
end

#titleObject

Returns the value of attribute title.



96
97
98
# File 'lib/atomic/atompub.rb', line 96

def title
  @title
end

Instance Method Details

#handle_close_element(node) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/atomic/atompub.rb', line 119

def handle_close_element(node)
  case [node.depth, node.uri, node.name]
  when [0, NS_APP, 'workspace']
  when [1, NS_ATOM, 'title']
    self.title = node.text
  when [1, NS_APP, 'collection']
  else
    puts("Workspace ==> Unexpected Close Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect} #{node.text}")
  end
end

#handle_open_element(node, reader) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/atomic/atompub.rb', line 105

def handle_open_element(node, reader)
  progressed = false
  case [node.depth, node.uri, node.name]
  when [0, NS_APP, 'workspace']
  when [1, NS_ATOM, 'title']
  when [1, NS_APP, 'collection']
    self.collections << Collection.new.deserialize(reader)
    progressed = true
  else
    puts("Workspace ==> Unexpected Open Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect}")
  end
  return progressed
end

#to_hashObject



130
131
132
133
134
135
# File 'lib/atomic/atompub.rb', line 130

def to_hash
  {
    :title => self.title,
    :collections => self.collections.collect{ |collection| collection.to_hash }
  }
end

#to_xml(as_document = true, target = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/atomic/atompub.rb', line 137

def to_xml(as_document=true, target=nil)
  nsdec = as_document ? {:"xmlns:atom" => NS_ATOM, :"xmlns:app" => NS_APP} : {}
  xml = Builder::XmlMarkup.new(:target => target)
  xml.instruct! if as_document
  xml.app(:workspace, nsdec) do
    xml.atom(:title, self.title)
    self.collections.each do |collection|
      collection.to_xml(false, xml)
    end
  end
  xml.target!
end