Class: DynarexBlog

Inherits:
Object
  • Object
show all
Defined in:
lib/dynarex-blog.rb

Constant Summary collapse

ENTITIES =
'entities.xml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = './') ⇒ DynarexBlog

Returns a new instance of DynarexBlog.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dynarex-blog.rb', line 15

def initialize(file_path='./')
  @file_path = file_path[/\/$/] ? file_path : file_path + '/'
  if File.exists? (@file_path + 'index.xml') then  open(@file_path) else fresh_start() end
  @current_lookup = '_entry_lookup.xml'
  @hc_lookup = HashCache.new(size: 15)

  @hc_lookup.read(@current_lookup) { Rexle.new File.open(@file_path + @current_lookup,'r').read }
  
  @hc_result = HashCache.new(size: 10)
  @hc_entry_file = HashCache.new(size: 10)
  @hc_lookup_a = HashCache.new(size: 10)
  
  xpath = "records/section[summary/name='tags']/records/entity/summary"
  @tags = @entities.xpath(xpath){|e| [e.text('name'), e.text('count')]}
  super()
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/dynarex-blog.rb', line 13

def id
  @id
end

Instance Method Details

#create_entry(record = {}) ⇒ Object



32
33
34
35
36
37
38
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
64
# File 'lib/dynarex-blog.rb', line 32

def create_entry(record={})

  if record.has_key? :title then
    lookup = '_entry_lookup.xml'
    doc = Rexle.new File.open(@file_path + lookup,'r').read
    if doc.element("records/entry[uri='#{format_uri(record[:title])}']") then
    raise "err: create_entry: Duplicate title found"
    end
  end

  @hc_result.reset
  @id += 1
  # create a new record
  @index.create record, @id

  # if there is more than 10 records shift the 1st record.
  if @index.flat_records.length > 10 then
    # '+++ deleting the 1st index record +++'
    @index.to_doc.delete('records/*[1]')
  end

  @index.save

  create_record(record, @id.to_s, name='_entry', type='main')


  if record[:tags] then
    record[:tags].split(/\s/).each do |tag|
      create_record(record, @id.to_s, name=tag, type='tags')
    end
  end    

end

#delete(id = '') ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dynarex-blog.rb', line 122

def delete(id='')

  # delete from the tags files (entry and lookup), the entry file and lookup
  # look the id up in lookup.xml

  lookup = Dynarex.new @file_path + @current_lookup
  lookup_id = lookup.records[id][:id]
  file = lookup.records[id][:body][:file]
  
  dynarex = Dynarex.new(@file_path + file)    
  tags = dynarex.record(id).tags.split(/\s/)
  dynarex.delete(id).save

  lookup.delete(lookup_id).save

  tags.each do |tag_name|
    # find the lookup
    delete_entry(tag_name, id)
  end

  # start of reindexing

  if index_include? id then

    count = @index.xpath('count(records/entry)')

    if count < 10 then
      @index.delete(id)
      @index.save
    else
      refresh_index     
    end
  end
end

#entry(id) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/dynarex-blog.rb', line 66

def entry(id)

  doc_lookup = @hc_lookup.read(@current_lookup) { Rexle.new File.open(@file_path + @current_lookup,'r').read }
  file = doc_lookup.text("records/entry[id='#{id}']/file")
  doc_entries = Rexle.new(@hc_entry_file.read(file) { File.open(@file_path + file,'r').read })

  doc_entries.element("records/entry[@id='#{id}']")
end

#fresh_startObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dynarex-blog.rb', line 157

def fresh_start()

  @id = 1
  @dynarex = new_blog_file '_entry1.xml'
  @index = new_blog_file 'index.xml'

  @entities = Polyrex.new('entities/section[name]/entity[name,count,entry_count]')

  @entities.create.section({name: 'main'}, id='main') do |create|
    create.entity(name: '_entry', count: '1')
  end

  @entities.create.section({name: 'tags'}, id='tags')
  @entities.save @file_path + ENTITIES
  @lookup = new_lookup_file '_entry_lookup.xml'

  # add the _404.xml
  dynarex = Dynarex.new('entries/entry(id,file,year,month,uri)')
  year, month = [:year, :month].map{|x| "%02d" % Time.now.method(x).call}
  dynarex.create id: '1', file: '_4041.xml', year: year, month: month
  dynarex.save '_404_lookup.xml'

  Dynarex.new('entries/entry(title,body,tags,user)').save '_4041.xml'

