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

#attribute_get(name) ⇒ Object

wrapper around instance_variable_get, returns current value of @name



222
223
224
# File 'lib/active_fedora/model.rb', line 222

def attribute_get(name)
  instance_variable_get("@#{name}")
end

#attribute_set(name, value) ⇒ Object

wrapper around instance_variable_set, sets @name to value



217
218
219
# File 'lib/active_fedora/model.rb', line 217

def attribute_set(name, value)
  instance_variable_set("@#{name}", value)
end

#class_fieldsObject



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

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

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

Takes :all or a pid as arguments Returns an Array of objects of the Class that find is being called on



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_fedora/model.rb', line 59

def find(args, opts={})
  opts = {:rows=>25}.merge(opts)
  return_multiple = false
  if args == :all
    return_multiple = true
    escaped_class_name = self.name.gsub(/(:)/, '\\:')
    q = "#{ActiveFedora::SolrService.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}"
  elsif args.class == String
    escaped_id = args.gsub(/(:)/, '\\:')
    q = "#{SOLR_DOCUMENT_ID}:#{escaped_id}"
  end
  if return_multiple == true
    hits = SolrService.instance.conn.query(q, :rows=>opts[:rows]).hits 
  else
    hits = SolrService.instance.conn.query(q).hits 
  end
  results = hits.map do |hit|
    obj = Fedora::Repository.instance.find_model(hit[SOLR_DOCUMENT_ID], self)
    #obj.inner_object.new_object = false
    #return obj
  end
  if return_multiple == true
    return results
  else
    return results.first
  end
end

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

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 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"]


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
156
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/active_fedora/model.rb', line 128

def find_by_fields_by_solr(query_fields,opts={})
  #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)=>:ascending]}) 
  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]
        sort_direction =~ /^desc/ ? sort_direction = :descending : :ascending
      else
        key = sort.to_s
      end
      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
  end

  logger.debug "Querying solr for #{self.name} objects with query: '#{query}'"
    	results = ActiveFedora::SolrService.instance.conn.query(query,query_opts)
    	#objects = []
  #  results.hits.each do |hit|
  #    puts "get object for #{hit[SOLR_DOCUMENT_ID]}"
  #    obj = Fedora::Repository.instance.find_model(hit[SOLR_DOCUMENT_ID], self)
  #    obj.inner_object.new_object = false
  #    objects.push(obj)
  #end
  #objects
  #ActiveFedora::SolrService.reify_solr_results(results)
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.


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

def find_by_solr(query, args={})
  if query == :all
    escaped_class_name = self.name.gsub(/(:)/, '\\:')
    SolrService.instance.conn.query("#{ActiveFedora::SolrService.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}", args)
  elsif query.class == String
    escaped_id = query.gsub(/(:)/, '\\:')          
    SolrService.instance.conn.query("#{SOLR_DOCUMENT_ID}:#{escaped_id}", args)
  end
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



52
53
54
# File 'lib/active_fedora/model.rb', line 52

def load_instance(pid)
  Fedora::Repository.instance.find_model(pid, self)
end

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

Sends a query directly to SolrService



88
89
90
# File 'lib/active_fedora/model.rb', line 88

def solr_search(query, args={})
  SolrService.instance.conn.query(query, args)
end