Class: MarkdownComposer::Registry
- Inherits:
-
Object
- Object
- MarkdownComposer::Registry
- Defined in:
- lib/markdown_composer/registries/registry.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
- #[](token) ⇒ Object
- #fetch(token, default = nil) ⇒ Object
-
#initialize(entries) ⇒ Registry
constructor
A new instance of Registry.
- #key?(token) ⇒ Boolean
- #keys ⇒ Object
- #normalise(token) ⇒ Object
- #to_a ⇒ Object
- #tokens ⇒ Object
Constructor Details
#initialize(entries) ⇒ Registry
Returns a new instance of Registry.
32 33 34 35 36 37 38 |
# File 'lib/markdown_composer/registries/registry.rb', line 32 def initialize(entries) @entries = entries @by_token = entries.to_h { |entry| [ entry.token, entry ] } @aliases = entries.each_with_object({}) do |entry, map| entry.aliases.each { |alias_token| map[alias_token] = entry.token } end end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
30 31 32 |
# File 'lib/markdown_composer/registries/registry.rb', line 30 def entries @entries end |
Instance Method Details
#[](token) ⇒ Object
40 41 42 |
# File 'lib/markdown_composer/registries/registry.rb', line 40 def [](token) @by_token[normalise(token)] end |
#fetch(token, default = nil) ⇒ Object
48 49 50 |
# File 'lib/markdown_composer/registries/registry.rb', line 48 def fetch(token, default = nil) self[token] || default end |
#key?(token) ⇒ Boolean
44 45 46 |
# File 'lib/markdown_composer/registries/registry.rb', line 44 def key?(token) !self[token].nil? end |
#keys ⇒ Object
57 58 59 |
# File 'lib/markdown_composer/registries/registry.rb', line 57 def keys tokens end |
#normalise(token) ⇒ Object
52 53 54 55 |
# File 'lib/markdown_composer/registries/registry.rb', line 52 def normalise(token) value = token.to_s @by_token.key?(value) ? value : @aliases[value] end |
#to_a ⇒ Object
65 66 67 |
# File 'lib/markdown_composer/registries/registry.rb', line 65 def to_a @entries.map(&:to_h) end |
#tokens ⇒ Object
61 62 63 |
# File 'lib/markdown_composer/registries/registry.rb', line 61 def tokens @by_token.keys end |