Skip to content

Commit

Permalink
Tidying CommonPrefix implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjamieson committed Aug 18, 2008
1 parent a9b4f15 commit 138e9de
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/parkplace/s3.rb
Expand Up @@ -67,23 +67,25 @@ def get(bucket_name)
contents = Slot.find :all, opts

if @input.delimiter
@input.prefix = '' if @input.prefix.nil?

# Build a hash of { :prefix => content_key }. The prefix will not include the supplied @input.prefix.
prefixes = contents.inject({}) do |hash, c|
prefix = c.name.sub(@input.prefix || '', '').split(@input.delimiter)[0]
if hash[prefix].nil?
hash[prefix] = 1
else
hash[prefix] += 1
end
prefix = get_prefix(c).to_sym
hash[prefix] = [] unless hash[prefix]
hash[prefix] << c.name
hash
end

# The common prefixes are those with more than one element
common_prefixes = prefixes.inject([]) do |array, prefix|
array << (@input.prefix || '') + prefix[0] if prefix[1] > 1
array << prefix[0].to_s if prefix[1].size > 1
array
end

# The contents are everything that doesn't have a common prefix
contents = contents.reject do |c|
prefix = (@input.prefix || '') + c.name.sub(@input.prefix || '', '').split(@input.delimiter)[0]
common_prefixes.include? prefix
common_prefixes.include? get_prefix(c)
end
end

Expand Down Expand Up @@ -118,6 +120,11 @@ def get(bucket_name)
end
end
end

private
def get_prefix(c)
c.name.sub(@input.prefix, '').split(@input.delimiter)[0] + @input.delimiter
end
end

class RSlot < S3 '/(.+?)/(.+)'
Expand Down

0 comments on commit 138e9de

Please sign in to comment.