Sha256: f9f4b08c11e4aa3bd1d5721b99974143fb8e3d8cb8635609d2fe0c22c5fe842d

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Tori
  class File
    def initialize(model, title: nil, from: nil, &block)
      @model = model
      @title = title.kind_of?(String) ? title.to_sym : title
      if from.respond_to?(:read) and from.respond_to?(:rewind)
        from.rewind
        @from = from.read
      else
        @from = from
      end
      @filename_callback = block
    end

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

    def from?
      !@from.nil?
    end

    def read
      Tori.config.backend.read name
    end

    def write(opts = nil)
      Tori.config.backend.write name, @from, opts
    end

    def delete
      Tori.config.backend.delete name if exist?
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tori-0.4.1 lib/tori/file.rb