module CephStorage
  # core representation of a file.
  # This is a mixin module for other storage object modules
  module StorageObject
    # Represent an object that can be written to or read from
    # This is designed to be modular so that you can expand into other
    # technologies

    attr_accessor :name
    def move(dst_object)
      log("move to '#{dst_object.class}/#{dst_object.name}'")
      dst_object.write_file(read_file)
      copy_xattrs(dst_object)
      destroy
    end

    def copy(dst_object)
      log("copy to '#{dst_object.class}/#{dst_object.name}'")
      dst_object.write_file(read_file)
      copy_xattrs(dst_object)
    end

    def copy_xattrs(dst_object)
      log('copy xattrs')
      return unless xattr_supported?(dst_object)
      xattr_enumerator.each do |source_xattr|
        dst_object.xattr(source_xattr.name) do |dst_xattr|
          dst_xattr.value = source_xattr.value
        end
      end
    end

    def xattr_supported?(dst_object)
      CephStorage::StorageObject.supports_xattr?(self) &&
        CephStorage::StorageObject.supports_xattr?(dst_object)
    end

    class << self
      def supports_xattr?(obj)
        obj.respond_to? :xattr_enumerator
      end
    end

    def log(message)
      CephStorage.log("storage object #{name} #{message}")
    end
  end
end