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)


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

def jruby?
  defined?(JRUBY_VERSION)
end

#object_address_for(object) ⇒ Object



49
50
51
52
# File 'lib/super_diff/helpers.rb', line 49

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
33
34
35
36
# File 'lib/super_diff/helpers.rb', line 25

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

#ruby_version_matches?(version_string) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/super_diff/helpers.rb', line 42

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/super_diff/helpers.rb', line 68

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