Class: BitBroker::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbroker/metadata.rb

Defined Under Namespace

Classes: FileInfo

Constant Summary collapse

TYPE_ADVERTISE =

describes message types

1<<0
TYPE_REQUEST_ALL =
1<<1
TYPE_SUGGESTION =
1<<2
TYPE_REQUEST =
1<<3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Metadata

Returns a new instance of Metadata.



13
14
15
16
17
18
# 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

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



11
12
13
# File 'lib/bitbroker/metadata.rb', line 11

def dir
  @dir
end

Instance Method Details

sending message for broker



35
36
37
38
39
40
# File 'lib/bitbroker/metadata.rb', line 35

def advertise(broker)
  broker.({
    :type => TYPE_ADVERTISE,
    :data => @files.map{|x| x.to_h },
  })
end

#create(path) ⇒ Object



26
27
28
29
30
31
32
# 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) ⇒ Object

utility methods



61
62
63
64
# 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) ⇒ Object



20
21
22
# File 'lib/bitbroker/metadata.rb', line 20

def get_with_path(path)
  @files.select{|f| f.path == path}.first
end

#remove_with_path(path) ⇒ Object



23
24
25
# File 'lib/bitbroker/metadata.rb', line 23

def remove_with_path(path)
  @files.reject!{|f| f.path == path}
end

#request(broker, files, dest) ⇒ Object



53
54
55
56
57
58
# File 'lib/bitbroker/metadata.rb', line 53

def request(broker, files, dest)
  broker.(dest, {
    :type => TYPE_REQUEST,
    :data => files,
  })
end

#request_all(broker, files) ⇒ Object



41
42
43
44
45
46
# File 'lib/bitbroker/metadata.rb', line 41

def request_all(broker, files)
  broker.({
    :type => TYPE_REQUEST_ALL,
    :data => files,
  })
end

#suggestion(broker, files, dest) ⇒ Object



47
48
49
50
51
52
# File 'lib/bitbroker/metadata.rb', line 47

def suggestion(broker, files, dest)
  broker.(dest, {
    :type => TYPE_SUGGESTION,
    :data => files,
  })
end