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
-
#attribute_get(name) ⇒ Object
wrapper around instance_variable_get, returns current value of @name.
-
#attribute_set(name, value) ⇒ Object
wrapper around instance_variable_set, sets @name to value.
-
#find(args) ⇒ Object
Takes :all or a pid as arguments Returns an Array of objects of the Class that
find
is being called on. -
#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).
-
#load_instance(pid) ⇒ Object
Load an instance with the following pid.
-
#solr_search(query, args = {}) ⇒ Object
Sends a query directly to SolrService.
Instance Method Details
#attribute_get(name) ⇒ Object
wrapper around instance_variable_get, returns current value of @name
106 107 108 109 |
# File 'lib/active_fedora/model.rb', line 106 def attribute_get(name) #instance_variable_get(properties[":#{name}"].instance_variable_name) instance_variable_get("@#{name}") end |
#attribute_set(name, value) ⇒ Object
wrapper around instance_variable_set, sets @name to value
101 102 103 |
# File 'lib/active_fedora/model.rb', line 101 def attribute_set(name, value) instance_variable_set("@#{name}", value) end |
#find(args) ⇒ Object
Takes :all or a pid as arguments Returns an Array of objects of the Class that find
is being called on
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_fedora/model.rb', line 56 def find(args) if args == :all escaped_class_name = self.name.gsub(/(:)/, '\\:') q = "#{SolrMapper.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}" elsif args.class == String escaped_id = args.gsub(/(:)/, '\\:') q = "#{SOLR_DOCUMENT_ID}:#{escaped_id}" end hits = SolrService.instance.conn.query(q).hits 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 results.first 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.
89 90 91 92 93 94 95 96 97 |
# File 'lib/active_fedora/model.rb', line 89 def find_by_solr(query, args={}) if query == :all escaped_class_name = self.name.gsub(/(:)/, '\\:') SolrService.instance.conn.query("#{SolrMapper.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
Load an instance with the following pid. Note that you can actually pass an 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.
49 50 51 |
# File 'lib/active_fedora/model.rb', line 49 def load_instance(pid) Fedora::Repository.instance.find_model(pid, self) end |
#solr_search(query, args = {}) ⇒ Object
Sends a query directly to SolrService
74 75 76 |
# File 'lib/active_fedora/model.rb', line 74 def solr_search(query, args={}) SolrService.instance.conn.query(query, args) end |