Skip to content

Commit

Permalink
Make documents into directories for now. Zip files are too complicate…
Browse files Browse the repository at this point in the history
…d to deal with right at the moment (but see zipfile branch). [#4]
  • Loading branch information
marnen committed Nov 25, 2009
1 parent fd0148a commit 725a45f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 35 deletions.
86 changes: 66 additions & 20 deletions spec/document/document_model_spec.rb
@@ -1,43 +1,89 @@
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/../../src/document/document_model'
require 'tmpdir'

describe DocumentModel do
before :each do
@tmpdir = Dir.mktmpdir 'DocumentModel'
end

describe "constructor" do
before :each do
@model = DocumentModel.new
end
it "should set the data object to a kind of TableModel" do
@model = DocumentModel.new File.join(@tmpdir, 'constructor')
@model.data.class.included_modules.should include(javax.swing.table.TableModel)
end

it "should set the title" do
@model.title.should_not be_nil
it "should have a zero-argument form" do
lambda {DocumentModel.new}.should_not raise_error(ArgumentError)
end

it "should set the data object to a kind of TableModel" do
@model.data.class.included_modules.should include(javax.swing.table.TableModel)
it "should take a filename as argument" do
lambda {DocumentModel.new File.join(@tmpdir, 'foo')}.should_not raise_error(ArgumentError)
end

it "should create a new folder with the specified filename" do
filename = File.join(@tmpdir, 'some crazy filename')
File.should_not exist(filename)
DocumentModel.new filename
File.should exist(filename)
File.stat(filename).should be_directory
end

it "should put an H2 database into the folder it creates" do
pending "Does this really need to happen at creation time? \
It's probably OK if the DB springs into existence when the first table is added." do
filename = File.join(@tmpdir, 'test file')
@model = DocumentModel.new filename
Dir[File.join filename, '*.h2.db'].should_not be_empty
end
end
end

describe "instance methods" do
before :each do
@model = DocumentModel.new
end

it "should have a title attribute (read/write)" do
@model.should respond_to(:title)
@model.should respond_to(:title=)
it "should have a data attribute (read/write)" do
@model.should respond_to(:data)
@model.should respond_to(:data=)
end

it "should have a count writer" do
@model.should respond_to(:count=)
end
describe "filename" do
it "should be valid for reading and writing" do
@model.should respond_to(:filename)
@model.should respond_to(:filename=)
end

it "should incorporate the count in the title" do
@model.title.should == 'Document-1'
@model.count = 25
@model.title.should == 'Document-25'
it "should raise an error if an existing filename is changed" do
filename = File.join @tmpdir, 'foo'
@model.filename.should be_nil
@model.filename = filename
@model.filename.should == filename
lambda {@model.filename = File.join @tmpdir, 'bar'}.should raise_error
end
end

it "should have a data attribute (read/write)" do
@model.should respond_to(:data)
@model.should respond_to(:data=)
describe "title" do
it "should be valid" do
@model.should respond_to(:title)
end

it "should return the basename of the filename" do
name = 'an arbitrary filename'
subdirectory = 'an arbitrary subdirectory'
Dir.mkdir File.join(@tmpdir, subdirectory)
@model.filename = File.join @tmpdir, subdirectory, name
@model.title.should == name
end

it "should return nil if the filename is nil" do
@model.title.should be_nil
end
end
end

after :each do
FileUtils.rm_r @tmpdir
end
end
3 changes: 2 additions & 1 deletion src/application_controller.rb
@@ -1,4 +1,5 @@
class ApplicationController < Monkeybars::Controller
require 'tmpdir'
require 'document_controller'

# Add content here that you want to be available to all the controllers
Expand All @@ -19,7 +20,7 @@ def reset
# Starts the application.
def startup
# TODO: stop opening this wasteful document at startup!
DocumentController.instance.open
DocumentController.create_instance(File.join Dir.mktmpdir('hive'), '*scratch*').open
end

# Displays a file chooser dialog, and returns the full pathname of the selected file, or nil if the dialog was cancelled.
Expand Down
11 changes: 5 additions & 6 deletions src/document/document_controller.rb
@@ -1,8 +1,9 @@
class DocumentController < ApplicationController
set_model 'DocumentModel' do |model|
# Workaround since {DocumentModel.new @@count} doesn't work -- see http://kenai.com/jira/browse/MONKEYBARS-15
model.title = @title
# Workaround since {DocumentModel.new @filename} doesn't work -- see http://kenai.com/jira/browse/MONKEYBARS-15
model.filename = @filename
end

set_view 'DocumentView'
set_close_action :exit

Expand All @@ -14,9 +15,7 @@ def file_new_menu_item_action_performed
def self.create_instance(filename = nil)
filename ||= choose_file

File.open filename, 'a' do |file|
@title = File.basename filename
super()
end
@filename = filename
super()
end
end
36 changes: 28 additions & 8 deletions src/document/document_model.rb
Expand Up @@ -2,23 +2,43 @@ class DocumentModel
require 'data_table_model'

load_gettext
# Window title.
attr_accessor :title
# Structure holding the actual data.
attr_accessor :data
# Name of the associated file.
attr_reader :filename

def initialize
set_title_with_number 1
def initialize(filename = nil)
@data = DataTableModel.new
if filename
self.filename = filename
end
end

def count=(number)
set_title_with_number number
# Sets the filename, but only if it's nil.
# This is a workaround for Monkeybars bug 15 (see DocumentController) -- we can remove it when that bug is fixed.
def filename=(string)
if filename.nil?
@filename = string
create_file @filename
return @filename
else
# TODO: choose a type for this exception
raise "Can't change filename on DocumentModel!"
end
end

# Returns the document title (for example, for window title bar).
# This will normally be the basename of the associated file.
def title
filename.nil? ? nil : File.basename(filename)
end

protected

def set_title_with_number(number)
@title = _('Document-%{num}') % {:num => number}
# Create the document as a directory.
def create_file(filename)
# See zipfile branch in repository for zip file creation code.
Dir.mkdir filename
# TODO: Handle case where directory can't be created.
end
end

0 comments on commit 725a45f

Please sign in to comment.