Class: Moose::Inventory::InventoryContext

Inherits:
Object
  • Object
show all
Defined in:
lib/moose_inventory/inventory_context.rb

Overview

Thin facade over the current DB singleton.

This gives new operation/service objects a small inventory-facing seam without forcing the legacy CLI to stop using the DB singleton all at once.

Constant Summary collapse

AUTOMATIC_GROUP =
'ungrouped'

Instance Method Summary collapse

Constructor Details

#initialize(db: Moose::Inventory::DB) ⇒ InventoryContext

Returns a new instance of InventoryContext.



13
14
15
# File 'lib/moose_inventory/inventory_context.rb', line 13

def initialize(db: Moose::Inventory::DB)
  @db = db
end

Instance Method Details

#automatic_groupObject



41
42
43
# File 'lib/moose_inventory/inventory_context.rb', line 41

def automatic_group
  find_or_create_group(AUTOMATIC_GROUP)
end

#create_group(name) ⇒ Object



33
34
35
# File 'lib/moose_inventory/inventory_context.rb', line 33

def create_group(name)
  db.models[:group].create(name: name)
end

#create_host(name) ⇒ Object



25
26
27
# File 'lib/moose_inventory/inventory_context.rb', line 25

def create_host(name)
  db.models[:host].create(name: name)
end

#find_group(name) ⇒ Object



29
30
31
# File 'lib/moose_inventory/inventory_context.rb', line 29

def find_group(name)
  db.models[:group].find(name: name)
end

#find_host(name) ⇒ Object



21
22
23
# File 'lib/moose_inventory/inventory_context.rb', line 21

def find_host(name)
  db.models[:host].find(name: name)
end

#find_or_create_group(name) ⇒ Object



37
38
39
# File 'lib/moose_inventory/inventory_context.rb', line 37

def find_or_create_group(name)
  db.models[:group].find_or_create(name: name)
end

#transactionObject



17
18
19
# File 'lib/moose_inventory/inventory_context.rb', line 17

def transaction(&)
  db.transaction(&)
end