| Module | TaliaCore::ActiveSourceParts::Finders |
| In: |
lib/talia_core/active_source_parts/finders.rb
|
Class method for ActiveSource that deal with find and friends, and other forms of querying the data store.
The count for ActiveSource will accept the same options as the find method
# File lib/talia_core/active_source_parts/finders.rb, line 60
60: def count(*args)
61: if((options = args.last).is_a?(Hash))
62: options.to_options!
63: options.delete(:prefetch_relations) # This is not relevant for counting
64: prepare_options!(options)
65: end
66: super
67: end
Extends the functionality of the ActiveRecord find. This version also accepts URIs as "ids" and has a few additional options:
# With a URI as id
ActiveSource.find(N::LOCAL.mySource)
# A URI as a string, same as above
ActiveSource.find('http://www.foobar.org')
# Find through a given attribute, and prefetch all attributes for the found sources
ActiveSource.find(:all, :find_through => [N::DCT.creator, N::LOCAL.schopenhauer], :prefetch_relations => true)
# File lib/talia_core/active_source_parts/finders.rb, line 29
29: def find(*args)
30: prefetching = false
31: if(args.last.is_a?(Hash))
32: options = args.last
33: options.to_options!
34:
35: # Hack the "default" ordering
36: options[:order] = 'id' if(options[:order] == :default)
37:
38: prefetching = options.delete(:prefetch_relations)
39: if(options.empty?) # If empty we remove the args hash, so that the 1-param uri search works
40: args.pop
41: else
42: prepare_options!(options)
43: end
44: end
45:
46: result = if(args.size == 1 && (uri_s = uri_string_for(args[0])))
47: src = super(:first, :conditions => { :uri => uri_s })
48: raise(ActiveRecord::RecordNotFound, "Not found: #{uri_s}") unless(src)
49: src
50: else
51: super
52: end
53:
54: prefetch_relations_for(result) if(prefetching)
55:
56: result
57: end
Find the Sources within the given namespace by a partial local name. Works like find_by_uri_token, except that only sources from the given namspace are returned
# File lib/talia_core/active_source_parts/finders.rb, line 92
92: def find_by_partial_local(namespace, local_part, options = {})
93: namesp = N::URI[namespace]
94: return [] unless(namesp)
95: find(:all, {
96: :conditions => [ 'uri LIKE ?', "#{namesp.uri}#{local_part}%" ],
97: :order => "uri ASC"}.merge!(options))
98: end
Find the fist Source that matches the given URI. This works like find_by_uri_token, except that the whole URI is matched, not only the local name.
# File lib/talia_core/active_source_parts/finders.rb, line 102
102: def find_by_partial_uri(id, options = {})
103: find(:all, { :conditions => ["uri LIKE ?", '%' + id + '%'] }.merge!(options))
104: end
Find a list of sources which contains the given token inside the local name. This means that the namespace it will be excluded from the toke search
Sources in system:
With these sources, you will get:
Source.find_by_uri_token('a') # => [ ]
Source.find_by_uri_token('o') # => [ 'http://talia.org/one', 'http://talia.org/two' ]
NOTE: It internally uses a MySQL function, as sql condition, to find the local name of the uri.
# File lib/talia_core/active_source_parts/finders.rb, line 84
84: def find_by_uri_token(token, options = {})
85: find(:all, {
86: :conditions => [ "LOWER(SUBSTRING_INDEX(uri, '/', -1)) LIKE ?", '%' + token.downcase + '%' ],
87: :order => "uri ASC" }.merge!(options))
88: end