Module: FakeZip::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/fake_zip.rb

Instance Method Summary collapse

Instance Method Details

#collect_all(given) ⇒ Object



32
33
34
35
36
# File 'lib/fake_zip.rb', line 32

def collect_all(given)
  found = []
  traverse given do |x,y| found << [x,y] end
  found
end

#collect_only(given, kind) ⇒ Object



37
38
39
# File 'lib/fake_zip.rb', line 37

def collect_only(given, kind)
  collect_all(given).map { |(name,type)| name if type == kind }.compact
end

#dirs(given) ⇒ Object



43
44
45
# File 'lib/fake_zip.rb', line 43

def dirs(given)
  collect_only(given, :dir).map { |x| x.join ?/ }
end

#files(given) ⇒ Object



40
41
42
# File 'lib/fake_zip.rb', line 40

def files(given)
  collect_only(given, :file).map { |x| x.join ?/ }
end

#traverse(array_or_hash, path = [], &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fake_zip.rb', line 18

def traverse array_or_hash, path=[], &block
  given = array_or_hash
  case given
  when Hash
    given.each_pair do |k,v|
      block.call path+[k], :dir # entering directory
      traverse v,path+[k],&block
    end
  when Array then given.each { |x| traverse x,path,&block }
  else
    block.call path+[given], :file
  end
end