Class: DistributedFile
- Inherits:
-
Object
- Object
- DistributedFile
- Includes:
- DistributedShelf
- Defined in:
- lib/dshelf/dfile.rb
Constant Summary
Constants included from DistributedShelf
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(filename, mode = 'r', *args, &block) ⇒ DistributedFile
constructor
“r” | Read-only, starts at beginning of file (default mode).
- #method_missing(method) ⇒ Object
- #mimetype ⇒ Object
- #path ⇒ Object
- #puts(*args) ⇒ Object
- #read(length = 0, offset = 0) ⇒ Object
- #read_full(length = 0, offset = 0) ⇒ Object
- #write(string) ⇒ Object
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").
32 33 34 35 |
# File 'lib/dshelf/dfile.rb', line 32 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
128 129 130 |
# File 'lib/dshelf/dfile.rb', line 128 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
118 119 120 121 122 123 124 125 126 |
# File 'lib/dshelf/dfile.rb', line 118 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
#close ⇒ Object
57 58 59 |
# File 'lib/dshelf/dfile.rb', line 57 def close # p "#{path}: closing" end |
#mimetype ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/dshelf/dfile.rb', line 41 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 |
#path ⇒ Object
37 38 39 |
# File 'lib/dshelf/dfile.rb', line 37 def path @filename end |
#puts(*args) ⇒ Object
98 99 100 |
# File 'lib/dshelf/dfile.rb', line 98 def puts *args write args.join("\n") end |
#read(length = 0, offset = 0) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/dshelf/dfile.rb', line 73 def read length=0, offset=0 # if RUBY_VERSION >= '1.8.7' then # Stream.new "#{server_url}/storage#{URI.escape absolutepath}" # else read_full length, offset # end end |
#read_full(length = 0, offset = 0) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dshelf/dfile.rb', line 81 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, absolutepath end end end |
#write(string) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dshelf/dfile.rb', line 102 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 |