Module: TaliaCore::ActiveSourceParts::Finders

Included in:
TaliaCore::ActiveSource
Defined in:
lib/talia_core/active_source_parts/finders.rb

Overview

Class method for ActiveSource that deal with #find and friends, and other forms of querying the data store.

Instance Method Summary collapse

Instance Method Details

#count(*args) ⇒ Object

The count for ActiveSource will accept the same options as the find method



60
61
62
63
64
65
66
67
# File 'lib/talia_core/active_source_parts/finders.rb', line 60

def count(*args)
  if((options = args.last).is_a?(Hash))
    options.to_options!
    options.delete(:prefetch_relations) # This is not relevant for counting
    prepare_options!(options)
  end
  super
end

#find(*args) ⇒ Object

Extends the functionality of the ActiveRecord #find. This version also accepts URIs as “ids” and has a few additional options:

find_through

accepts and array with an predicate name and an object value/uri, to search for predicates that match the given predicate/value combination

type

specifically looks for sources with the given type.

find_through_inv

like :find_through, but for the “inverse” lookup

prefetch_relations

if set to “true”, this will pre-load all semantic relations for the sources (experimental, not fully implemented yet)

Examples:

# 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)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/talia_core/active_source_parts/finders.rb', line 29

def find(*args)
  prefetching = false
  if(args.last.is_a?(Hash))
    options = args.last
    options.to_options!
    
    # Hack the "default" ordering
    options[:order] = 'id' if(options[:order] == :default)
    
    prefetching =  options.delete(:prefetch_relations)
    if(options.empty?) # If empty we remove the args hash, so that the 1-param uri search works
      args.pop
    else
      prepare_options!(options)
    end
  end
  
  result = if(args.size == 1 && (uri_s = uri_string_for(args[0])))
    src = super(:first, :conditions => { :uri => uri_s })
    raise(ActiveRecord::RecordNotFound, "Not found: #{uri_s}") unless(src)
    src
  else
    super
  end

  prefetch_relations_for(result) if(prefetching)

  result
end

#find_by_partial_local(namespace, local_part, options = {}) ⇒ Object

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



92
93
94
95
96
97
98
# File 'lib/talia_core/active_source_parts/finders.rb', line 92

def find_by_partial_local(namespace, local_part, options = {})
  namesp = N::URI[namespace]
  return [] unless(namesp)
  find(:all, { 
    :conditions => [ 'uri LIKE ?', "#{namesp.uri}#{local_part}%" ], 
    :order => "uri ASC"}.merge!(options))
end

#find_by_partial_uri(id, options = {}) ⇒ Object

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.



102
103
104
# File 'lib/talia_core/active_source_parts/finders.rb', line 102

def find_by_partial_uri(id, options = {})
  find(:all, { :conditions => ["uri LIKE ?", '%' + id + '%'] }.merge!(options))
end

#find_by_uri_token(token, options = {}) ⇒ Object

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

Example

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.



84
85
86
87
88
# File 'lib/talia_core/active_source_parts/finders.rb', line 84

def find_by_uri_token(token, options = {})
  find(:all, { 
    :conditions => [ "LOWER(SUBSTRING_INDEX(uri, '/', -1)) LIKE ?", '%' + token.downcase + '%' ], 
    :order => "uri ASC" }.merge!(options))
end