Sha256: 7346d82a4fbacbee4432533c92fdf356046a26750ef6a727088419fd7008fa1f

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

module Tori
  class File
    def initialize(model, title: nil, from: nil, to: nil, &block)
      @model = model
      @title = title.kind_of?(String) ? title.to_sym : title

      @from_path = if from.respond_to?(:path)
        from.path
      else
        nil
      end

      @backend = to

      @from = if from.respond_to?(:read) and from.respond_to?(:rewind)
        from.rewind
        from.read
      else
        from
      end
      @filename_callback = block
    end

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

    def from?
      !@from.nil?
    end

    def read
      backend.read name
    end

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

    def delete
      backend.delete name if exist?
    end

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

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

    private

    def respond_to_missing?(sym, include_private)
      backend.respond_to?(sym, include_private)
    end

    def method_missing(sym, *args)
      if respond_to_missing?(sym, false)
        backend.__send__ sym, name, *args
      else
        fail NameError, "undefined method `#{sym}' for #{Tori.config.backend.inspect}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tori-0.6.1 lib/tori/file.rb
tori-0.6.0 lib/tori/file.rb
tori-0.5.0 lib/tori/file.rb