Module: SuperDiff::Helpers

Extended by:
Helpers
Included in:
Helpers, TieredLinesElider
Defined in:
lib/super_diff/helpers.rb

Instance Method Summary collapse

Instance Method Details

#jruby?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/super_diff/helpers.rb', line 34

def jruby?
  defined?(JRUBY_VERSION)
end

#object_address_for(object) ⇒ Object



45
46
47
48
# File 'lib/super_diff/helpers.rb', line 45

def object_address_for(object)
  # Source: <https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyBasicObject.java>
  "0x%x" % object.hash
end

#plural_type_for(value) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/super_diff/helpers.rb', line 25

def plural_type_for(value)
  case value
  when Numeric then "numbers"
  when String then "strings"
  when Symbol then "symbols"
  else "objects"
  end
end

#ruby_version_matches?(version_string) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/super_diff/helpers.rb', line 38

def ruby_version_matches?(version_string)
  Gem::Requirement.new(version_string).satisfied_by?(
    Gem::Version.new(RUBY_VERSION),
  )
end

#style(*args, color_enabled: true, **opts, &block) ⇒ Object

TODO: Simplify this



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/super_diff/helpers.rb', line 6

def style(*args, color_enabled: true, **opts, &block)
  klass =
    if color_enabled && Csi.color_enabled?
      Csi::ColorizedDocument
    else
      Csi::UncolorizedDocument
    end

  document = klass.new.extend(ColorizedDocumentExtensions)

  if block
    document.__send__(:evaluate_block, &block)
  else
    document.colorize(*args, **opts)
  end

  document
end

#with_slice_of_array_replaced(array, range, replacement) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/super_diff/helpers.rb', line 64

def with_slice_of_array_replaced(array, range, replacement)
  beginning =
    if range.begin > 0
      array[Range.new(0, range.begin - 1)]
    else
      []
    end

  ending =
    if range.end <= array.length - 1
      array[Range.new(range.end + 1, array.length - 1)]
    else
      []
    end

  beginning + [replacement] + ending
end