Module: SuperDiff::Core::Helpers

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

Instance Method Summary collapse

Instance Method Details

#jruby?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/super_diff/core/helpers.rb', line 41

def jruby?
  defined?(JRUBY_VERSION)
end

#object_address_for(object) ⇒ Object



52
53
54
55
# File 'lib/super_diff/core/helpers.rb', line 52

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/super_diff/core/helpers.rb', line 28

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)


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

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/super_diff/core/helpers.rb', line 9

def style(*args, color_enabled: true, **opts, &block)
  klass =
    if color_enabled && SuperDiff.configuration.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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/super_diff/core/helpers.rb', line 71

def with_slice_of_array_replaced(array, range, replacement)
  beginning =
    if range.begin.positive?
      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