Class: Functio::FunctionRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/functio/function_repository.rb

Overview

Repository for function management.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs = {}) ⇒ FunctionRepository

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.



40
41
42
43
44
# 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

Instance Attribute Details

#function_classObject (readonly)

Class of the function instances this repository manages.



33
34
35
# File 'lib/functio/function_repository.rb', line 33

def function_class
  @function_class
end

#id_attrObject (readonly)

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



27
28
29
# File 'lib/functio/function_repository.rb', line 27

def id_attr
  @id_attr
end

#storageObject (readonly)

Storage instance used for function persistence.



30
31
32
# File 'lib/functio/function_repository.rb', line 30

def storage
  @storage
end

Instance Method Details

#add(function) ⇒ Object

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.



49
50
51
52
53
# 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

#allObject

Retrieves all function instances in the repository.



56
57
58
# File 'lib/functio/function_repository.rb', line 56

def all
  storage.all.map { |record| function_class.build(record) }
end

#delete(id) ⇒ Object

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



69
70
71
# File 'lib/functio/function_repository.rb', line 69

def delete(id)
  storage.delete(id_attr => id)
end

#find(id) ⇒ Object

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



61
62
63
64
# File 'lib/functio/function_repository.rb', line 61

def find(id)
  found = storage.find(id_attr => id)
  function_class.build(found) if found
end