Class: Alexandria::SmartLibrary
- Inherits:
-
Array
- Object
- Array
- Alexandria::SmartLibrary
- 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
-
#deleted_books ⇒ Object
Returns the value of attribute deleted_books.
-
#n_rated ⇒ Object
readonly
Returns the value of attribute n_rated.
-
#name ⇒ Object
Returns the value of attribute name.
-
#predicate_operator_rule ⇒ Object
Returns the value of attribute predicate_operator_rule.
-
#rules ⇒ Object
Returns the value of attribute rules.
Class Method Summary collapse
- .deleted_libraries ⇒ Object
- .from_hash(hash, store) ⇒ Object
- .really_delete_deleted_libraries ⇒ Object
- .sample_smart_libraries(store) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #copy_covers(somewhere) ⇒ Object
- #cover(book) ⇒ Object
- #delete ⇒ Object
- #deleted? ⇒ Boolean
- #final_cover(book) ⇒ Object
-
#initialize(name, rules, predicate_operator_rule, store = nil) ⇒ SmartLibrary
constructor
A new instance of SmartLibrary.
- #n_unrated ⇒ Object
- #refilter ⇒ Object
- #save(book = nil) ⇒ Object
- #save_cover(book, _cover_uri) ⇒ Object
- #to_hash ⇒ Object
- #undelete ⇒ Object
- #update(*params) ⇒ Object
- #yaml(book = nil) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(name, rules, predicate_operator_rule, store = nil) ⇒ SmartLibrary
Returns a new instance of SmartLibrary.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/alexandria/smart_library.rb', line 25 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_books ⇒ Object
Returns the value of attribute deleted_books.
21 22 23 |
# File 'lib/alexandria/smart_library.rb', line 21 def deleted_books @deleted_books end |
#n_rated ⇒ Object (readonly)
Returns the value of attribute n_rated.
20 21 22 |
# File 'lib/alexandria/smart_library.rb', line 20 def n_rated @n_rated end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/alexandria/smart_library.rb', line 20 def name @name end |
#predicate_operator_rule ⇒ Object
Returns the value of attribute predicate_operator_rule.
21 22 23 |
# File 'lib/alexandria/smart_library.rb', line 21 def predicate_operator_rule @predicate_operator_rule end |
#rules ⇒ Object
Returns the value of attribute rules.
21 22 23 |
# File 'lib/alexandria/smart_library.rb', line 21 def rules @rules end |
Class Method Details
.deleted_libraries ⇒ Object
187 188 189 |
# File 'lib/alexandria/smart_library.rb', line 187 def self.deleted_libraries @@deleted_libraries end |
.from_hash(hash, store) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/alexandria/smart_library.rb', line 82 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_libraries ⇒ Object
191 192 193 194 195 196 |
# File 'lib/alexandria/smart_library.rb', line 191 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
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 80 |
# File 'lib/alexandria/smart_library.rb', line 41 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
181 182 183 |
# File 'lib/alexandria/smart_library.rb', line 181 def ==(other) other.is_a?(self.class) && other.name == name end |
#copy_covers(somewhere) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/alexandria/smart_library.rb', line 165 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
136 137 138 |
# File 'lib/alexandria/smart_library.rb', line 136 def cover(book) @cache[book].cover(book) end |
#delete ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/alexandria/smart_library.rb', line 198 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
211 212 213 |
# File 'lib/alexandria/smart_library.rb', line 211 def deleted? @@deleted_libraries.include?(self) end |
#final_cover(book) ⇒ Object
161 162 163 |
# File 'lib/alexandria/smart_library.rb', line 161 def final_cover(book) @cache[book].final_cover(book) end |
#n_unrated ⇒ Object
177 178 179 |
# File 'lib/alexandria/smart_library.rb', line 177 def n_unrated length - n_rated end |
#refilter ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/alexandria/smart_library.rb', line 119 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..nil? && x. > 0 } end |
#save(book = nil) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/alexandria/smart_library.rb', line 148 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
157 158 159 |
# File 'lib/alexandria/smart_library.rb', line 157 def save_cover(book, _cover_uri) @cache[book].save_cover(book) end |
#to_hash ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/alexandria/smart_library.rb', line 89 def to_hash { name: @name, predicate_operator_rule: @predicate_operator_rule == ALL_RULES ? :all : :any, rules: @rules.map(&:to_hash) } end |
#undelete ⇒ Object
215 216 217 218 219 |
# File 'lib/alexandria/smart_library.rb', line 215 def undelete raise unless @@deleted_libraries.include?(self) @@deleted_libraries.delete(self) end |
#update(*params) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/alexandria/smart_library.rb', line 106 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
140 141 142 143 144 145 146 |
# File 'lib/alexandria/smart_library.rb', line 140 def yaml(book = nil) if book @cache[book].yaml(book) else File.join(base_dir, @name + EXT) end end |