Class: Swarm::Hive

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

Defined Under Namespace

Classes: IllegalDefaultError, MissingTypeError, 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.



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

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

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



23
24
25
# File 'lib/swarm/hive.rb', line 23

def storage
  @storage
end

#work_queueObject (readonly)

Returns the value of attribute work_queue.



23
24
25
# File 'lib/swarm/hive.rb', line 23

def work_queue
  @work_queue
end

Class Method Details

.defaultObject



15
16
17
18
19
20
# File 'lib/swarm/hive.rb', line 15

def default
  unless @default
    raise NoDefaultSetError.new("No default Hive defined yet")
  end
  @default
end

.default=(default) ⇒ Object



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

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

Instance Method Details

#fetch(klass, id) ⇒ Object



57
58
59
# File 'lib/swarm/hive.rb', line 57

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

#inspectObject



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

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

#queue(action, object) ⇒ Object



50
51
52
53
54
55
# File 'lib/swarm/hive.rb', line 50

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

#registered_observersObject



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

def registered_observers
  @registered_observers ||= []
end

#reify_from_hash(hsh) ⇒ Object

Raises:



61
62
63
64
65
66
67
68
69
# File 'lib/swarm/hive.rb', line 61

def reify_from_hash(hsh)
  Support.symbolize_keys!(hsh)
  raise MissingTypeError.new(hsh.inspect) unless hsh[:type]
  Swarm::Support.constantize(hsh.delete(:type)).new_from_storage(
    hsh.merge(
      :hive => self
    )
  )
end

#storage_classObject



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

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

#trace(new_element) ⇒ Object



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

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

#tracedObject



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

def traced
  storage.trace ||= []
end