Class: Tori::File

Inherits:
Object
  • Object
show all
Defined in:
lib/tori/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, title: nil, from: nil, to: nil, &block) ⇒ File



5
6
7
8
9
10
11
12
# File 'lib/tori/file.rb', line 5

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

Instance Method Details

#backendObject



56
57
58
# File 'lib/tori/file.rb', line 56

def backend
  @backend || Tori.config.backend
end

#backend=(new_backend) ⇒ Object



60
61
62
# File 'lib/tori/file.rb', line 60

def backend=(new_backend)
  @backend = new_backend
end

#deleteObject



48
49
50
# File 'lib/tori/file.rb', line 48

def delete
  backend.delete name if exist?
end

#filename_callbackObject



52
53
54
# File 'lib/tori/file.rb', line 52

def filename_callback
  @filename_callback || Tori.config.filename_callback
end

#fromObject



21
22
23
# File 'lib/tori/file.rb', line 21

def from
  @from
end

#from=(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tori/file.rb', line 25

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?Boolean



39
40
41
# File 'lib/tori/file.rb', line 39

def from?
  !@from.nil?
end

#nameObject Also known as: to_s



14
15
16
17
18
# File 'lib/tori/file.rb', line 14

def name
  context = Context.new(@title)
  context.define_singleton_method(:__filename_callback__, filename_callback)
  context.__filename_callback__(@model)
end

#write(opts = nil) ⇒ Object



43
44
45
46
# File 'lib/tori/file.rb', line 43

def write(opts = nil)
  opts ||= {}
  backend.write name, @from, opts.merge(from_path: @from_path)
end