Class: Spectro::Database

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/spectro/database.rb

Overview

Gives access to the current collection of algorithms (lambdas) providing several ways to fetch specific elements by different criteria.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



17
18
19
# File 'lib/spectro/database.rb', line 17

def initialize
  self.cache = {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



15
16
17
# File 'lib/spectro/database.rb', line 15

def cache
  @cache
end

Instance Method Details

#fetch(file_path, method_name, *required_params) ⇒ Proc

Fetches and returns the target lambda based on the given class, method name and required aprameters.

Parameters:

  • file_path (String)

    relative path of the file that requests the lambda

  • method_name (Symbol)

    the method name that would be implemented

  • required_params (<Symbol>)

    parameters that would be required by the lambda

Returns:

  • (Proc)

    the labda that would be implemented



41
42
43
44
45
46
47
# File 'lib/spectro/database.rb', line 41

def fetch file_path, method_name, *required_params
  if self.index["#{file_path}"].nil? || self.index["#{file_path}"]["#{method_name}"].nil?
    return nil
  end
  λ_id = self.index["#{file_path}"]["#{method_name}"]['lambda_id']
  return self.cache[λ_id] ||= eval(File.read(".spectro/cache/#{λ_id}.rb"))
end

#indexHash

Lazy loads the index.yml and returns it

Returns:

  • (Hash)

    the parsed index.yml



24
25
26
# File 'lib/spectro/database.rb', line 24

def index
  return @index ||= load_index()
end

#reset_indexObject

Sets the index cache to nil Just in case you want the database to parse the file once again



30
31
32
# File 'lib/spectro/database.rb', line 30

def reset_index
  @index = nil
end