Skip to content

Commit

Permalink
Add File#add and File#add_aggregate.
Browse files Browse the repository at this point in the history
Also adds write access to the manifest aggregates list.
  • Loading branch information
hainesr committed Jul 4, 2014
1 parent 547fb8c commit 92402d1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/ro-bundle/file.rb
Expand Up @@ -55,5 +55,45 @@ def self.create(filename, mimetype = MIMETYPE, &block)
super(filename, mimetype, &block)
end

# :call-seq:
# add(entry, src_path, options = {}, &continue_on_exists_proc)
#
# Convenience method for adding the contents of a file to the bundle
# file. If asked to add a file with a reserved name, such as the special
# mimetype header file or .ro/manifest.json, this method will raise a
# ReservedNameClashError.
#
# This method automatically adds new entries to the list of bundle
# aggregates unless the <tt>:aggregate</tt> option is set to false.
#
# See the rubyzip documentation for details of the
# +continue_on_exists_proc+ parameter.
def add(entry, src_path, options = {}, &continue_on_exists_proc)
super(entry, src_path, &continue_on_exists_proc)

options = { :aggregate => true }.merge(options)

if options[:aggregate]
@manifest.add_aggregate(entry)
end
end

# :call-seq:
# add_aggregate(entry, &continue_on_exists_proc)
# add_aggregate(entry, src_path, &continue_on_exists_proc)
#
# The first form of this method adds an already existing entry in the
# bundle to the list of aggregates. <tt>Errno:ENOENT</tt> is raised if the
# entry does not exist.
#
# The second form is equivalent to File#add called without any options.
def add_aggregate(entry, src_path = nil, &continue_on_exists_proc)
if src_path.nil?
@manifest.add_aggregate(entry)
else
add(entry, src_path, &continue_on_exists_proc)
end
end

end
end
15 changes: 15 additions & 0 deletions lib/ro-bundle/ro/manifest.rb
Expand Up @@ -128,6 +128,21 @@ def aggregates
structure[:aggregates].dup
end

# :call-seq:
# add_aggregate(entry)
#
# Add the given entry to the list of aggregates in this manifest.
# <tt>Errno:ENOENT</tt> is raised if the entry does not exist.
def add_aggregate(entry)
raise Errno::ENOENT if container.find_entry(entry).nil?

# Mangle the filename according to the RO Bundle specification.
name = "/#{entry_name(entry)}"

@edited = true
structure[:aggregates] << Aggregate.new(name)
end

# :call-seq:
# annotations
#
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/list_tests.rb
@@ -0,0 +1,16 @@
#------------------------------------------------------------------------------
# Copyright (c) 2014 The University of Manchester, UK.
#
# BSD Licenced. See LICENCE.rdoc for details.
#
# Author: Robert Haines
#------------------------------------------------------------------------------

# Is the given file aggregate in the list of aggregate objects
def file_aggregate_in_list(agg, list)
list.each do |e|
return true if e.file == "/#{agg}"
end

false
end
34 changes: 34 additions & 0 deletions test/tc_create.rb
Expand Up @@ -10,6 +10,8 @@
require "tmpdir"
require "ro-bundle"

require "helpers/list_tests"

class TestCreation < Test::Unit::TestCase

def test_create_empty_bundle
Expand Down Expand Up @@ -42,4 +44,36 @@ def test_create_empty_bundle
end
end

def test_add_aggregates
Dir.mktmpdir do |dir|
filename = File.join(dir, "test.bundle")

entry1 = "test1.json"
entry2 = "test2.json"
entry3 = "test3.json"

assert_nothing_raised do
ROBundle::File.create(filename) do |b|
assert b.aggregates.empty?
assert_nil b.find_entry(entry1)

b.add(entry1, $man_ex3, :aggregate => false)
assert b.aggregates.empty?
assert_not_nil b.find_entry(entry1)

b.add(entry2, $man_ex3)
assert file_aggregate_in_list(entry2, b.aggregates)
assert_not_nil b.find_entry(entry2)

b.add_aggregate(entry3, $man_ex3)
assert file_aggregate_in_list(entry3, b.aggregates)
assert_not_nil b.find_entry(entry3)

b.add_aggregate(entry1)
assert file_aggregate_in_list(entry1, b.aggregates)
end
end
end
end

end

0 comments on commit 92402d1

Please sign in to comment.