Class: ANSI::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi/chain.rb

Overview

ANSI::Chain was inspired by Kazuyoshi Tlacaelel’s Isna library.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codesObject (readonly)

Returns the value of attribute codes.



19
20
21
# File 'lib/ansi/chain.rb', line 19

def codes
  @codes
end

#stringObject (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_sObject



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_strObject



43
44
45
# File 'lib/ansi/chain.rb', line 43

def to_str
  to_s
end