Sha256: 73c0f7dd602c9e04df102a2770cf87c9bf2ae4fa4ad390d00a4e9b71869ff08b

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

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

    attr_reader :file, :dir

    def initialize(opts = {})
      wrapper = Ramdisk::Factory.get
      list = wrapper.list
      disk = nil
      if list.length <= 0
        # Create a new Ramdisk
        disk = wrapper.create(DEFAULTS[:disk_size])
      else
        # Use one which is large enough
        list.each { |d| disk = d if d.size >= Filesize.from(DEFAULTS[:disk_size]) }
        disk = wrapper.create(DEFAULTS[:disk_size]) if disk.nil?
      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)
    end

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

    def data
      IO.read(@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

1 entries across 1 versions & 1 rubygems

Version Path
ramdo-0.2.1 lib/ramdo/store.rb