Class: Swarm::Hive

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm/hive.rb

Defined Under Namespace

Classes: IllegalDefaultError, NoDefaultSetError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage:, work_queue:) ⇒ Hive

Returns a new instance of Hive.



28
29
30
31
# File 'lib/swarm/hive.rb', line 28

def initialize(storage:, work_queue:)
  @storage = storage
  @work_queue = work_queue
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



26
27
28
# File 'lib/swarm/hive.rb', line 26

def storage
  @storage
end

#work_queueObject (readonly)

Returns the value of attribute work_queue.



26
27
28
# File 'lib/swarm/hive.rb', line 26

def work_queue
  @work_queue
end

Class Method Details

.defaultObject



17
18
19
20
21
22
23
# File 'lib/swarm/hive.rb', line 17

def default
  unless @default
    raise NoDefaultSetError, "No default Hive defined yet"
  end

  @default
end

.default=(default) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/swarm/hive.rb', line 9

def default=(default)
  unless default.is_a?(self)
    raise IllegalDefaultError, "Default must be a Swarm::Hive"
  end

  @default = default
end

Instance Method Details

#fetch(klass, id) ⇒ Object



60
61
62
# File 'lib/swarm/hive.rb', line 60

def fetch(klass, id)
  Swarm::Support.constantize(klass).fetch(id, hive: self)
end

#inspectObject



37
38
39
# File 'lib/swarm/hive.rb', line 37

def inspect
  "#<Swarm::Hive storage: #{storage_class}, work_queue: #{work_queue.name}>"
end

#queue(action, object) ⇒ Object



53
54
55
56
57
58
# File 'lib/swarm/hive.rb', line 53

def queue(action, object)
  @work_queue.add_job({
    action: action,
    metadata: object.to_hash
  })
end

#registered_observersObject



33
34
35
# File 'lib/swarm/hive.rb', line 33

def registered_observers
  @registered_observers ||= []
end

#storage_classObject



41
42
43
# File 'lib/swarm/hive.rb', line 41

def storage_class
  storage.class.name.split('::').last
end

#trace(new_element) ⇒ Object



49
50
51
# File 'lib/swarm/hive.rb', line 49

def trace(new_element)
  storage.trace = traced + [new_element]
end

#tracedObject



45
46
47
# File 'lib/swarm/hive.rb', line 45

def traced
  storage.trace ||= []
end