Module: Specdiff

Defined in:
lib/specdiff.rb,
lib/specdiff/config.rb,
lib/specdiff/plugins.rb,
lib/specdiff/version.rb

Defined Under Namespace

Modules: Colorize, Differ, Plugin, Plugins Classes: Compare, Config, Hashprint, Inspect, RSpecIntegration, WebmockIntegration

Constant Summary collapse

DEFAULT =
Config.new(colorize: true).freeze
BUILTIN_PLUGINS =
%i[json]
BUILTIN_TYPES =
%i[hash array binary text nil]
PLUGIN_INTERFACE =
[:id, :detect_type, :compatible?, :diff, :stringify].freeze
VERSION =
"0.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/specdiff/config.rb', line 9

def config
  @config
end

.pluginsObject (readonly)

Returns the value of attribute plugins.



3
4
5
# File 'lib/specdiff/plugins.rb', line 3

def plugins
  @plugins
end

Class Method Details

._clear_plugins!Object

private



65
66
67
# File 'lib/specdiff/plugins.rb', line 65

def self._clear_plugins!
  @plugins = []
end

._set_config(new_config) ⇒ Object

private, used for testing



16
17
18
# File 'lib/specdiff/config.rb', line 16

def self._set_config(new_config)
  @config = new_config
end

.configure {|@config| ... } ⇒ Object

Set the configuration

Yields:



21
22
23
24
# File 'lib/specdiff/config.rb', line 21

def self.configure
  yield(@config)
  @config
end

.default_configurationObject

Generates the default configuration



27
28
29
# File 'lib/specdiff/config.rb', line 27

def self.default_configuration
  DEFAULT
end

.diffObject



9
10
11
# File 'lib/specdiff.rb', line 9

def self.diff(...)
  ::Specdiff::Compare.call(...)
end

.diff_inspectObject



17
18
19
# File 'lib/specdiff.rb', line 17

def self.diff_inspect(...)
  ::Specdiff::Inspect.call(...)
end

.hashprintObject



13
14
15
# File 'lib/specdiff.rb', line 13

def self.hashprint(...)
  ::Specdiff::Hashprint.call(...)
end

.load!(*plugins) ⇒ Object

Load extra type support, such as support for json strings



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/specdiff/plugins.rb', line 11

def self.load!(*plugins)
  return if plugins.size == 0

  plugins.each do |new_plugin|
    if BUILTIN_PLUGINS.include?(new_plugin)
      case new_plugin
      when :json
        require "#{__dir__}/plugins/json"
        load_plugin_class!(::Specdiff::Plugins::Json)
      end
    else
      load_plugin_class!(new_plugin)
    end
  end

  nil
end

.load_plugin_class!(plugin) ⇒ Object

Load a single plugin class, does not support symbols for builtin plugins.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/specdiff/plugins.rb', line 32

def self.load_plugin_class!(plugin)
  plugin_interface = PLUGIN_INTERFACE

  if plugin.respond_to?(:id)
    if BUILTIN_TYPES.include?(plugin.id)
      plugin_interface = PLUGIN_INTERFACE - [:detect_type]
    end

    if plugin.id == :unknown
      raise <<~MSG
        plugin #{plugin.inspect} defined #id to = :unknown, but this is not \
        allowed because it would undermine the utility of the #empty? method \
        on the diff.
      MSG
    end
  end

  missing = plugin_interface.filter do |method_name|
    !plugin.respond_to?(method_name)
  end

  if missing.any?
    raise <<~MSG
      plugin #{plugin.inspect} does not respond to required methods:
      these are required: #{plugin_interface}
      these were missing: #{missing.inspect}
    MSG
  end

  @plugins << plugin
end