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

#all(opts = {}, &block) ⇒ Object



101
102
103
# File 'lib/active_fedora/model.rb', line 101

def all(opts = {}, &block)
  find(:all, opts, &block)
end

#count(args = {}) ⇒ Object

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



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

def count(args = {})
  q = search_model_clause ? [search_model_clause] : []
  q << "#{args[:conditions]}"  if args[:conditions]
  SolrService.query(q.join(' AND '), :raw=>true, :rows=>0)['response']['numFound']
end

#exists?(pid) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

#find(args, opts = {}, &block) ⇒ 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



91
92
93
94
95
96
97
98
99
# File 'lib/active_fedora/model.rb', line 91

def find(args, opts={}, &block)
  return find_one(args, opts[:cast]) if args.class == String
  return to_enum(:find, args, opts).to_a unless block_given?

  args = {} if args == :all
  find_each(args, opts) do |obj|
    yield obj
  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



145
146
147
148
149
150
151
# File 'lib/active_fedora/model.rb', line 145

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_fedora/model.rb', line 121

def find_in_batches conditions, opts={}
  opts[:q] = create_query(conditions)
  opts[:qt] = solr_query_handler
  #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_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

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



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

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

#quote_for_solr(value) ⇒ Object



186
187
188
# File 'lib/active_fedora/model.rb', line 186

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

#to_class_uri(attrs = {}) ⇒ Object

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_fedora/model.rb', line 70

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