Module: Bsm::Constrainable::Registry

Included in:
Field, Operation
Defined in:
lib/bsm/constrainable/registry.rb

Instance Method Summary collapse

Instance Method Details

#new(kind, *args) ⇒ Object

Create a new object of a certain kind.

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/bsm/constrainable/registry.rb', line 15

def new(kind, *args)
  raise ArgumentError, "Invalid kind #{kind}" unless registered?(kind)
  registry[kind.to_sym].new(*args)
end

#register(klass) ⇒ Object

Register a class

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/bsm/constrainable/registry.rb', line 9

def register(klass)
  raise ArgumentError, "Already registered kind: #{klass.kind}" if registered?(klass.kind)
  registry[klass.kind] = klass
end

#registered?(kind) ⇒ Boolean

Returns true if kind was already registered, else false

Returns:

  • (Boolean)


21
22
23
# File 'lib/bsm/constrainable/registry.rb', line 21

def registered?(kind)
  registry.key?(kind.to_sym)
end

#registryObject

Returns the current registry Hash



4
5
6
# File 'lib/bsm/constrainable/registry.rb', line 4

def registry
  @registry ||= {}
end