Class: CheapSkate::InputDocument
- Inherits:
-
Object
- Object
- CheapSkate::InputDocument
- Defined in:
- lib/cheap_skate/models.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#query_time ⇒ Object
Returns the value of attribute query_time.
Instance Method Summary collapse
- #add ⇒ Object
- #as_json ⇒ Object
- #as_ruby ⇒ Object
- #commit ⇒ Object
- #delete ⇒ Object
-
#initialize(doc, index) ⇒ InputDocument
constructor
A new instance of InputDocument.
- #optimize ⇒ Object
- #parse ⇒ Object
- #to_hash ⇒ Object
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
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
189 190 191 |
# File 'lib/cheap_skate/models.rb', line 189 def doc @doc end |
#query_time ⇒ Object
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
#add ⇒ Object
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_json ⇒ Object
252 253 254 |
# File 'lib/cheap_skate/models.rb', line 252 def as_json return to_hash.to_json end |
#as_ruby ⇒ Object
248 249 250 |
# File 'lib/cheap_skate/models.rb', line 248 def as_ruby return to_hash.inspect end |
#commit ⇒ Object
221 222 223 |
# File 'lib/cheap_skate/models.rb', line 221 def commit @index.flush end |
#delete ⇒ Object
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 |
#optimize ⇒ Object
240 241 242 |
# File 'lib/cheap_skate/models.rb', line 240 def optimize @index.optimize end |
#parse ⇒ Object
197 198 199 200 |
# File 'lib/cheap_skate/models.rb', line 197 def parse action = @doc.root.name self.send(action) end |
#to_hash ⇒ Object
244 245 246 |
# File 'lib/cheap_skate/models.rb', line 244 def to_hash return {"responseHeader"=>{"QTime"=>self.query_time, "status"=>0}} end |