Module: ActiveFedora::FinderMethods
- Included in:
- Relation
- Defined in:
- lib/active_fedora/relation/finder_methods.rb
Instance Method Summary collapse
- #exists?(conditions) ⇒ Boolean
-
#find(*args) ⇒ Object
Returns an Array of objects of the Class that
find
is being called on. -
#find_each(conditions = {}, opts = {}) ⇒ Object
Yields the found ActiveFedora::Base object to the passed block.
-
#find_in_batches(conditions, opts = {}) ⇒ Object
Yields each batch of solr records that was found by the find
options
as an array. -
#find_one(pid, cast = nil) ⇒ Object
Retrieve the Fedora object with the given pid, explore the returned object, determine its model using #ContentModel.known_models_for and cast to that class.
-
#find_with_conditions(conditions, opts = {}) ⇒ Object
Returns a solr result matching the supplied conditions @param conditions can either be specified as a string, or hash representing the query part of an solr statement.
-
#first ⇒ Object
Returns the first records that was found.
-
#last ⇒ Object
Returns the last record sorted by id.
Instance Method Details
#exists?(conditions) ⇒ Boolean
66 67 68 69 70 71 72 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 66 def exists?(conditions) conditions = conditions.id if Base === conditions return false if !conditions !!DigitalObject.find(self.klass, conditions) rescue ActiveFedora::ObjectNotFoundError false end |
#find(*args) ⇒ Object
Returns an Array of objects of the Class that find
is being called on
@param args either a pid or a hash of conditions
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 37 def find(*args) return to_a.find { |*block_args| yield(*block_args) } if block_given? = args. = .dup cast = if @klass == ActiveFedora::Base && !.has_key?(:cast) true else .delete(:cast) end if [:sort] # Deprecate sort sometime? sort = .delete(:sort) [:order] ||= sort if sort.present? end if .present? = args.first unless args.empty? = {conditions: } () else raise ArgumentError, "#{self}.find() expects a pid. You provided `#{args.inspect}'" unless args.is_a? Array find_with_ids(args, cast) end end |
#find_each(conditions = {}, opts = {}) ⇒ Object
Yields the found ActiveFedora::Base object to the passed block
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 95 def find_each( conditions={}, opts={}) cast = opts.delete(:cast) find_in_batches(conditions, opts.merge({:fl=>SOLR_DOCUMENT_ID})) do |group| group.each do |hit| begin yield(load_from_fedora(hit[SOLR_DOCUMENT_ID], cast)) rescue ActiveFedora::ObjectNotFoundError logger.error "Although #{hit[SOLR_DOCUMENT_ID]} was found in Solr, it doesn't seem to exist in Fedora. The index is out of synch." end end end end |
#find_in_batches(conditions, opts = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 123 def find_in_batches conditions, opts={} opts[:q] = create_query(conditions) opts[:qt] = @klass.solr_query_handler #set default sort to created date ascending unless opts[:sort].present? opts[:sort]= @klass.default_sort_params 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_one(pid, cast = nil) ⇒ Object
Retrieve the Fedora object with the given pid, explore the returned object, determine its model using #ContentModel.known_models_for and cast to that class. Raises a ObjectNotFoundError if the object is not found.
150 151 152 153 154 155 156 157 158 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 150 def find_one(pid, cast=nil) if where_values.empty? load_from_fedora(pid, cast) else conditions = where_values + [ActiveFedora::SolrService.raw_query(SOLR_DOCUMENT_ID, pid)] query = conditions.join(" AND ".freeze) to_enum(:find_each, query, {}).to_a.first end end |
#find_with_conditions(conditions, opts = {}) ⇒ Object
Returns a solr result matching the supplied conditions @param conditions can either be specified as a string, or hash representing the query part of an solr statement. If a hash is provided, this method will generate conditions based simple equality combined using the boolean AND operator. @param options
82 83 84 85 86 87 88 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 82 def find_with_conditions(conditions, opts={}) #set default sort to created date ascending unless opts.include?(:sort) opts[:sort]=@klass.default_sort_params end SolrService.query(create_query(conditions), opts) end |
#first ⇒ Object
Returns the first records that was found.
9 10 11 12 13 14 15 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 9 def first if loaded? @records.first else @first ||= limit(1).to_a[0] end end |
#last ⇒ Object
Returns the last record sorted by id. ID was chosen because this mimics how ActiveRecord would achieve the same behavior.
23 24 25 26 27 28 29 |
# File 'lib/active_fedora/relation/finder_methods.rb', line 23 def last if loaded? @records.last else @last ||= order('id desc').limit(1).to_a[0] end end |