Class: DistributedFile

Inherits:
Object
  • Object
show all
Includes:
DistributedShelf
Defined in:
lib/dshelf/dfile.rb

Constant Summary

Constants included from DistributedShelf

DistributedShelf::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DistributedShelf

#absolutepath, config=, #distributed?, #override_class_method, #proxy_method, #server_url

Constructor Details

#initialize(filename, mode = 'r', *args, &block) ⇒ DistributedFile

“r” | Read-only, starts at beginning of file (default mode). —–+——————————————————– “r+” | Read-write, starts at beginning of file. —–+——————————————————– “w” | Write-only, truncates existing file

|  to zero length or creates a new file for writing.

—–+——————————————————– “w+” | Read-write, truncates existing file to zero length

|  or creates a new file for reading and writing.

—–+——————————————————– “a” | Write-only, starts at end of file if file exists,

|  otherwise creates a new file for writing.

—–+——————————————————– “a+” | Read-write, starts at end of file if file exists,

|  otherwise creates a new file for reading and
|  writing.

—–+——————————————————–

"b" |  Binary file mode (may appear with
    |  any of the key letters listed above).
    |  Suppresses EOL <-> CRLF conversion on Windows. And
    |  sets external encoding to ASCII-8BIT unless explicitly
    |  specified.

—–+——————————————————–

"t" |  Text file mode (may appear with
    |  any of the key letters listed above except "b").


33
34
35
36
# File 'lib/dshelf/dfile.rb', line 33

def initialize filename, mode='r', *args, &block
  @filename = filename
  @mode = mode
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



130
131
132
# File 'lib/dshelf/dfile.rb', line 130

def method_missing method
  raise "Distributed File error: #{method} not implemented"
end

Class Method Details

.open(filename, *args, &b) ⇒ Object

File.open(filename, mode=“r” [, opt]) => file File.open(filename [, mode [, perm]] [, opt]) => file File.open(filename, mode=“r” [, opt]) {|file| block } => obj File.open(filename [, mode [, perm]] [, opt]) {|file| block } => obj



120
121
122
123
124
125
126
127
128
# File 'lib/dshelf/dfile.rb', line 120

def self.open filename, *args, &b
  f = self.new filename, *args
  if b
    b.call(f)
    f.close
  else
    f
  end
end

Instance Method Details

#closeObject



58
59
60
# File 'lib/dshelf/dfile.rb', line 58

def close
  # p "#{path}: closing"
end

#mimetypeObject



42
43
44
45
46
47
48
49
50
# File 'lib/dshelf/dfile.rb', line 42

def mimetype
  return @mimetype if @mimetype
  @mimetype = MIME::Types.type_for(File.basename path)
  @mimetype = if @mimetype.empty?
    "application/octet-stream"
  else
    @mimetype[0]
  end
end

#pathObject



38
39
40
# File 'lib/dshelf/dfile.rb', line 38

def path
  @filename
end

#puts(*args) ⇒ Object



100
101
102
# File 'lib/dshelf/dfile.rb', line 100

def puts *args
  write args.join("\n")
end

#read(length = 0, offset = 0) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dshelf/dfile.rb', line 74

def read length=0, offset=0
#   if RUBY_VERSION >= '1.8.7' then
#     # stream
#     Stream.new "#{server_url}/storage#{URI.escape absolutepath}"
#   else
#     read_full length, offset
#   end
# end
# 
# def read_full length=0, offset=0
  params = {}
  params[:length] = length unless length == 0
  params[:offset] = offset unless offset == 0
  RestClient.get("#{server_url}/storage#{URI.escape absolutepath}",
    {:params => params}) do |response, request, result|
    case response.code
    when 200
      response
    when 402
      raise Errno::ENOSPC
    when 404
      raise Errno::ENOENT
    end
  end
end

#write(string) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/dshelf/dfile.rb', line 104

def write string
  RestClient.put "#{server_url}/storage#{URI.escape absolutepath}", string, :content_type => mimetype do |response, request, result|
    case response.code
    when 204
      string.size
    when 402
      raise Errno::ENOSPC
    end
  end
end