class Tori::File

Public Class Methods

new(model, title: nil, from: nil, to: nil, &block) click to toggle source
# File lib/tori/file.rb, line 3
def initialize(model, title: nil, from: nil, to: nil, &block)
  @model = model
  @title = title.kind_of?(String) ? title.to_sym : title
  @backend = to
  @filename_callback = block

  self.from = from
end

Public Instance Methods

backend() click to toggle source
# File lib/tori/file.rb, line 54
def backend
  @backend || Tori.config.backend
end
backend=(new_backend) click to toggle source
# File lib/tori/file.rb, line 58
def backend=(new_backend)
  @backend = new_backend
end
delete() click to toggle source
# File lib/tori/file.rb, line 46
def delete
  backend.delete name if exist?
end
filename_callback() click to toggle source
# File lib/tori/file.rb, line 50
def filename_callback
  @filename_callback || Tori.config.filename_callback
end
from() click to toggle source
# File lib/tori/file.rb, line 19
def from
  @from
end
from=(file) click to toggle source
# File lib/tori/file.rb, line 23
def from=(file)
  @from_path = if file.respond_to?(:path)
    file.path
  else
    nil
  end
  @from = if file.respond_to?(:read) and file.respond_to?(:rewind)
    file.rewind
    file.read
  else
    file
  end
end
from?() click to toggle source
# File lib/tori/file.rb, line 37
def from?
  !@from.nil?
end
name() click to toggle source
# File lib/tori/file.rb, line 12
def name
  context = Context.new(@title)
  context.define_singleton_method(:__filename_callback__, filename_callback)
  context.__filename_callback__(@model)
end
Also aliased as: to_s
to_s()
Alias for: name
write(opts = nil) click to toggle source
# File lib/tori/file.rb, line 41
def write(opts = nil)
  opts ||= {}
  backend.write name, @from, opts.merge(from_path: @from_path)
end

Private Instance Methods

method_missing(sym, *args, &block) click to toggle source
# File lib/tori/file.rb, line 68
def method_missing(sym, *args, &block)
  if respond_to_missing?(sym, false)
    backend.__send__ sym, name, *args, &block
  else
    raise NameError, "undefined method `#{sym}' for #{backend}"
  end
end
respond_to_missing?(sym, include_private) click to toggle source
# File lib/tori/file.rb, line 64
def respond_to_missing?(sym, include_private)
  backend.respond_to?(sym, include_private)
end