class Functio::FunctionRepository
Repository for function management.
Attributes
Class of the function instances this repository manages.
Symbol with the name of the attribute that identifies a function instance.
Storage instance used for function persistence.
Public Class Methods
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
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
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
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
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