Module: ActiveFedora::Model::ClassMethods

Defined in:
lib/active_fedora/model.rb

Overview

Class Methods

These methods are mixed into the inheriting class.

Accessor and mutator methods are dynamically generated based on the contents of the @@field_spec hash, which stores the field specifications recorded during invocation of has_metadata.

Each metadata field will generate 3 methods:

fieldname_values
  *returns the current values array for this field
fieldname_values=(val) 
  *store val as the values array. val 
  may be a single string, or an array of strings 
  (single items become single element arrays).
fieldname_append(val)
  *appends val to the values array.

Instance Method Summary collapse

Instance Method Details

#class_fieldsObject



344
345
346
347
348
349
350
351
# File 'lib/active_fedora/model.rb', line 344

def class_fields
  #create dummy object that is empty by passing in fake pid
  object = self.new()#{:pid=>'FAKE'})
  fields = object.fields
  #reset id to nothing
  fields[:id][:values] = []
  return fields
end

#count(args = {}) ⇒ Object

Get a count of the number of objects from solr Takes :conditions as an argument



180
181
182
183
184
185
# File 'lib/active_fedora/model.rb', line 180

def count(args = {})
    escaped_class_uri = SolrService.escape_uri_for_query(self.to_class_uri)
    q = "#{ActiveFedora::SolrService.solr_name(:has_model, :symbol)}:#{escaped_class_uri}"
    q << " AND #{args[:conditions]}" if args[:conditions]
    SolrService.query(q, :raw=>true, :rows=>0)['response']['numFound']
end

#create_query(conditions) ⇒ Object

Returns a solr query for the supplied conditions @param conditions solr conditions to match



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/active_fedora/model.rb', line 319

def create_query(conditions)
  escaped_class_uri = SolrService.escape_uri_for_query(self.to_class_uri)
  clauses = ["#{ActiveFedora::SolrService.solr_name(:has_model, :symbol)}:#{escaped_class_uri}"]
  conditions.each_pair do |key,value|
    unless value.nil?
      if value.is_a? Array
        value.each do |val|
          clauses << "#{key}:#{quote_for_solr(val)}"  
        end
      else
        key = SOLR_DOCUMENT_ID if (key === :id || key === :pid)
        escaped_value = quote_for_solr(value)
        clauses << (key.to_s.eql?(SOLR_DOCUMENT_ID) ? "#{key}:#{escaped_value}" : "#{key}:#{escaped_value}")
      end
    end
  end

  clauses.join(" AND ")
end

#exists?(pid) ⇒ Boolean

Returns true if the pid exists in the repository @param pid @return

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/active_fedora/model.rb', line 166

def exists?(pid)
  inner = DigitalObject.find(self, pid)
  !inner.new?
end

#find(args, opts = {}) ⇒ Object

Returns an Array of objects of the Class that find is being called on

@param args either a pid or :all or a hash of conditions

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (opts):

  • :rows (Integer)

    when :all is passed, the maximum number of rows to load from solr

  • :cast (Boolean)

    when true, examine the model and cast it to the first known cModel



104
105
106
107
108
109
110
111
112
# File 'lib/active_fedora/model.rb', line 104

def find(args, opts={})
  return find_one(args, opts[:cast]) if args.class == String
  args = {} if args == :all
  results = []
  find_each(args, opts) do |obj|
     results << obj
  end
  results
end

#find_by_fields_by_solr(query_fields, opts = {}) ⇒ Object

Deprecated.

Find all ActiveFedora objects for this model that match arguments passed in by querying Solr. Like find_by_solr this returns a solr result.

query_fields a hash of object field names and values to filter on (query_fields must be the solr_field_name for non-MetadataDatastream derived datastreams) opts specifies options for the solr query

options may include:

:sort             => array of hash with one hash per sort by field... defaults to [{system_create=>:descending}]
:default_field, :rows, :filter_queries, :debug_query,
:explain_other, :facets, :highlighting, :mlt,
:operator         => :or / :and
:start            => defaults to 0
:field_list       => array, defaults to ["*", "score"]


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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/active_fedora/model.rb', line 231

