Class: Tone::Aliaser

Inherits:
Object
  • Object
show all
Defined in:
lib/tone/aliaser.rb

Overview

Allows storage of custom custom which can be referenced when colorizing text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults: DEFAULTS) ⇒ Aliaser

Returns a new instance of Aliaser.



12
13
14
15
# File 'lib/tone/aliaser.rb', line 12

def initialize defaults: DEFAULTS
  @defaults = defaults
  @custom = {}
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



10
11
12
# File 'lib/tone/aliaser.rb', line 10

def defaults
  @defaults
end

Instance Method Details

#add(key, *styles) ⇒ Object



29
30
31
32
33
34
# File 'lib/tone/aliaser.rb', line 29

def add(key, *styles)
  fail Error, "Alias must have styles: #{key.inspect}." if styles.tap(&:compact!).empty?

  custom[key.to_sym] = validate key, styles.map(&:to_sym)
  self
end

#get(key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tone/aliaser.rb', line 17

def get key
  symbol = String(key).to_sym

  custom.fetch symbol do
    return key if defaults.key? symbol

    usage = defaults.keys.append(*custom.keys).to_usage "and/or"

    fail Error, "Invalid alias or default: #{key.inspect}. Use: #{usage}."
  end
end

#to_hObject



36
# File 'lib/tone/aliaser.rb', line 36

def to_h = custom.dup