module Scrivito # This class allows you to retrieve Objects from a specific working copy. # You can get an instance by accessing {Workspace#objs}. # @api public class ObjCollection attr_reader :workspace def initialize(workspace) @workspace = workspace end # Find a {BasicObj Obj} by its id. # If the parameter is an Array containing ids, return a list of corresponding Objs. # @param [String, Integer, Array]id_or_list # @return [Obj, Array] # @api public def find(id_or_list) find_using(id_or_list, :find_by) end # Find a {BasicObj Obj} by its id. # If the parameter is an Array containing ids, return a list of corresponding Objs. # The results include deleted objects as well. # @param [String, Integer, Array]id_or_list # @return [Obj, Array] # @api public def find_including_deleted(id_or_list) find_using(id_or_list, :find_by_including_deleted) end # Find the {BasicObj Obj} with the given path. # Returns +nil+ if no matching Obj exists. # @param [String] path Path of the {BasicObj Obj}. # @return [Obj] # @api public def find_by_path(path) find_by(:path, [path]).first.first end # Returns the {BasicObj Obj} with the given permalink, or +nil+ if no matching Obj exists. # @param [String] permalink The permalink of the {BasicObj Obj}. # @return [Obj] # @api public def find_by_permalink(permalink) find_by(:permalink, [permalink]).first.first end # Returns a {ObjSearchEnumerator} with the given initial subquery consisting of the four arguments. # # Note that +field+ and +value+ can also be arrays for searching several fields or searching for several values. # # {ObjSearchEnumerator}s can be chained using one of the chainable methods (e.g. {ObjSearchEnumerator#and} and {ObjSearchEnumerator#and_not}). # # @param [Symbol, String, Array] field See {ObjSearchEnumerator#and} for details # @param [Symbol, String] operator See {ObjSearchEnumerator#and} for details # @param [String, Array] value See {ObjSearchEnumerator#and} for details # @param [Hash] boost See {ObjSearchEnumerator#and} for details # @return [ObjSearchEnumerator] # @api public def where(field, operator, value, boost = nil) ObjSearchEnumerator.new(workspace) .and(field, operator, value, boost) end # Returns a {ObjSearchEnumerator} of all {BasicObj Obj}s. # @return [ObjSearchEnumerator] # @api public def all ObjSearchEnumerator.new(workspace).batch_size(1000) end # Returns a {ObjSearchEnumerator} of all Objs with the given +obj_class+. # @param [String] obj_class Name of the ObjClass. # @return [ObjSearchEnumerator] # @api public def find_all_by_obj_class(obj_class) all.and(:_obj_class, :equals, obj_class) end def find_by_parent_path(path) find_by(:ppath, [path]).first end def find_by_id(id) find_by(:id, [id]).first.first end def find_by_paths(paths) find_by(:path, paths).map(&:first) end def changes(offset, batch_size) where('_modification', 'equals', ['new', 'edited', 'deleted']) .batch_size(batch_size) .offset(offset) .include_deleted end private def find_using(id_or_list, method) case id_or_list when Array send(method, :id, id_or_list).map(&:first).compact else obj = send(method, :id, [id_or_list.to_s]).first.first obj or raise ResourceNotFound, "Could not find Obj with id #{id_or_list}" end end def find_by(view, keys) find_by_including_deleted(view, keys).map do |list| list.reject(&:deleted?) end end # accepts the name of an "obj_by" - view, a list of keys # returns a list of lists of Objs: a list of Objs for each given keys. def find_by_including_deleted(view, keys) result = CmsBackend.instance.find_obj_data_by( workspace.revision, view, keys) result.map do |list| list.map do |obj_data| obj = BasicObj.instantiate(obj_data) obj.revision = workspace.revision obj end end end end end