Class: Tone::Aliaser
- Inherits:
-
Object
- Object
- Tone::Aliaser
- Defined in:
- lib/tone/aliaser.rb
Overview
Allows storage of custom custom which can be referenced when colorizing text.
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
Instance Method Summary collapse
- #add(key, *styles) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(defaults: DEFAULTS) ⇒ Aliaser
constructor
A new instance of Aliaser.
- #to_h ⇒ Object
Constructor Details
Instance Attribute Details
#defaults ⇒ Object (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_h ⇒ Object
36 |
# File 'lib/tone/aliaser.rb', line 36 def to_h = custom.dup |