Class: Bulldog::Stream::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bulldog/stream.rb

Direct Known Subclasses

ForFile, ForIO, ForMissingFile, ForSavedFile, ForTempfile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Base

Returns a new instance of Base.



30
31
32
# File 'lib/bulldog/stream.rb', line 30

def initialize(target)
  @target = target
end

Instance Attribute Details

#targetObject (readonly)

The underlying object.



37
38
39
# File 'lib/bulldog/stream.rb', line 37

def target
  @target
end

Instance Method Details

#content_typeObject

Return the mime-type of the content.



77
78
79
# File 'lib/bulldog/stream.rb', line 77

def content_type
  @content_type ||= `file --brief --mime #{path}`.strip
end

#file_nameObject

Return the original file name of the underlying object. This is the basename of the file as the user uploaded it (for an uploaded file), or the file on the filesystem (for a File object). For other IO objects, return nil.



59
60
61
62
63
64
65
# File 'lib/bulldog/stream.rb', line 59

def file_name
  if @target.respond_to?(:original_path)
    @target.original_path
  else
    nil
  end
end

#missing?Boolean

Return true if this stream represents a missing file.

Returns:

  • (Boolean)


49
50
51
# File 'lib/bulldog/stream.rb', line 49

def missing?
  false
end

#pathObject

Return the path of a file where the content can be found.



42
43
44
# File 'lib/bulldog/stream.rb', line 42

def path
  @target.path
end

#reloadObject

Reload data from the target.



95
96
97
# File 'lib/bulldog/stream.rb', line 95

def reload
  @content_type = nil
end

#sizeObject

Return the size of the content.



70
71
72
# File 'lib/bulldog/stream.rb', line 70

def size
  File.size(path)
end

#write_to(path) ⇒ Object

Write the content to the given path.



84
85
86
87
88
89
90
# File 'lib/bulldog/stream.rb', line 84

def write_to(path)
  src, dst = self.path, path
  unless src == dst
    FileUtils.mkdir_p File.dirname(path)
    FileUtils.cp src, dst
  end
end