Module: RealZip::Helpers

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

Instance Method Summary collapse

Instance Method Details

#collect_all(given) ⇒ Object



34
35
36
37
38
# File 'lib/real_zip.rb', line 34

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

#collect_only(given, kind) ⇒ Object



39
40
41
# File 'lib/real_zip.rb', line 39

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

#dirs(given) ⇒ Object



45
46
47
# File 'lib/real_zip.rb', line 45

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

#files(given) ⇒ Object



42
43
44
# File 'lib/real_zip.rb', line 42

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

#is_file(file) ⇒ Object



49
50
51
# File 'lib/real_zip.rb', line 49

def is_file(file)
  File.file?(file)
end

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



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/real_zip.rb', line 20

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