Class: Rindle::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rindle/document.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Document

Returns a new instance of Document.



100
101
102
103
104
105
# File 'lib/rindle/document.rb', line 100

def initialize path
  path = "/#{File.join('documents', path)}" unless path =~ %r{^/{0,1}documents/}
  path = "/#{path}" unless path =~ %r{^/}
  @path  = path
  @index = Rindle::Document.generate_index(path)
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



98
99
100
# File 'lib/rindle/document.rb', line 98

def index
  @index
end

#pathObject (readonly)

Returns the value of attribute path.



98
99
100
# File 'lib/rindle/document.rb', line 98

def path
  @path
end

Class Method Details

.all(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rindle/document.rb', line 25

def all options = {}
  filtered = []
  Rindle.index.each_pair do |index, doc|
    match = true
    options.each_pair do |key, value|
      case key
      when :named
        match = match && doc.filename =~ /#{value.is_a?(String) ? Regexp.escape(value) : value}/
      when :indexed
        match = match && index =~ /#{value.is_a?(String) ? Regexp.escape(value) : value}/
      end
    end
    filtered << doc if match
  end
  filtered
end

.create(filename, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rindle/document.rb', line 9

def create filename, options = {}
  doc = Rindle::Document.new filename
  Rindle.index[doc.index] = doc

  absolute_path = File.join(Rindle.root_path, doc.path)
  if options[:data]
    File.open(absolute_path, 'w+') do |f|
      f.write options[:data]
    end
  else
    FileUtils.touch absolute_path
  end

  doc
end

.find(method = :all, options = {}) ⇒ Object



58
59
60
# File 'lib/rindle/document.rb', line 58

def find method = :all, options = {}
  self.send method, options
end

.find_by_index(index) ⇒ Object



84
85
86
# File 'lib/rindle/document.rb', line 84

def find_by_index index
  Rindle.index[index]
end

.find_by_name(name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rindle/document.rb', line 72

def find_by_name name
  Rindle.index.values.each do |doc|
    case name
    when String
      return doc if doc.filename == name
    when Regexp
      return doc if doc.filename =~ name
    end
  end
  nil
end

.first(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rindle/document.rb', line 42

def first options = {}
  Rindle.index.each_pair do |index, doc|
    match = true
    options.each_pair do |key, value|
      case key
      when :named
        match = match && doc.filename =~ /#{value.is_a?(String) ? Regexp.escape(value) : value}/
      when :indexed
        match = match && index =~ /#{value.is_a?(String) ? Regexp.escape(value) : value}/
      end
    end
    return doc if match
  end
  nil
end

.generate_index(path) ⇒ Object

Generates the index for the current path



89
90
91
92
93
94
95
# File 'lib/rindle/document.rb', line 89

def generate_index path
  if path =~ /([\w\s]+)-asin_([A-Z0-9]+)-type_([A-Z]+)-v_[0-9]+.azw/
    "##{$2}^#{$3}"
  else
    "*#{Digest::SHA1.hexdigest(File.join('/mnt/us', path))}"
  end
end

.unassociatedObject



62
63
64
65
66
67
68
69
70
# File 'lib/rindle/document.rb', line 62

def unassociated
  unassociated = []
  Rindle.index.each_pair do |index, doc|
    unless Rindle.collections.values.inject(false) { |acc, col| acc = acc or col.include?(index) }
      unassociated << doc
    end
  end
  unassociated
end

Instance Method Details

#==(other) ⇒ Object

Two documents are the same if the indices are equal.



108
109
110
# File 'lib/rindle/document.rb', line 108

def == other
  @index == other.index
end

#amazon?Boolean

Returns true if the ‘path` looks like an amazon kindle store file.

Returns:

  • (Boolean)


123
124
125
# File 'lib/rindle/document.rb', line 123

def amazon?
  !(path !~ /([\w\s]+)-asin_([A-Z0-9]+)-type_([A-Z]+)-v_[0-9]+.azw/)
end

#collectionsObject

Returns an array of all the collections, this document is in.



164
165
166
167
168
# File 'lib/rindle/document.rb', line 164

def collections
  Rindle.collections.values.select do |col|
    col.include? self.index
  end
end

#delete!Object



158
159
160
161
# File 'lib/rindle/document.rb', line 158

def delete!
  destroy!
  FileUtils.rm_f File.join(Rindle.root_path, path)
end

#destroy!Object



151
152
153
154
155
156
# File 'lib/rindle/document.rb', line 151

def destroy!
  Rindle.collections.values.each do |col|
    col.remove index if col.include? index
  end
  Rindle.index.delete index
end

#filenameObject

Returns the filename of this document.



118
119
120
# File 'lib/rindle/document.rb', line 118

def filename
  File.basename(@path)
end

#filesizeObject

Returns the filesize of this document.



113
114
115
# File 'lib/rindle/document.rb', line 113

def filesize
  @filesize ||= File.size(File.join(Rindle.root_path, @path))
end

#rename!(new_name) ⇒ Object

Renames the document. This also means that the index is changed and the Index-hash is updated.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rindle/document.rb', line 129

def rename! new_name
  Rindle.index.delete(@index)

  old_index = @index
  old_path = @path.dup
  @path.gsub!(filename, new_name)
  @index = Rindle::Document.generate_index(@path)

  File.rename File.join(Rindle.root_path, old_path),
              File.join(Rindle.root_path, @path)

  Rindle.collections.values.each do |col|
    if col.include?(old_index)
      col.remove old_index
      col.add @index
    end
  end

  Rindle.index[@index] = self
  true
end