def find_by_fields_by_solr(query_fields,opts={})
  ActiveSupport::Deprecation.warn("find_by_fields_by_solr is deprecated and will be removed in the next release")
  #create solr_args from fields passed in, needs to be comma separated list of form field1=value1,field2=value2,...
  escaped_class_name = self.name.gsub(/(:)/, '\\:')
  query = "#{ActiveFedora::SolrService.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}" 
  
  query_fields.each_pair do |key,value|
    unless value.nil?
      solr_key = key
      #convert to symbol if need be
      key = key.to_sym if !class_fields.has_key?(key)&&class_fields.has_key?(key.to_sym)
      #do necessary mapping with suffix in most cases, otherwise ignore as a solr field key that activefedora does not know about
      if class_fields.has_key?(key) && class_fields[key].has_key?(:type)
        type = class_fields[key][:type]
        type = :string unless type.kind_of?(Symbol)
        solr_key = ActiveFedora::SolrService.solr_name(key,type)
      end
      
      escaped_value = value.gsub(/(:)/, '\\:')
      #escaped_value = escaped_value.gsub(/ /, '\\ ')
      key = SOLR_DOCUMENT_ID if (key === :id || key === :pid)
      query = key.to_s.eql?(SOLR_DOCUMENT_ID) ? "#{query} AND #{key}:#{escaped_value}" : "#{query} AND #{solr_key}:#{escaped_value}"  
    end
  end

  query_opts = {}
  opts.each do |key,value|
    key = key.to_sym
    query_opts[key] = value
  end

  #set default sort to created date ascending
  unless query_opts.include?(:sort)
    query_opts.merge!({:sort=>[ActiveFedora::SolrService.solr_name(:system_create,:date)+' asc']}) 
  else
    #need to convert to solr names for all fields
    sort_array =[]
  
    opts[:sort].collect do |sort|
      sort_direction = 'ascending'
      if sort.respond_to?(:keys)
        key = sort.keys[0]
        sort_direction = sort[key]
      else
        key = sort.to_s
      end
      sort_direction = sort_direction =~ /^desc/ ? 'desc' : 'asc'
      field_name = key
      
      if key.to_s =~ /^system_create/
        field_name = :system_create_date
        key = :system_create
      elsif key.to_s =~ /^system_mod/  
        field_name = :system_modified_date
        key = :system_modified
      end
   
      solr_name = field_name 
      if class_fields.include?(field_name.to_sym)
        solr_name = ActiveFedora::SolrService.solr_name(key,class_fields[field_name.to_sym][:type])
      end
      sort_array.push("#{solr_name} #{sort_direction}")
    end
  
    query_opts[:sort] = sort_array.join(",")
  end

  logger.debug "Querying solr for #{self.name} objects with query: '#{query}'"
  SolrService.query(query, query_opts) 
end

#find_by_solr(query, args = {}) ⇒ Object

If query is :all, this method will query Solr for all instances

of self.type (based on active_fedora_model_s as indexed
by Solr). If the query is any other string, this method simply does
a pid based search (id:query). 

Note that this method does _not_ return ActiveFedora::Model 
objects, but rather an array of SolrResults.

Args is an options hash, which is passed into the SolrService 
connection instance.


205
206
207
208
209
210
211
212
213
# File 'lib/active_fedora/model.rb', line 205

def find_by_solr(query, args={})
  if query == :all
    escaped_class_name = self.name.gsub(/(:)/, '\\:')
    SolrService.query("#{ActiveFedora::SolrService.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}", args) 
  elsif query.class == String
    escaped_id = query.gsub(/(:)/, '\\:')          
    SolrService.query("#{SOLR_DOCUMENT_ID}:#{escaped_id}", args) 
  end
end

#find_each(conditions = {}, opts = {}) ⇒ Object

Yields the found ActiveFedora::Base object to the passed block

Parameters:

  • conditions (Hash) (defaults to: {})

    the conditions for the solr search to match

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :cast (Boolean)

    when true, examine the model and cast it to the first known cModel



154
155
156
157
158
159
160
# File 'lib/active_fedora/model.rb', line 154

def find_each( conditions={}, opts={})
  find_in_batches(conditions, opts.merge({:fl=>SOLR_DOCUMENT_ID})) do |group|
    group.each do |hit|
      yield(find_one(hit[SOLR_DOCUMENT_ID], opts[:cast]))
    end
  end
