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 criterias.

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 return 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


35
36
37
38
39
40
41
# File 'lib/spectro/database.rb', line 35

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
  @index ||= YAML.load_file('./.spectro/index.yml')
end