Module: IPFS::Upload::TreeWalker

Defined in:
lib/ipfs-api/upload.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.depth_first(nodes) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ipfs-api/upload.rb', line 136

def depth_first nodes
  nodes = [ nodes ] if not nodes.is_a?(Array)
  stack = [ [ nodes.shift, 0 ] ]
  Enumerator.new do |yielder|
    while not stack.empty?
      node, depth = stack.pop
      node = resolve_node(node)
      next if not node
      yielder << [ node, depth ]
      if node.folder?
        stack += node.children.reverse.map { |n| [ n, depth + 1 ] }
      end
      if stack.empty? and not nodes.empty?
        stack << [ nodes.shift, 0 ]
      end
    end
  end
end

.resolve_node(node) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ipfs-api/upload.rb', line 155

def resolve_node node
  if node.is_a?(Node)
    node
  elsif node.is_a?(Dir)
    FolderNode.new(File.basename(node.path)) do |d|
      node.each do |child|
        if child != '.' and child != '..'
          child_path = File.join(node.path, child)
          if File.directory?(child_path)
            d.add_node resolve_node(Dir.new(child_path))
          else
            d.add_node resolve_node(File.new(child_path))
          end
        end
      end
    end
  elsif node.is_a?(File)
    FileNode.new(File.basename(node.path)) do |d|
      d.write File.read(node.path)
    end
  end
end