Sha256: 43c23751e50fb4f01fb05c3aa4d3c0abbcd464b2089a9f9d83cf731d65481fad

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module Ramdo
  class Store
    NAME_PATTERN = /^ramdo_([a-z0-9]+)_([0-9]+)$/

    attr_reader :file, :dir

    def initialize(opts = {})
      list = DiskInstance.list
      disk = nil
      if list.length <= 0
        disk = DiskInstance.create
      else
        disk = list.first
      end

      # Every time a new store is created we check if any other store is out of date
      Cleaner.clean_up(disk)

      ext = opts[:extension] ? opts[:extension].sub('.', '') : 'bin'
      uuid = SecureRandom.hex(4)
      timestamp = Time.now.utc.to_i
      @dir = File.join(disk.path, "ramdo_#{uuid}_#{timestamp}")
      @file = File.join(@dir, "store.#{ext}")

      Dir.mkdir(@dir)

      if opts.has_key?(:data)
        self.data = opts[:data]
      elsif opts.has_key?(:file)
        FileUtils.cp opts[:file], @file
      end
    end

    def data=(data)
      IO.binwrite(@file, data)
    end

    def data
      IO.binread(@file)
    end

    def truncate
      return if @dir.empty? || @dir == File::SEPARATOR # Safety net
      FileUtils.rm_r @dir, :force => true
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ramdo-0.3.2 lib/ramdo/store.rb
ramdo-0.3.1 lib/ramdo/store.rb
ramdo-0.3.0 lib/ramdo/store.rb