Class: Deprecation

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

Overview

Represent a deprecated option. Util to store deprecated options for future reference.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, replacement, version_or_year = nil, month = nil) ⇒ Deprecation

Creates a representation of a Deprecation

Examples:

using a version as a deadline to deprecate

deprecation = Deprecation.new :to_hash, :to, "v2.0.0"
deprecation.warn
# => NOTE: option `:to_hash` is deprecated; use `:to` instead. It will be
#    removed on or after version v2.0.0.

using a date (YYYY/MM) as a deadline to deprecate

deprecation = Deprecation.new :to_hash, :to, 2015, 05
deprecation.warn
# => NOTE: option `:to_hash` is deprecated; use `:to` instead. It will be
#    removed on or after 2015-05-01.

using the caller information to compose the warn message

deprecation = Deprecation.new :to_hash, :to, "v2.0.0"
deprecation.warn
deprecation.caller = caller
# => NOTE: option `:to_hash` is deprecated; use `:to` instead. It will be
#    removed on or after version v2.0.0.
#    Called from examples/client_maroto.rb:5:in `<class:Client>'.


28
29
30
31
32
# File 'lib/deprecation.rb', line 28

def initialize(option, replacement, version_or_year = nil, month = nil)
  @option = option
  @replacement = replacement
  @date, @version = extract_date_and_version version_or_year, month
end

Instance Attribute Details

#caller=(value) ⇒ Object (writeonly)

Sets the attribute caller

Parameters:

  • value

    the value to set the attribute caller to.



5
6
7
# File 'lib/deprecation.rb', line 5

def caller=(value)
  @caller = value
end

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/deprecation.rb', line 4

def date
  @date
end

#optionObject (readonly)

Returns the value of attribute option.



4
5
6
# File 'lib/deprecation.rb', line 4

def option
  @option
end

#replacementObject (readonly)

Returns the value of attribute replacement.



4
5
6
# File 'lib/deprecation.rb', line 4

def replacement
  @replacement
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/deprecation.rb', line 4

def version
  @version
end

Instance Method Details

#warnString

Composes the deprecation message accordingly to date or version of deprecation. Also verifies (and show) the caller information.

Returns:

  • (String)


38
39
40
41
42
43
44
# File 'lib/deprecation.rb', line 38

def warn
  message = [ "NOTE: option `:#{@option}` is deprecated; use ",
              "`:#{@replacement}` instead. ",
              "It will be removed #{when_deprecation_occurs}."]
  message << "\nCalled from #{@caller}." if @caller
  message.join << "\n"
end