Class: Trith::Cache

Inherits:
RDF::Repository
  • Object
show all
Defined in:
lib/trith/cache.rb

Overview

The Trith code cache.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cache) initialize(options = {}, &block)

A new instance of Cache

Parameters:

  • (Hash{Symbol => Object} options) Hash{Symbol

    => Object} options



28
29
30
31
# File 'lib/trith/cache.rb', line 28

def initialize(options = {}, &block)
  @data = []
  super(options, &block)
end

Class Method Details

+ (Trith::Cache) load(*filenames)

Parameters:

  • (Array<String>) filenames

Returns:



18
19
20
21
22
23
24
# File 'lib/trith/cache.rb', line 18

def self.load(*filenames)
  self.new do |repository|
    filenames.each do |filename|
      repository.load(filename)
    end
  end
end

+ (Trith::Cache) load_core

Returns a cache containing all core functions.

Returns:



11
12
13
# File 'lib/trith/cache.rb', line 11

def self.load_core
  load(*Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'etc', '*.nt'))))
end

Instance Method Details

- (Enumerator<Trith::Function>) each_function(&block)

Enumerates each function in the cache.

Returns:



61
62
63
64
65
66
67
68
69
# File 'lib/trith/cache.rb', line 61

def each_function(&block)
  unless block_given?
    Enumerator.new(self, :each_function)
  else
    query(:predicate => RDF.type, :object => Trith::Function::URI) do |statement|
      block.call(Trith::Function.new(statement.subject, :data => self))
    end
  end
end

- (Enumerator<Trith::Function>) find_functions(options = {}, &block)

Finds functions fulfilling the given criteria.

Parameters:

  • (Hash{Symbol => Object}) options (defaults to: {})

Options Hash (options):

  • (Symbol, #to_s) :label N/A

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/trith/cache.rb', line 77

def find_functions(options = {}, &block)
  unless block_given?
    Enumerator.new(self, :find_functions, options)
  else
    # FIXME: would really need BGP query support in RDF.rb...
    each_function do |function|
      if options.empty?
        block.call(function)
      else
        pattern = case
          when options[:label]
            {:predicate => RDF::RDFS.label, :object => options[:label].to_s}
          else {}
        end
        block.call(function) unless query({:subject => function.id}.merge(pattern)).empty?
      end
    end
  end
end

- (Boolean) has_function?(function)

Returns true if the given function is cached.

Parameters:

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/trith/cache.rb', line 38

def has_function?(function)
  id = case function
    when Trith::Function then function.id
    else Trith::Function.new(function).id
  end
  !query(:subject => id, :predicate => RDF.type, :object => Trith::Function::URI).empty?
end

- (Boolean) has_label?(label, options = {})

Returns true if a function with the given label is cached.

Parameters:

  • (Symbol, #to_s) label
  • (Hash{Symbol => Object} options) Hash{Symbol

    => Object} options

  • (Hash) options (defaults to: {})

    a customizable set of options

Options Hash (options):

  • (Symbol, #to_s) :language — default: nil

Returns:

  • (Boolean)


53
54
55
# File 'lib/trith/cache.rb', line 53

def has_label?(label, options = {})
  !query(:predicate => RDF::RDFS.label, :object => RDF::Literal.new(label.to_s, options)).empty?
end