Class: Ruber::DocumentProject

Inherits:
AbstractProject show all
Defined in:
lib/ruber/document_project.rb

Defined Under Namespace

Classes: Backend

Instance Attribute Summary collapse

Attributes inherited from AbstractProject

#project_file, #project_name

Instance Method Summary collapse

Methods inherited from AbstractProject

#[]=, #add_extension, #close, #each_extension, #extension, #extensions, #has_extension?, #method_missing, #query_close, #remove_extension, #save

Methods included from SettingsContainer

#[], #[]=, #add_setting, #add_widget, #default, #dialog, #has_setting?, #relative_path?, #remove_setting, #remove_widget

Constructor Details

#initialize(doc) ⇒ DocumentProject

TODO:

in classes derived from Qt::Object, korundum executes the code in initialize,

Creates a new DocumentProject. doc is the document the project refers to. Note that, until doc becomes associated with a file, attempting to save the project will fail with an ArgumentError.

If the path of the file associated with the document changes (usually because of a “Save As” action), the file associated with the backend is changed automatically

up until the call to super twice. This means that two Backend items will be created. See if something can be done to avoid it. I don’t know whether this has any bad consequence or not.



137
138
139
140
141
142
143
# File 'lib/ruber/document_project.rb', line 137

def initialize doc
  @document = doc
  path = backend_file
  back = Backend.new path
  !File.exist?(back.file) ? super(doc, back, path) : super(doc, back)
  connect doc, SIGNAL('document_url_changed(QObject*)'), self, SLOT(:change_file)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ruber::AbstractProject

Instance Attribute Details

#documentObject (readonly)

The document associated with the project



122
123
124
# File 'lib/ruber/document_project.rb', line 122

def document
  @document
end

Instance Method Details

#filesObject

Override of AbstractProject#files which returns an array with the path of the associated document, if it corresponds to a file, and an empty array otherwise



200
201
202
203
204
205
206
207
208
# File 'lib/ruber/document_project.rb', line 200

def files
  url =  @document.url
  if url.local_file?
    path = url.path
  else 
    path = url.to_encoded(Qt::Url::RemoveUserInfo|Qt::Url::RemovePort|Qt::Url::RemoveFragment).to_s
  end
  path.empty? ? [] : [path] 
end

#match_rule?(obj) ⇒ Boolean

Override of AbstractProject#match_rule? which also takes into account the mimetype and the file extension of the document and compares them with those in the rule. The comparison is made using Document#file_type_match?. This method returns true only if the Document#file_type_match? returns true and the rule’s scope includes :document

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
# File 'lib/ruber/document_project.rb', line 159

def match_rule? obj
  if !super then false
  elsif !obj.place.include?(@document.url.local_file? ? :local : :remote) 
    false
  elsif !@document.file_type_match? obj.mimetype, obj.file_extension then false
  else true
  end
end

#project_directoryObject Also known as: project_dir

Override of AbstractProject#project_directory which returns the current directory if the document isn’t associated with a file.



172
173
174
175
# File 'lib/ruber/document_project.rb', line 172

def project_directory
  path = @document.path
  path.empty? ? Dir.pwd : File.dirname(path) 
end

#scopeObject

Override of AbstractProject#scope which returns :document



148
149
150
# File 'lib/ruber/document_project.rb', line 148

def scope
  :document
end

#writeObject

Override of AbstractProject#write which prevents a Errno::ENOENT exception to be raised by the backend if the document isn’t associated with a file. If the document is associated with a file, however, the exception will be raised as usual.

The reason for this kind of behaviour is that the backend is expected to raise the exception when the document isn’t associated with a file: it simply means that it doesn’t know where to write the data. If the document is associated with a file, however, this shouldn’t happen and the exception is then propagated because it truly means something is wrong.



189
190
191
192
193
194
# File 'lib/ruber/document_project.rb', line 189

def write
  begin super
  rescue Errno::ENOENT
    raise unless @document.path.empty?
  end
end