Class: SuperDiff::Csi::Document
- Inherits:
-
Object
- Object
- SuperDiff::Csi::Document
show all
- Includes:
- Enumerable
- Defined in:
- lib/super_diff/csi/document.rb
Defined Under Namespace
Classes: ColorRequest, MethodRequest, Request
Instance Method Summary
collapse
-
#bold ⇒ Object
-
#colorize(*args, **opts, &block) ⇒ Object
(also: #colored)
-
#each(&block) ⇒ Object
-
#indent(by:, &block) ⇒ Object
-
#initialize(&block) ⇒ Document
constructor
A new instance of Document.
-
#line(*contents, indent_by: 0, &block) ⇒ Object
-
#method_missing(name, *args, **opts, &block) ⇒ Object
-
#newline ⇒ Object
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#text(*contents, &block) ⇒ Object
(also: #plain)
-
#to_s ⇒ Object
Constructor Details
#initialize(&block) ⇒ Document
Returns a new instance of Document.
8
9
10
11
12
13
|
# File 'lib/super_diff/csi/document.rb', line 8
def initialize(&block)
@parts = []
@indentation_stack = []
evaluate_block(&block) if block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **opts, &block) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/super_diff/csi/document.rb', line 82
def method_missing(name, *args, **opts, &block)
request = derive_request_from(name)
if request
request.resolve(self, args, opts, &block)
else
super
end
end
|
Instance Method Details
#bold ⇒ Object
19
20
21
|
# File 'lib/super_diff/csi/document.rb', line 19
def bold(...)
colorize(BoldSequence.new, ...)
end
|
#colorize(*args, **opts, &block) ⇒ Object
Also known as:
colored
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/super_diff/csi/document.rb', line 23
def colorize(*args, **opts, &block)
contents, colors =
args.partition { |arg| arg.is_a?(String) || arg.is_a?(self.class) }
if colors[0].is_a?(Symbol)
raise ArgumentError, "#colorize can't call itself!" if colors[0] == :colorize
public_send(colors[0], *contents, *colors[1..], **opts, &block)
elsif !block && colors.empty? && opts.empty?
text(*contents)
elsif block
colorize_block(colors, opts, &block)
elsif contents.any?
colorize_inline(contents, colors, opts)
else
raise ArgumentError, 'Must have something to colorize!'
end
end
|
#each(&block) ⇒ Object
15
16
17
|
# File 'lib/super_diff/csi/document.rb', line 15
def each(&block)
parts.each(&block)
end
|
#indent(by:, &block) ⇒ Object
75
76
77
78
79
80
|
# File 'lib/super_diff/csi/document.rb', line 75
def indent(by:, &block)
indentation_stack << (by.is_a?(String) ? by : ' ' * by)
evaluate_block(&block)
indentation_stack.pop
end
|
#line(*contents, indent_by: 0, &block) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/super_diff/csi/document.rb', line 55
def line(*contents, indent_by: 0, &block)
indent(by: indent_by) do
add_part(indentation_stack.join)
if block
evaluate_block(&block)
elsif contents.any?
text(*contents)
else
raise ArgumentError, 'Must have something to add to the document!'
end
end
add_part("\n")
end
|
#newline ⇒ Object
71
72
73
|
# File 'lib/super_diff/csi/document.rb', line 71
def newline
add_part("\n")
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
92
93
94
95
|
# File 'lib/super_diff/csi/document.rb', line 92
def respond_to_missing?(name, include_private = false)
request = derive_request_from(name)
!request.nil? || super
end
|
#text(*contents, &block) ⇒ Object
Also known as:
plain
44
45
46
47
48
49
50
51
52
|
# File 'lib/super_diff/csi/document.rb', line 44
def text(*contents, **, &block)
if block
evaluate_block(&block)
elsif contents.any?
contents.each { |part| add_part(part) }
else
raise ArgumentError, 'Must have something to add to the document!'
end
end
|
#to_s ⇒ Object
97
98
99
|
# File 'lib/super_diff/csi/document.rb', line 97
def to_s
parts.map(&:to_s).join.rstrip
end
|