Class: Clip::File

Inherits:
Object
  • Object
show all
Defined in:
lib/clip/file.rb

Constant Summary collapse

MAGIC =
'CSFCHUNK'
SQLITE_MAGIC =
'SQLite format 3'
'CHNKFoot'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.

Raises:



19
20
21
22
23
24
# File 'lib/clip/file.rb', line 19

def initialize(path)
  @path = path

  raise InvalidClipFile, 'Not a CSP .clip' unless magic_present?
  raise InvalidClipFile, 'No database found in .clip' unless sqlite_present?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/clip/file.rb', line 13

def path
  @path
end

Class Method Details

.openObject



15
16
17
# File 'lib/clip/file.rb', line 15

def self.open(*, **)
  new(*, **)
end

Instance Method Details

#canvas_previewsObject



77
78
79
80
81
82
# File 'lib/clip/file.rb', line 77

def canvas_previews
  rows = sqlite_execute_as_hash "    SELECT * FROM CanvasPreview;\n  SQL\n  rows.map { |row| CanvasPreview.new(self, row) }\nend\n"

#closeObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clip/file.rb', line 64

def close
  if defined? :@sqlite && @sqlite
    @sqlite.close
    @sqlite = nil
  end

  if defined? :@sqlite_file && @sqlite_file
    @sqlite_file.close
    @sqlite_file.unlink
    @sqlite_file = nil
  end
end

#fileObject



26
27
28
# File 'lib/clip/file.rb', line 26

def file
  @file ||= ::File.open(path, 'rb')
end

#magic_present?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/clip/file.rb', line 34

def magic_present?
  file.pread(MAGIC.length, 0) == MAGIC
end

#raw_contentObject



30
31
32
# File 'lib/clip/file.rb', line 30

def raw_content
  @raw_content ||= file.read
end

#sqliteObject



60
61
62
# File 'lib/clip/file.rb', line 60

def sqlite
  @sqlite ||= SQLite3::Database.open(sqlite_file.path)
end

#sqlite_contentObject



42
43
44
45
46
47
48
# File 'lib/clip/file.rb', line 42

def sqlite_content
  @sqlite_content ||=
    begin
      len = footer_index - sqlite_index
      raw_content.byteslice(sqlite_index, len)
    end
end

#sqlite_fileObject



50
51
52
53
54
55
56
57
58
# File 'lib/clip/file.rb', line 50

def sqlite_file
  @sqlite_file ||=
    begin
      file = Tempfile.new("ruby-clip-#{basename}--", binmode: true)
      file.write(sqlite_content)
      file.seek(0)
      file
    end
end

#sqlite_present?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/clip/file.rb', line 38

def sqlite_present?
  !sqlite_index.nil?
end