Class: SuperDiff::Csi::Document

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/super_diff/csi/document.rb

Direct Known Subclasses

ColorizedDocument, UncolorizedDocument

Defined Under Namespace

Classes: ColorRequest, MethodRequest, Request

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Document

Returns a new instance of Document.



6
7
8
9
10
11
# File 'lib/super_diff/csi/document.rb', line 6

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



83
84
85
86
87
88
89
90
91
# File 'lib/super_diff/csi/document.rb', line 83

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(*args, **opts, &block) ⇒ Object



17
18
19
# File 'lib/super_diff/csi/document.rb', line 17

def bold(*args, **opts, &block)
  colorize(BoldSequence.new, *args, **opts, &block)
end

#colorize(*args, **opts, &block) ⇒ Object Also known as: colored



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/super_diff/csi/document.rb', line 21

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)
    if colors[0] == :colorize
      raise ArgumentError, "#colorize can't call itself!"
    else
      public_send(colors[0], *contents, *colors[1..-1], **opts, &block)
    end
  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



13
14
15
# File 'lib/super_diff/csi/document.rb', line 13

def each(&block)
  parts.each(&block)
end

#indent(by:, &block) ⇒ Object



76
77
78
79
80
81
# File 'lib/super_diff/csi/document.rb', line 76

def indent(by:, &block)
  # TODO: This won't work if using `text` manually to add lines
  indentation_stack << (by.is_a?(String) ? by : " " * by)
  evaluate_block(&block)
  indentation_stack.pop
end

#line(*contents, indent_by: 0, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/super_diff/csi/document.rb', line 54

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.new(
              "Must have something to add to the document!"
            )
    end
  end

  add_part("\n")
end

#newlineObject



72
73
74
# File 'lib/super_diff/csi/document.rb', line 72

def newline
  add_part("\n")
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/super_diff/csi/document.rb', line 93

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



43
44
45
46
47
48
49
50
51
# File 'lib/super_diff/csi/document.rb', line 43

def text(*contents, **, &block)
  if block
    evaluate_block(&block)
  elsif contents.any?
    contents.each { |part| add_part(part) }
  else
    raise ArgumentError.new("Must have something to add to the document!")
  end
end

#to_sObject



98
99
100
# File 'lib/super_diff/csi/document.rb', line 98

def to_s
  parts.map(&:to_s).join.rstrip
end