Module: FlowOrganizer::Callable::Alias

Defined in:
lib/flow_organizer/callable/alias.rb

Overview

Allows registration of a callable in a local store with an alias name. It can be used later by using the alias.

## Registering the alias

“‘ruby callable = ->(counter:) { [:ok, counter: counter + 1]} FlowOrganizer::Callable::Alias.register(id: :increment_counter, target: callable) “`

## Using the alias

“‘ruby FlowOrganizer::FlowOrganizer.call(

list: [
  [:alias, :increment_counter],
],
ctx: { counter: 1 },

) “‘

Class Method Summary collapse

Class Method Details

.create_storeObject

Create a store to save organizer’s aliases



55
56
57
# File 'lib/flow_organizer/callable/alias.rb', line 55

def self.create_store
  {}
end

.get(id:, store: nil) ⇒ Object

Get the alias identified with ‘:id`



43
44
45
46
47
48
49
50
51
52
# File 'lib/flow_organizer/callable/alias.rb', line 43

def self.get(id:, store: nil)
  store ||= local_store
  record = store[id.to_sym]

  if !record
    raise "FlowOrganizer::Callable::Alias | unknown alias `#{ id }`"
  end

  record[:target]
end

.local_storeObject

Default store when not specified as a parameter



60
61
62
# File 'lib/flow_organizer/callable/alias.rb', line 60

def self.local_store
  @local_store ||= create_store
end

.register(id:, target:, store: nil) ⇒ Object

Saves a ‘:target` in the alias store, identified by `:id`



34
35
36
37
38
39
40
# File 'lib/flow_organizer/callable/alias.rb', line 34

def self.register(id:, target:, store: nil)
  store ||= local_store

  store[id.to_sym] = {
    target:,
  }
end

.resolve(args:, store: nil) ⇒ Object

Note:

The expected format is ‘[:alias, alias_name]`.

Resolves ‘alias_name` to the registered `:callable`



26
27
28
29
30
31
# File 'lib/flow_organizer/callable/alias.rb', line 26

def self.resolve(args:, store: nil)
  _, alias_name = args
  store ||= local_store

  [:ok, callable: get(id: alias_name, store:)]
end