Class: Pressroom::Blocks::Registry
- Inherits:
-
Object
- Object
- Pressroom::Blocks::Registry
- Includes:
- Enumerable
- Defined in:
- lib/pressroom/blocks/registry.rb
Overview
Ordered collection of block definitions.
Registration order matters: it is the order tools appear in the editor's toolbar.
Instance Method Summary collapse
- #clear! ⇒ Object
- #delete(type) ⇒ Object
- #each(&block) ⇒ Object
- #fetch(type) ⇒ Object (also: #[])
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #key?(type) ⇒ Boolean
- #register(type, &block) ⇒ Object
- #types ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
12 13 14 |
# File 'lib/pressroom/blocks/registry.rb', line 12 def initialize @definitions = {} end |
Instance Method Details
#clear! ⇒ Object
51 52 53 |
# File 'lib/pressroom/blocks/registry.rb', line 51 def clear! @definitions = {} end |
#delete(type) ⇒ Object
47 48 49 |
# File 'lib/pressroom/blocks/registry.rb', line 47 def delete(type) @definitions.delete(type.to_s) end |
#each(&block) ⇒ Object
43 44 45 |
# File 'lib/pressroom/blocks/registry.rb', line 43 def each(&block) @definitions.each_value(&block) end |
#fetch(type) ⇒ Object Also known as: []
30 31 32 |
# File 'lib/pressroom/blocks/registry.rb', line 30 def fetch(type) @definitions[type.to_s] end |
#key?(type) ⇒ Boolean
35 36 37 |
# File 'lib/pressroom/blocks/registry.rb', line 35 def key?(type) @definitions.key?(type.to_s) end |
#register(type, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pressroom/blocks/registry.rb', line 16 def register(type, &block) key = type.to_s if @definitions.key?(key) raise Pressroom::Error, "block #{key.inspect} is already registered" end definition = Definition.new(key) block&.call(definition) definition.validate! @definitions[key] = definition end |
#types ⇒ Object
39 40 41 |
# File 'lib/pressroom/blocks/registry.rb', line 39 def types @definitions.keys end |