Class: ROCrate::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/ro_crate/model/entry.rb

Overview

A class to represent a “physical” file or directory within an RO-Crate. It handles the actual reading/writing of bytes.

Direct Known Subclasses

RemoteEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Entry

Create a new Entry.

Parameters:

  • source (#read)

    An IO-like source that can be read.



12
13
14
# File 'lib/ro_crate/model/entry.rb', line 12

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/ro_crate/model/entry.rb', line 6

def source
  @source
end

Instance Method Details

#directory?Boolean

Does this Entry point to a directory on the disk?

Returns:

  • (Boolean)


37
38
39
# File 'lib/ro_crate/model/entry.rb', line 37

def directory?
  ::File.directory?(source) rescue false
end

#pathObject



53
54
55
56
57
58
59
60
61
# File 'lib/ro_crate/model/entry.rb', line 53

def path
  if source.is_a?(Pathname)
    source.to_s
  elsif source.respond_to?(:path)
    source.path
  else
    nil
  end
end

#readObject

Read from the source.



31
32
33
# File 'lib/ro_crate/model/entry.rb', line 31

def read
  source.read
end

#remote?Boolean

Does this Entry point to a remote resource?

Returns:

  • (Boolean)


49
50
51
# File 'lib/ro_crate/model/entry.rb', line 49

def remote?
  false
end

#symlink?Boolean

Does this Entry point to a symlink on the disk?

Returns:

  • (Boolean)


43
44
45
# File 'lib/ro_crate/model/entry.rb', line 43

def symlink?
  ::File.symlink?(source) rescue false
end

#write_to(dest) ⇒ Object

Write the entry’s source to the destination via a buffer.

Parameters:

  • dest (#write)

    An IO-like destination to write to.



20
21
22
23
24
25
26
# File 'lib/ro_crate/model/entry.rb', line 20

def write_to(dest)
  input = source
  input = input.open('rb') if input.is_a?(Pathname)
  while (buff = input.read(4096))
    dest.write(buff)
  end
end