Class: CheapSkate::InputDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/cheap_skate/models.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, index) ⇒ InputDocument

Returns a new instance of InputDocument.



191
192
193
194
195
# File 'lib/cheap_skate/models.rb', line 191

def initialize(doc, index)
  doc.sub!(/^[^\<]*/,'')
  @doc = Hpricot::XML(doc)
  @index = index
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



189
190
191
# File 'lib/cheap_skate/models.rb', line 189

def doc
  @doc
end

#query_timeObject

Returns the value of attribute query_time.



190
191
192
# File 'lib/cheap_skate/models.rb', line 190

def query_time
  @query_time
end

Instance Method Details

#addObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/cheap_skate/models.rb', line 202

def add
  (@doc/'/add/doc').each do |doc|
    document = @index.create_document
    (doc/'field').each do |elem|
      field = elem.attributes['name']
      value = nil
      value = elem.inner_html
      if field and value
        if field == "id"
          document[@index.schema.id_field] = value
        else
          document.add_field(field, value)
        end
      end
    end      
    @index << document
  end
end

#as_jsonObject



252
253
254
# File 'lib/cheap_skate/models.rb', line 252

def as_json
  return to_hash.to_json    
end

#as_rubyObject



248
249
250
# File 'lib/cheap_skate/models.rb', line 248

def as_ruby
  return to_hash.inspect
end

#commitObject



221
222
223
# File 'lib/cheap_skate/models.rb', line 221

def commit
  @index.flush
end

#deleteObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/cheap_skate/models.rb', line 225

def delete
  ids = []
  (@doc/"/delete/id").each do |del|
    ids << del.inner_html
  end
  (@doc/"/delete/query").each do |del|
    @index.search_each(del.attributes['q'], :limit=>:all) do |id,score|
      ids << id
    end      
  end
  unless ids.empty?
    @index.delete(ids)
  end
end

#optimizeObject



240
241
242
# File 'lib/cheap_skate/models.rb', line 240

def optimize
  @index.optimize
end

#parseObject



197
198
199
200
# File 'lib/cheap_skate/models.rb', line 197

def parse
  action = @doc.root.name
  self.send(action)
end

#to_hashObject



244
245
246
# File 'lib/cheap_skate/models.rb', line 244

def to_hash
  return {"responseHeader"=>{"QTime"=>self.query_time, "status"=>0}}
end