Class: ANSI::Chain
- Inherits:
-
Object
- Object
- ANSI::Chain
- Defined in:
- lib/ansi/chain.rb
Overview
ANSI::Chain was inspired by Kazuyoshi Tlacaelel’s Isna library.
Instance Attribute Summary collapse
-
#codes ⇒ Object
readonly
Returns the value of attribute codes.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
-
#initialize(string) ⇒ Chain
constructor
A new instance of Chain.
- #method_missing(s, *a, &b) ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(string) ⇒ Chain
Returns a new instance of Chain.
10 11 12 13 |
# File 'lib/ansi/chain.rb', line 10 def initialize(string) @string = string.to_s @codes = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(s, *a, &b) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/ansi/chain.rb', line 22 def method_missing(s, *a, &b) if ANSI::CHART.key?(s) @codes << s self else super(s, *a, &b) end end |
Instance Attribute Details
#codes ⇒ Object (readonly)
Returns the value of attribute codes.
19 20 21 |
# File 'lib/ansi/chain.rb', line 19 def codes @codes end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
16 17 18 |
# File 'lib/ansi/chain.rb', line 16 def string @string end |
Instance Method Details
#to_s ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/ansi/chain.rb', line 32 def to_s if codes.empty? result = @string else result = Code.ansi(@string, *codes) codes.clear end result end |
#to_str ⇒ Object
43 44 45 |
# File 'lib/ansi/chain.rb', line 43 def to_str to_s end |