Class: SuperDiff::ObjectInspection::InspectionTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/super_diff/object_inspection/inspection_tree.rb

Defined Under Namespace

Classes: BlockArgument

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ InspectionTree

Returns a new instance of InspectionTree.



6
7
8
9
10
11
12
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 6

def initialize(&block)
  @nodes = []

  if block
    instance_eval(&block)
  end
end

Instance Method Details

#add_break(*args, &block) ⇒ Object



48
49
50
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 48

def add_break(*args, &block)
  add_node :break, *args, &block
end

#add_inspection_of(value = nil, &block) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 110

def add_inspection_of(value = nil, &block)
  if block
    add_node :inspection, &block
  else
    add_node :inspection, value
  end
end

#add_text(*args, &block) ⇒ Object



36
37
38
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 36

def add_text(*args, &block)
  add_node :text, *args, &block
end

#apply_tree(tree) ⇒ Object



118
119
120
121
122
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 118

def apply_tree(tree)
  tree.each do |node|
    append_node(node.clone_with(tree: self))
  end
end

#before_each_callbacksObject



18
19
20
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 18

def before_each_callbacks
  @_before_each_callbacks ||= Hash.new { |h, k| h[k] = [] }
end

#each(&block) ⇒ Object



14
15
16
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 14

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

#evaluate(object, as_single_line:, indent_level:) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 22

def evaluate(object, as_single_line:, indent_level:)
  nodes.reduce("") do |str, node|
    str << node.evaluate(
      object,
      as_single_line: as_single_line,
      indent_level: indent_level,
    )
  end
end

#evaluate_block(object, &block) ⇒ Object



32
33
34
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 32

def evaluate_block(object, &block)
  instance_exec(object, &block)
end

#insert_array_inspection_of(array) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 64

def insert_array_inspection_of(array)
  # FIXME: why must this be inside the `nested`?
  add_break

  insert_separated_list(array) do |value|
    add_inspection_of value
  end
end

#insert_hash_inspection_of(hash, initial_break: " ") ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 73

def insert_hash_inspection_of(hash, initial_break: " ")
  # FIXME: why must this be inside the `nested`?
  add_break initial_break

  format_keys_as_kwargs = hash.keys.all? do |key|
    key.is_a?(Symbol)
  end

  insert_separated_list(hash) do |(key, value)|
    if format_keys_as_kwargs
      add_text key
      add_text ": "
    else
      add_inspection_of key
      add_text " => "
    end

    add_inspection_of value
  end
end

#insert_separated_list(enumerable, separator: ",") ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 94

def insert_separated_list(enumerable, separator: ",")
  enumerable.each_with_index do |value, index|
    if index > 0
      if separator.is_a?(Nodes::Base)
        append_node separator
      else
        add_text separator
      end

      add_break " "
    end

    yield value
  end
end

#nested(&block) ⇒ Object



52
53
54
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 52

def nested(&block)
  add_node :nesting, &block
end

#when_empty(&block) ⇒ Object



56
57
58
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 56

def when_empty(&block)
  add_node :when_empty, &block
end

#when_multiline(&block) ⇒ Object



40
41
42
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 40

def when_multiline(&block)
  add_node :when_multiline, &block
end

#when_non_empty(&block) ⇒ Object



60
61
62
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 60

def when_non_empty(&block)
  add_node :when_non_empty, &block
end

#when_singleline(&block) ⇒ Object



44
45
46
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 44

def when_singleline(&block)
  add_node :when_singleline, &block
end