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
- #check_title(filename, attr_key, attr_val) ⇒ Object
- #delete_attribute(filename, attr_name) ⇒ Object
- #get_attribute(filename, attribute) ⇒ Object
- #load_index ⇒ Object
- #print_index ⇒ Object
- #save_index ⇒ Object
- #set_attribute(filename, key, val) ⇒ Object
Instance Method Details
#check_count ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/indexer.rb', line 72 def check_count if @index.keys.length > 0 yield else msg = "there is currently no saved index" raise RuntimeError, msg end end |
#check_filename(filename) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/indexer.rb', line 27 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 |
# File 'lib/indexer.rb', line 20 def check_index(filename) check_filename(filename) do @index[filename] ||= {} yield @index[filename] end end |
#check_title(filename, attr_key, attr_val) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/indexer.rb', line 38 def check_title(filename, attr_key, attr_val) return true unless attr_key.to_s == "title" unique_title = true @index.keys.reject{|k| k == filename}.each do |key| unique_title = false if (@index[key][attr_key.to_s] == attr_val) end STDERR << "warning: title '#{attr_val}' is not unique\n" unless unique_title true end |
#delete_attribute(filename, attr_name) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/indexer.rb', line 59 def delete_attribute(filename, attr_name) check_index(filename) do |index| index.delete(attr_name.to_s) save_index end end |
#get_attribute(filename, attribute) ⇒ Object
66 67 68 69 70 |
# File 'lib/indexer.rb', line 66 def get_attribute(filename, attribute) check_index(filename) do |index| index[attribute] end end |
#load_index ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/indexer.rb', line 81 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 @index = str.length > 0 ? JSON.parse(str) : {} end |
#print_index ⇒ Object
98 99 100 |
# File 'lib/indexer.rb', line 98 def print_index puts @index.to_json + "\n" if @verbose end |
#save_index ⇒ Object
92 93 94 95 96 |
# File 'lib/indexer.rb', line 92 def save_index File.open(@index_file, 'w') do |f| f.write(@index.to_json) if @index end end |
#set_attribute(filename, key, val) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/indexer.rb', line 50 def set_attribute(filename, key, val) check_index(filename) do |index| if check_title(filename, key, val) index[key.to_s] = val save_index end end end |