Module: BluxIndexer
- Included in:
- BlogManager, DraftManager
- Defined in:
- lib/indexer.rb
Overview
Copyright 2010 Louis-Philippe Salin de l’Etoile, aka Louis Salin email: [email protected]
This file is part of Blux.
Blux is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Blux is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Blux. If not, see <www.gnu.org/licenses/>.
Instance Method Summary collapse
- #check_count ⇒ Object
- #check_filename(filename) ⇒ Object
- #check_index(filename) ⇒ Object
- #delete_attribute(filename, attr_name) ⇒ Object
- #delete_index(filename) ⇒ Object
- #ensure_not_deleted(filename) ⇒ Object
- #get_attribute(filename, attribute) ⇒ Object
- #load_index ⇒ Object
- #print_index ⇒ Object
- #save_index(index) ⇒ Object
- #set_attribute(filename, key, val) ⇒ Object
Instance Method Details
#check_count ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/indexer.rb', line 99 def check_count index = load_index if index.keys.length > 0 yield else msg = "there is currently no saved index" raise RuntimeError, msg end end |
#check_filename(filename) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/indexer.rb', line 28 def check_filename(filename) draft_filename = "#{self.draft_dir}/#{filename}" if (File.exists?(draft_filename)) yield draft_filename else msg = "draft filename #{filename} does not exist" raise RuntimeError, msg end end |
#check_index(filename) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/indexer.rb', line 20 def check_index(filename) check_filename(filename) do full_index = load_index full_index[filename] ||= {} yield full_index, full_index[filename] end end |
#delete_attribute(filename, attr_name) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/indexer.rb', line 65 def delete_attribute(filename, attr_name) check_index(filename) do |full_index, index| case attr_name when "categories" if block_given? categories = yield categories_to_remove = categories.split(',') values = index[attr_name.to_s].split(',') new_values = values.reject{|i| categories_to_remove.include?(i)}.join(',') if new_values.length > 0 index[attr_name.to_s] = new_values else index.delete(attr_name.to_s) end else index.delete(attr_name.to_s) end else index.delete(attr_name.to_s) end save_index(full_index) end end |
#delete_index(filename) ⇒ Object
109 110 111 112 |
# File 'lib/indexer.rb', line 109 def delete_index(filename) index = load_index save_index if index.delete filename end |
#ensure_not_deleted(filename) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/indexer.rb', line 136 def ensure_not_deleted(filename) check_index(filename) do |full_index, index| msg = "draft filename #{filename} has been deleted" raise RuntimeError, msg if index["deleted"] end end |
#get_attribute(filename, attribute) ⇒ Object
93 94 95 96 97 |
# File 'lib/indexer.rb', line 93 def get_attribute(filename, attribute) check_index(filename) do |full_index, index| index[attribute] end end |
#load_index ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/indexer.rb', line 114 def load_index system "touch #{@index_file}" unless File.exists? @index_file str = '' File.open(@index_file, 'r') do |f| f.each_line {|l| str += l} end return str.length > 0 ? JSON.parse(str) : {} end |
#print_index ⇒ Object
131 132 133 134 |
# File 'lib/indexer.rb', line 131 def print_index index = load_index puts index.to_json + "\n" if @verbose end |
#save_index(index) ⇒ Object
125 126 127 128 129 |
# File 'lib/indexer.rb', line 125 def save_index(index) File.open(@index_file, 'w') do |f| f.write(index.to_json) if index end end |
#set_attribute(filename, key, val) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/indexer.rb', line 39 def set_attribute(filename, key, val) check_index(filename) do |full_index, index| case key when "title" unique_title = true full_index.keys.reject{|k| k == filename}.each do |other_key| unique_title = false if (full_index[other_key.to_s][key] == val) end STDERR << "warning: title '#{val}' is not unique\n" unless unique_title index[key.to_s] = val when "categories" values = Array.new values << index[key.to_s] unless index[key.to_s] == nil values << val index[key.to_s] = values.join(',') else index[key.to_s] = val end save_index(full_index) end end |