Class: RecordDiff::MatcherOptions

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

Overview

Handles the creation of Matcher options.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  before_id: :id,
  after_id: :id,
  before_transform: :itself,
  after_transform: :itself
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MatcherOptions

Returns a new instance of MatcherOptions.



48
49
50
51
52
# File 'lib/record_diff/matcher_options.rb', line 48

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge options
  @before_id = @options[:before_id].to_proc
  @after_id = @options[:after_id].to_proc
end

Instance Attribute Details

#after_idObject (readonly)

Returns the value of attribute after_id.



46
47
48
# File 'lib/record_diff/matcher_options.rb', line 46

def after_id
  @after_id
end

#before_idObject (readonly)

Returns the value of attribute before_id.



46
47
48
# File 'lib/record_diff/matcher_options.rb', line 46

def before_id
  @before_id
end

#optionsObject (readonly)

Returns the value of attribute options.



46
47
48
# File 'lib/record_diff/matcher_options.rb', line 46

def options
  @options
end

Class Method Details

.create_proc_chain(ary) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/record_diff/matcher_options.rb', line 23

def self.create_proc_chain(ary)
  procs = ary.map { |a| create_transform_method a }
  proc do |input|
    procs.reduce(input) do |result, next_proc|
      next_proc.call(result)
    end
  end
end

.create_transform_method(arg = :itself) ⇒ Proc

Parameters:

  • arg (Proc, Symbol, Object) (defaults to: :itself)
    • symbol or proc

Returns:

  • (Proc)


15
16
17
18
19
20
21
# File 'lib/record_diff/matcher_options.rb', line 15

def self.create_transform_method(arg = :itself)
  return proc { |ary| ary[1] } if arg == :second
  return transform_from_hsh arg if arg.is_a?(Hash)
  return create_proc_chain arg if arg.is_a?(Array)

  arg.to_proc
end

.transform_from_hsh(hsh) ⇒ Object

Creates a proc based on the hash provided.

Raises:

  • (StandardError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/record_diff/matcher_options.rb', line 33

def self.transform_from_hsh(hsh)
  return proc { |hash| hash.slice(*hsh[:keys]) } if hsh.keys == [:keys]

  if hsh.keys == [:attrs]
    return proc do |obj|
      attrs = hsh[:attrs]
      Hash[attrs.collect { |k| [k, obj.send(k)] }]
    end
  end

  raise StandardError, 'Unexpected Hash Config'
end

Instance Method Details

#after_transformObject



59
60
61
62
# File 'lib/record_diff/matcher_options.rb', line 59

def after_transform
  @after_transform ||=
    self.class.create_transform_method options[:after_transform]
end

#before_transformObject



54
55
56
57
# File 'lib/record_diff/matcher_options.rb', line 54

def before_transform
  @before_transform ||=
    self.class.create_transform_method options[:before_transform]
end