Class: Alexandria::SmartLibrary

Inherits:
Array
  • Object
show all
Extended by:
GetText
Includes:
Logging, GetText
Defined in:
lib/alexandria/smart_library.rb

Defined Under Namespace

Classes: Rule

Constant Summary collapse

ALL_RULES =
1
ANY_RULE =
2
EXT =
".yaml"
@@deleted_libraries =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

#initialize(name, rules, predicate_operator_rule, store = nil) ⇒ SmartLibrary

Returns a new instance of SmartLibrary.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/alexandria/smart_library.rb', line 24

def initialize(name, rules, predicate_operator_rule, store = nil)
  super()
  raise if name.nil? || rules.nil? || predicate_operator_rule.nil?

  @name = name.dup.force_encoding("UTF-8")
  @rules = rules
  @predicate_operator_rule = predicate_operator_rule
  @store = store
  libraries = LibraryCollection.instance
  libraries.add_observer(self)
  self.libraries = libraries.all_regular_libraries
  # carry deleted books over from libraries that are part of the smart library
  self.deleted_books = libraries.deleted_books
  @cache = {}
end

Instance Attribute Details

#deleted_booksObject

Returns the value of attribute deleted_books.



20
21
22
# File 'lib/alexandria/smart_library.rb', line 20

def deleted_books
  @deleted_books
end

#n_ratedObject (readonly)

Returns the value of attribute n_rated.



19
20
21
# File 'lib/alexandria/smart_library.rb', line 19

def n_rated
  @n_rated
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/alexandria/smart_library.rb', line 19

def name
  @name
end

#predicate_operator_ruleObject

Returns the value of attribute predicate_operator_rule.



20
21
22
# File 'lib/alexandria/smart_library.rb', line 20

def predicate_operator_rule
  @predicate_operator_rule
end

#rulesObject

Returns the value of attribute rules.



20
21
22
# File 'lib/alexandria/smart_library.rb', line 20

def rules
  @rules
end

Class Method Details

.deleted_librariesObject



186
187
188
# File 'lib/alexandria/smart_library.rb', line 186

def self.deleted_libraries
  @@deleted_libraries
end

.from_hash(hash, store) ⇒ Object



81
82
83
84
85
86
# File 'lib/alexandria/smart_library.rb', line 81

def self.from_hash(hash, store)
  SmartLibrary.new(hash[:name],
                   hash[:rules].map { |x| Rule.from_hash(x) },
                   hash[:predicate_operator_rule] == :all ? ALL_RULES : ANY_RULE,
                   store)
end

.really_delete_deleted_librariesObject



190
191
192
193
194
195
# File 'lib/alexandria/smart_library.rb', line 190

def self.really_delete_deleted_libraries
  @@deleted_libraries.each do |library|
    log.debug { "Deleting smart library file (#{yaml})" }
    FileUtils.rm_rf(library.yaml)
  end
end

.sample_smart_libraries(store) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/alexandria/smart_library.rb', line 40

def self.sample_smart_libraries(store)
  a = []

  operands = Rule::Operands::LEFT

  # Favorite books.
  rule = Rule.new(operands.find { |x| x.book_selector == :rating },
                  Rule::Operators::IS,
                  Book::MAX_RATING_STARS.to_s)
  a << new(_("Favorite"), [rule], ALL_RULES, store)

  # Loaned books.
  rule = Rule.new(operands.find { |x| x.book_selector == :loaned },
                  Rule::Operators::IS_TRUE,
                  nil)
  a << new(_("Loaned"), [rule], ALL_RULES, store)

  # Redd books.
  rule = Rule.new(operands.find { |x| x.book_selector == :redd },
                  Rule::Operators::IS_TRUE,
                  nil)
  a << new(_("Read"), [rule], ALL_RULES, store)

  # Own books.
  rule = Rule.new(operands.find { |x| x.book_selector == :own },
                  Rule::Operators::IS_TRUE,
                  nil)
  a << new(_("Owned"), [rule], ALL_RULES, store)

  # Want books.
  rule = Rule.new(operands.find { |x| x.book_selector == :want },
                  Rule::Operators::IS_TRUE,
                  nil)
  rule2 = Rule.new(operands.find { |x| x.book_selector == :own },
                   Rule::Operators::IS_NOT_TRUE,
                   nil)
  a << new(_("Wishlist"), [rule, rule2], ALL_RULES, store)

  a
