Class: Clip::File
- Inherits:
-
Object
- Object
- Clip::File
- Defined in:
- lib/clip/file.rb
Constant Summary collapse
- MAGIC =
'CSFCHUNK'- SQLITE_MAGIC =
'SQLite format 3'- FOOTER_MARKER =
'CHNKFoot'
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #canvas_previews ⇒ Object
- #close ⇒ Object
- #file ⇒ Object
-
#initialize(path) ⇒ File
constructor
A new instance of File.
- #magic_present? ⇒ Boolean
- #raw_content ⇒ Object
- #sqlite ⇒ Object
- #sqlite_content ⇒ Object
- #sqlite_file ⇒ Object
- #sqlite_present? ⇒ Boolean
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/clip/file.rb', line 13 def path @path end |
Class Method Details
.open ⇒ Object
15 16 17 |
# File 'lib/clip/file.rb', line 15 def self.open(*, **) new(*, **) end |
Instance Method Details
#canvas_previews ⇒ Object
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" |
#close ⇒ Object
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 |
#file ⇒ Object
26 27 28 |
# File 'lib/clip/file.rb', line 26 def file @file ||= ::File.open(path, 'rb') end |
#magic_present? ⇒ Boolean
34 35 36 |
# File 'lib/clip/file.rb', line 34 def magic_present? file.pread(MAGIC.length, 0) == MAGIC end |
#raw_content ⇒ Object
30 31 32 |
# File 'lib/clip/file.rb', line 30 def raw_content @raw_content ||= file.read end |
#sqlite ⇒ Object
60 61 62 |
# File 'lib/clip/file.rb', line 60 def sqlite @sqlite ||= SQLite3::Database.open(sqlite_file.path) end |
#sqlite_content ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/clip/file.rb', line 42 def sqlite_content @sqlite_content ||= begin len = - sqlite_index raw_content.byteslice(sqlite_index, len) end end |
#sqlite_file ⇒ Object
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
38 39 40 |
# File 'lib/clip/file.rb', line 38 def sqlite_present? !sqlite_index.nil? end |