end

#open(file_path = './') ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dynarex-blog.rb', line 183

def open(file_path='./')

  @file_path = file_path
  threads = []
  threads << Thread.new{
    @index = Dynarex.new @file_path + 'index.xml'
    @id = @index.records ? @index.records.to_a[-1][-1][:id].to_i : 0
  }
  threads << Thread.new{@entities = Polyrex.new @file_path + ENTITIES}
  threads.each {|thread| thread.join}

end

#page(number = '1') ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/dynarex-blog.rb', line 196

def page(number='1')
  lookup = @current_lookup

  result = nil
  
  result = @hc_result.read(lookup + number.to_s) do

    if (number.to_s == '1') and (lookup == '_entry_lookup.xml') and (@index.records.length <= 10) then 

      doc = @hc_lookup.refresh(lookup)
      r = @index.to_doc
    else

    	doc = @hc_lookup.read(lookup) { Rexle.new File.open(@file_path + lookup,'r').read }                
      r = select_page(doc, number.to_i)
      @current_lookup = '_entry_lookup.xml'
      
      # refresh to maintain _entry_lookup in the cache
      @hc_lookup.refresh(@current_lookup) 
      @hc_lookup_a.refresh(@current_lookup)
    end
    
    total_records = doc.xpath('count(records/entry)')
    return nil if total_records.nil?

    total_pages, remainder = %w(/ %).map {|x| total_records.send x, 10}
    total_pages += 1 if remainder > 0      
    
    summary = {
      total_records: total_records,
      page_number: number,
      total_pages: total_pages
    }
    
    summary.each do |name, text|
      r.element('summary').add Rexle::Element.new(name.to_s).add_text(text.to_s)    
    end    
    
    r
  end
  
  @current_lookup = '_entry_lookup.xml'
  
  result
end

#rebuild_indexObject



251
252
253
# File 'lib/dynarex-blog.rb', line 251

def rebuild_index()
  refresh_index()
end

#tag(tag) ⇒ Object



242
243
244
245
# File 'lib/dynarex-blog.rb', line 242

def tag(tag)      
  @current_lookup = (@tags.assoc(tag) ? tag  : '_404') + '_lookup.xml'      
  self
end

#tagsObject



247
248
249
# File 'lib/dynarex-blog.rb', line 247

def tags
  @tags
end

#update(id, h) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dynarex-blog.rb', line 75

def update(id, h)

  puts '@current_looup ' + @current_lookup
  puts 'id : ' + id.to_s
 
  lookup = Dynarex.new @file_path + @current_lookup
  puts 'records : ' + lookup.records.inspect
  lookup_id = lookup.records[id][:id]
  file = lookup.records[id][:body][:file]

  reset_cache_entry(@current_lookup, file)
  
  dynarex = Dynarex.new(@file_path + file) 
  prev_tags = dynarex.record(id).tags
  
  if h[:tags] and h[:tags].length > 0 then
    
    a = h[:tags].split(/\s/)
  
    if prev_tags and prev_tags.length > 0 then
	
      b = prev_tags.split(/\s/)
      old_list = a - b # tags to be deleted
      new_list = b - a # tags to be inserted

      old_list.each {|tag_name| delete_entry(tag_name, id) }
      common = a.to_set.intersection(b).to_a # tags to be updated     
      common.each {|name|  update_entry(name, id, h) }
    else
      new_list = a
    end
    
    new_list.each {|tag| create_record(h, id, name=tag, type='tags') }
    dynarex.update(id, h)
    puts 'dynarex : ' + dynarex.to_xml
    puts 'saving: ' + @file_path + file
    dynarex.save
  end
  
  lookup.update(lookup_id, uri: h[:title].gsub(/\s/,'-')).save

  @hc_lookup.write(@current_lookup) { Rexle.new File.open(@file_path + @current_lookup,'r').read }                

  (puts "rebuilding index #@file_path} ... "; refresh_index) if index_include? id

end