end

Instance Method Details

#==(other) ⇒ Object



180
181
182
# File 'lib/alexandria/smart_library.rb', line 180

def ==(other)
  other.is_a?(self.class) && other.name == name
end

#copy_covers(somewhere) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/alexandria/smart_library.rb', line 164

def copy_covers(somewhere)
  FileUtils.rm_rf(somewhere)
  FileUtils.mkdir(somewhere)
  each do |book|
    library = @cache[book]
    next unless File.exist?(library.cover(book))

    FileUtils.cp(File.join(library.path, book.ident + Library::EXT[:cover]),
                 File.join(somewhere, library.final_cover(book)))
  end
end

#cover(book) ⇒ Object



135
136
137
# File 'lib/alexandria/smart_library.rb', line 135

def cover(book)
  @cache[book].cover(book)
end

#deleteObject



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/alexandria/smart_library.rb', line 197

def delete
  if @@deleted_libraries.include?(self)
    log.info do
      "Already deleted a SmartLibrary with this name (this might mess up undeletes)"
    end
    FileUtils.rm_rf(yaml)
    # so we just delete the old smart library, and
    # 'pending' delete the new one of the same name...
    # urrr... yeah, that'll work!
  end
  @@deleted_libraries << self
end

#deleted?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/alexandria/smart_library.rb', line 210

def deleted?
  @@deleted_libraries.include?(self)
end

#final_cover(book) ⇒ Object



160
161
162
# File 'lib/alexandria/smart_library.rb', line 160

def final_cover(book)
  @cache[book].final_cover(book)
end

#n_unratedObject



176
177
178
# File 'lib/alexandria/smart_library.rb', line 176

def n_unrated
  length - n_rated
end

#refilterObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/alexandria/smart_library.rb', line 118

def refilter
  filters = @rules.map(&:filter_proc)
  selector = @predicate_operator_rule == ALL_RULES ? :all? : :any?

  clear
  @cache.clear

  @libraries.each do |library|
    filtered_library = library.select do |book|
      filters.send(selector) { |filter| filter.call(book) } # Problem here.
    end
    filtered_library.each { |x| @cache[x] = library }
    concat(filtered_library)
  end
  @n_rated = count { |x| !x.rating.nil? && x.rating > 0 }
end

#save(book = nil) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/alexandria/smart_library.rb', line 147

def save(book = nil)
  if book
    @cache[book].save(book)
  else
    FileUtils.mkdir_p(base_dir)
    File.open(yaml, "w") { |io| io.puts to_hash.to_yaml }
  end
end

#save_cover(book, _cover_uri) ⇒ Object



156
157
158
# File 'lib/alexandria/smart_library.rb', line 156

def save_cover(book, _cover_uri)
  @cache[book].save_cover(book)
end

#to_hashObject



88
89
90
91
92
93
94
# File 'lib/alexandria/smart_library.rb', line 88

def to_hash
  {
    name: @name,
    predicate_operator_rule: @predicate_operator_rule == ALL_RULES ? :all : :any,
    rules: @rules.map(&:to_hash)
  }
end

#undeleteObject



214
215
216
217
218
# File 'lib/alexandria/smart_library.rb', line 214

def undelete
  raise unless @@deleted_libraries.include?(self)

  @@deleted_libraries.delete(self)
end

#update(*params) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/alexandria/smart_library.rb', line 105

def update(*params)
  case params.first
  when LibraryCollection
    libraries, _, library = params
    unless library.is_a?(self.class)
      self.libraries = libraries.all_libraries
      refilter
    end
  when Library
    refilter
  end
end

#yaml(book = nil) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/alexandria/smart_library.rb', line 139

def yaml(book = nil)
  if book
    @cache[book].yaml(book)
  else
    File.join(base_dir, @name + EXT)
  end
end