end

#find_in_batches(conditions, opts = {}) ⇒ Object

Yields each batch of solr records that was found by the find options as an array. The size of each batch is set by the :batch_size option; the default is 1000.

Returns a solr result matching the supplied conditions @param conditions solr conditions to match @param options

Examples:

Person.find_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
group.each { |person| puts person['name_t'] }
end

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :sort (Array)

    a list of fields to sort by

  • :rows (Array)

    number of rows to return



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/active_fedora/model.rb', line 130

def find_in_batches conditions, opts={}
  opts[:q] = create_query(conditions)
  opts[:qt] = 'standard'
  #set default sort to created date ascending
  unless opts.include?(:sort)
    opts[:sort]=[ActiveFedora::SolrService.solr_name(:system_create,:date)+' asc'] 
  end

  batch_size = opts.delete(:batch_size) || 1000

  counter = 0
  begin
    counter += 1
    response = ActiveFedora::SolrService.instance.conn.paginate counter, batch_size, "select", :params => opts
    docs = response["response"]["docs"]
    yield docs
  end while docs.has_next? 
end

#find_model(pid) ⇒ Object

Deprecated.


172
173
174
175
# File 'lib/active_fedora/model.rb', line 172

def find_model(pid)
  ActiveSupport::Deprecation.warn("find_model is deprecated.  Use find instead")
  find(pid)
end

#find_with_conditions(conditions, opts = {}) ⇒ Object

Returns a solr result matching the supplied conditions @param conditions solr conditions to match @param options

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :sort (Array)

    a list of fields to sort by

  • :rows (Array)

    number of rows to return



307
308
309
310
311
312
313
314
# File 'lib/active_fedora/model.rb', line 307

def find_with_conditions(conditions, opts={})
  query = create_query(conditions)
  #set default sort to created date ascending
  unless opts.include?(:sort)
    opts[:sort]=[ActiveFedora::SolrService.solr_name(:system_create,:date)+' asc'] 
  end
  SolrService.query(query, opts) 
end

#load_instance(pid) ⇒ Object

Retrieve the Fedora object with the given pid and deserialize it as an instance of the current model Note that you can actually pass a pid into this method, regardless of Fedora model type, and ActiveFedora will try to parse the results into the current type of self, which may or may not be what you want.

Examples:

this will return an instance of Book, even if the object hydra:dataset1 asserts that it is a Dataset

Book.load_instance("hydra:dataset1") 

Parameters:

  • pid (String)

    of the object to load



74
75
76
77
# File 'lib/active_fedora/model.rb', line 74

def load_instance(pid)
  ActiveSupport::Deprecation.warn("load_instance is deprecated.  Use find instead")
  find(pid)
end

#quote_for_solr(value) ⇒ Object



339
340
341
# File 'lib/active_fedora/model.rb', line 339

def quote_for_solr(value)
  '"' + value.gsub(/(:)/, '\\:').gsub(/"/, '\\"') + '"'
end

#solr_search(query, args = {}) ⇒ Object

Deprecated.

Sends a query directly to SolrService



189
190
191
192
# File 'lib/active_fedora/model.rb', line 189

def solr_search(query, args={})
  ActiveSupport::Deprecation.warn("solr_search is deprecated and will be removed in the next release. Use SolrService.query instead")
  SolrService.instance.conn.query(query, args)
end

#to_class_uri(attrs = {}) ⇒ Object

Returns a suitable uri object for :has_model Should reverse Model#from_class_uri



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_fedora/model.rb', line 83

def to_class_uri(attrs = {})
  unless self.respond_to? :pid_suffix
    pid_suffix = attrs.has_key?(:pid_suffix) ? attrs[:pid_suffix] : ContentModel::CMODEL_PID_SUFFIX
  else
    pid_suffix = self.pid_suffix
  end
  unless self.respond_to? :pid_namespace
    namespace = attrs.has_key?(:namespace) ? attrs[:namespace] : ContentModel::CMODEL_NAMESPACE   
  else
    namespace = self.pid_namespace
  end
  "info:fedora/#{namespace}:#{ContentModel.sanitized_class_name(self)}#{pid_suffix}" 
end