Class: RecordDiff::MatcherOptions
- Inherits:
-
Object
- Object
- RecordDiff::MatcherOptions
- 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
-
#after_id ⇒ Object
readonly
Returns the value of attribute after_id.
-
#before_id ⇒ Object
readonly
Returns the value of attribute before_id.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .create_proc_chain(ary) ⇒ Object
- .create_transform_method(arg = :itself) ⇒ Proc
-
.transform_from_hsh(hsh) ⇒ Object
Creates a proc based on the hash provided.
Instance Method Summary collapse
- #after_transform ⇒ Object
- #before_transform ⇒ Object
-
#initialize(options = {}) ⇒ MatcherOptions
constructor
A new instance of MatcherOptions.
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 = DEFAULT_OPTIONS.merge @before_id = @options[:before_id].to_proc @after_id = @options[:after_id].to_proc end |
Instance Attribute Details
#after_id ⇒ Object (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_id ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
46 47 48 |
# File 'lib/record_diff/matcher_options.rb', line 46 def @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
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.
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_transform ⇒ Object
59 60 61 62 |
# File 'lib/record_diff/matcher_options.rb', line 59 def after_transform @after_transform ||= self.class.create_transform_method [:after_transform] end |
#before_transform ⇒ Object
54 55 56 57 |
# File 'lib/record_diff/matcher_options.rb', line 54 def before_transform @before_transform ||= self.class.create_transform_method [:before_transform] end |