Class: Yattho::Deprecations

Inherits:
Object
  • Object
show all
Defined in:
lib/yattho/deprecations.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.correctable?(component_name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/yattho/deprecations.rb', line 48

def correctable?(component_name)
  dep = registered_deprecations[component_name]
  return false if dep.nil?

  dep[:autocorrect]
end

.deprecated?(component_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/yattho/deprecations.rb', line 32

def deprecated?(component_name)
  # if the component is registered, it is deprecated
  registered_deprecations.key?(component_name)
end

.deprecated_componentsObject



28
29
30
# File 'lib/yattho/deprecations.rb', line 28

def deprecated_components
  registered_deprecations.keys.sort
end

.deprecation_message(component_name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yattho/deprecations.rb', line 66

def deprecation_message(component_name)
  return nil unless deprecated?(component_name)

  msg = ["'#{component_name}' has been deprecated."]

  # this nested structure is complex, because it has to
  # match all of the valid scenarios for a component being
  # replaceable, auto-correctable, and having a guide. for
  # more information on what is and is not valid, see the
  # documentation here: docs/contributors/deprecations.md

  if replacement?(component_name)
    msg << "Please update your code to use '#{replacement(component_name)}'."
    msg << "Use Rubocop's auto-correct, or replace it yourself." if correctable?(component_name)
    msg << "See #{guide(component_name)} for more information." if guide?(component_name)
  else # if there is no replacement, it must have a guide. this is enforced through tests
    msg << "Unfortunately, there is no direct replacement."
    msg << "See #{guide(component_name)} for more information and available options."
  end

  msg.join(" ")
end

.guide(component_name) ⇒ Object



59
60
61
62
63
64
# File 'lib/yattho/deprecations.rb', line 59

def guide(component_name)
  dep = registered_deprecations[component_name]
  return nil if dep.nil?

  dep[:guide]
end

.guide?(component_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/yattho/deprecations.rb', line 55

def guide?(component_name)
  !guide(component_name).nil?
end

.register(file_path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/yattho/deprecations.rb', line 9

def register(file_path)
  data = YAML.load_file(file_path)
  data["deprecations"].each do |dep|
    register_deprecation(dep["component"], {
                           autocorrect: dep["autocorrect"],
                           guide: dep["guide"],
                           replacement: dep["replacement"]
                         })
  end
end

.register_deprecation(component, options) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/yattho/deprecations.rb', line 20

def register_deprecation(component, options)
  registered_deprecations[component] = {
    autocorrect: options[:autocorrect],
    guide: options[:guide],
    replacement: options[:replacement]
  }
end

.replacement(component_name) ⇒ Object



41
42
43
44
45
46
# File 'lib/yattho/deprecations.rb', line 41

def replacement(component_name)
  dep = registered_deprecations[component_name]
  return nil if dep.nil?

  dep[:replacement]
end

.replacement?(component_name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/yattho/deprecations.rb', line 37

def replacement?(component_name)
  !replacement(component_name).nil?
end