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
12
13
# File 'lib/super_diff/csi/document.rb', line 6

def initialize(&block)
  @parts = []
  @indentation_stack = []

  if block
    evaluate_block(&block)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **opts, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/super_diff/csi/document.rb', line 90

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



19
20
21
# File 'lib/super_diff/csi/document.rb', line 19

def bold(*args, **opts, &block)
  colorize(BoldSequence.new, *args, **opts, &block)
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
42
43
# File 'lib/super_diff/csi/document.rb', line 23

def colorize(*args, **opts, &block)
  contents, colors = args.partition do |arg|
    arg.is_a?(String) || arg.is_a?(self.class)
  end

  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



15
16
17
# File 'lib/super_diff/csi/document.rb', line 15

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

#indent(by:, &block) ⇒ Object



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

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/super_diff/csi/document.rb', line 61

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



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

def newline
  add_part("\n")
end

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

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/super_diff/csi/document.rb', line 100

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



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/super_diff/csi/document.rb', line 46

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

#to_sObject



105
106
107
# File 'lib/super_diff/csi/document.rb', line 105

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