class Spectro::Database

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

Attributes

cache[RW]

Public Class Methods

new() click to toggle source
# File lib/spectro/database.rb, line 17
def initialize
  self.cache = {}
end

Public Instance Methods

fetch(file_path, method_name, *required_params) click to toggle source

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

@param [String] file_path relative path of the file that requests the lambda @param [Symbol] method_name the method name that would be implemented @param [<Symbol>] required_params parameters that would be required by the lambda @return [Proc] the labda that would be implemented

# 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
index() click to toggle source

Lazy loads the index.yml and returns it

@return [Hash] the parsed index.yml

# File lib/spectro/database.rb, line 24
def index
  return @index ||= load_index()
end
reset_index() click to toggle source

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

# File lib/spectro/database.rb, line 30
def reset_index
  @index = nil
end

Private Instance Methods

load_index() click to toggle source

Loads and returns the current project index or returns an empty one if not found

# File lib/spectro/database.rb, line 53
def load_index
  return {} if !File.exist?('.spectro/index.yml')

  return YAML.load_file('.spectro/index.yml') || {}
end