class BitBroker::Metadata

Constants

TYPE_ADVERTISE

describes message types

TYPE_REQUEST
TYPE_REQUEST_ALL
TYPE_SUGGESTION

Attributes

dir[R]

Public Class Methods

new(dir) click to toggle source
# File lib/bitbroker/metadata.rb, line 13
def initialize(dir)
  @dir = dir
  @files = scanning_files(@dir).map do |path|
    FileInfo.new(@dir, get_rpath(path))
  end
end

Public Instance Methods

advertise(broker) click to toggle source

sending message for broker

create(path) click to toggle source
# File lib/bitbroker/metadata.rb, line 26
def create(path)
  if get_with_path(path) == nil
    @files.push(FileInfo.new(@dir, path))
  else
    puts "Warning: #{path} is already created"
  end
end
get_rpath(path) click to toggle source

utility methods

# File lib/bitbroker/metadata.rb, line 61
def get_rpath(path)
  raise DiscomfortDirectoryStructure unless !!path.match(/^#{@dir}/)
  path.split(@dir).last
end
get_with_path(path) click to toggle source
# File lib/bitbroker/metadata.rb, line 20
def get_with_path(path)
  @files.select{|f| f.path == path}.first
end
remove_with_path(path) click to toggle source
# File lib/bitbroker/metadata.rb, line 23
def remove_with_path(path)
  @files.reject!{|f| f.path == path}
end
request(broker, files, dest) click to toggle source
# File lib/bitbroker/metadata.rb, line 53
def request(broker, files, dest)
  broker.send_p_metadata(dest, {
    :type => TYPE_REQUEST,
    :data => files,
  })
end
request_all(broker, files) click to toggle source
# File lib/bitbroker/metadata.rb, line 41
def request_all(broker, files)
  broker.send_metadata({
    :type => TYPE_REQUEST_ALL,
    :data => files,
  })
end
suggestion(broker, files, dest) click to toggle source
# File lib/bitbroker/metadata.rb, line 47
def suggestion(broker, files, dest)
  broker.send_p_metadata(dest, {
    :type => TYPE_SUGGESTION,
    :data => files,
  })
end

Private Instance Methods

scanning_files(current_dir, &block) click to toggle source
# File lib/bitbroker/metadata.rb, line 67
def scanning_files(current_dir, &block)
  arr = []
  Dir.foreach(current_dir) do |f|
    if /^\.+$/ !~ f
      path = "#{current_dir}/#{f}"

      if File.directory? f
        arr += scanning(path)
      else
        arr.push(path)
      end
    end
  end
  arr
end