Module: SolrMapper::SolrDocument

Defined in:
lib/solr_mapper/locators.rb,
lib/solr_mapper/relations.rb,
lib/solr_mapper/calculations.rb,
lib/solr_mapper/solr_document.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

property doesn’t exist yet. create it on demand.



191
192
193
194
195
196
197
# File 'lib/solr_mapper/solr_document.rb', line 191

def method_missing(m, *args, &block)
  method_name = m.to_s
  assignment = method_name.match(/\=$/)
  method_name = method_name.gsub(/\=/, '') if assignment
  self.class.class_eval { attr_accessor method_name }
  send "#{method_name}=", args[0] if assignment
end

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



188
189
190
# File 'lib/solr_mapper/solr_document.rb', line 188

def _id
  @_id
end

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
# File 'lib/solr_mapper/solr_document.rb', line 17

def self.included(base)
  base.extend(ClassMethods)
  base.solr_fields = []
  base.has_many_relationships = {}
end

Instance Method Details

#destroyObject

remove an object from the index



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/solr_mapper/solr_document.rb', line 212

def destroy
  # make an xml delete message that Solr will be happy with
  delete_message = ''
  builder = Builder::XmlMarkup.new(:target => delete_message, :indent => 2)

  builder.delete do |delete|
    delete.id(_id)
  end

  self.class.execute_write(delete_message)
end

#initialize(data = {}) ⇒ Object



275
276
277
# File 'lib/solr_mapper/solr_document.rb', line 275

def initialize(data = {})
  update_attributes_values(data)
end

#refresh_relation(field_name) ⇒ Object

when the owner collection is modified this will be called sync the relation up



195
196
197
198
199
200
201
202
# File 'lib/solr_mapper/relations.rb', line 195

def refresh_relation(field_name)
  ids = instance_variable_get("@#{self.class.has_many_relationships[field_name]}")
  ids.clear
    
  instance_variable_get("@#{field_name}").each do |child|
    ids << child._id
  end
end

#saveObject



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/solr_mapper/solr_document.rb', line 199

def save()
  send(:before_save) if respond_to?(:before_save)
  
  if not @_id and self.class.auto_generate_id?
    @_id = UUID.new().generate() 
    self.class.solr_fields << '_id' unless self.class.solr_fields.include?('_id')
  end

  self.class.execute_write(to_solr_xml, {:overwrite => true})
  send(:after_save) if respond_to?(:after_save)
end

#to_paramObject

handle rails building a url from us



271
272
273
# File 'lib/solr_mapper/solr_document.rb', line 271

def to_param
  instance_variable_get('@_id').to_s
end

#to_solr_xmlObject

convert a ruby object to xml compliant with a Solr update REST command



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/solr_mapper/solr_document.rb', line 225

def to_solr_xml
  output = ''
  builder = Builder::XmlMarkup.new(:target => output, :indent => 2)

  builder.add do |add|
    add.doc do |doc|
      self.class.solr_fields.each do |field_name|
        field_name = field_name.to_s

        if field_name == '_id'
          solr_field_name = 'id'
        else
          solr_field_name = field_name
        end

        val = instance_variable_get("@#{field_name}")

        if val
          if val.kind_of? Array
            val.each do |child|
              doc.field({:name => solr_field_name}, child)
            end
          else
            doc.field({:name => solr_field_name}, val)
          end
        end
      end
    end
  end

  output
end

#update_attributes(data) ⇒ Object



265
266
267
268
# File 'lib/solr_mapper/solr_document.rb', line 265

def update_attributes(data)
  update_attributes_values(data)
  save()
end

#update_attributes_values(data) ⇒ Object



258
259
260
261
262
263
# File 'lib/solr_mapper/solr_document.rb', line 258

def update_attributes_values(data)
  data.each_pair do |k, v|
    instance_variable_set("@#{k}", v)
    self.class.solr_fields << k.to_s unless self.class.solr_fields.include?(k.to_s)
  end
end