class Functio::FunctionRepository

Repository for function management.

Attributes

function_class[R]

Class of the function instances this repository manages.

id_attr[R]

Symbol with the name of the attribute that identifies a function instance.

storage[R]

Storage instance used for function persistence.

Public Class Methods

new(configs = {}) click to toggle source

Constructs a FunctionRepository instance passing in a configs Hash. The configs Hash can contain a :storage instance, defaults to a DataStorage instance; a :function_class, defaults to Function; and an :id_attr Symbol with a name of the attribute that identifies the function instances, defaults to :name.

# File lib/functio/function_repository.rb, line 40
def initialize(configs = {})
  @storage = configs.fetch(:storage, DataStorage.new)
  @function_class = configs.fetch(:function_class, Function)
  @id_attr = configs.fetch(:id_attr, :name)
end

Public Instance Methods

add(function) click to toggle source

Adds a function instance to repository and returns true. It doesn't add the function and returns false if its id_attr already exists in the repository.

# File lib/functio/function_repository.rb, line 49
def add(function)
  return false if find(function.attributes[id_attr])
  storage.store(function.attributes)
  true
end
all() click to toggle source

Retrieves all function instances in the repository.

# File lib/functio/function_repository.rb, line 56
def all
  storage.all.map { |record| function_class.build(record) }
end
delete(id) click to toggle source

Deletes a function instance from the repository by its id attribute. Returns true if the function instance is deleted successfully. Returns false otherwise.

# File lib/functio/function_repository.rb, line 69
def delete(id)
  storage.delete(id_attr => id)
end
find(id) click to toggle source

Finds a function instance by its id attribute. Returns nil otherwise.

# File lib/functio/function_repository.rb, line 61
def find(id)
  found = storage.find(id_attr => id)
  function_class.build(found) if found
end