Class: MarkdownComposer::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_composer/registries/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#entriesObject (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

Returns:

  • (Boolean)


44
45
46
# File 'lib/markdown_composer/registries/registry.rb', line 44

def key?(token)
  !self[token].nil?
end

#keysObject



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_aObject



65
66
67
# File 'lib/markdown_composer/registries/registry.rb', line 65

def to_a
  @entries.map(&:to_h)
end

#tokensObject



61
62
63
# File 'lib/markdown_composer/registries/registry.rb', line 61

def tokens
  @by_token.keys
end