Class: Aquarium::Finders::FinderResult

Inherits:
Object
  • Object
show all
Includes:
Utils::HashUtils, Utils::SetUtils
Defined in:
lib/aquarium/finders/finder_result.rb

Overview

FinderResult

Wraps hashes that hold the results of various *Finder utilities. The #not_matched method returns specified inputs that did not result in a successful find.

Constant Summary collapse

NIL_OBJECT =
FinderResult.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::SetUtils

#make_set, #strip_set_nils, strip_set_nils

Methods included from Utils::HashUtils

#make_hash, #strip_nil_keys

Methods included from Utils::ArrayUtils

#make_array, make_array, #strip_array_nils, strip_array_nils

Constructor Details

#initialize(hash = {}) ⇒ FinderResult

Returns a new instance of FinderResult.



31
32
33
34
# File 'lib/aquarium/finders/finder_result.rb', line 31

def initialize hash = {}
  @matched = convert_hash_values_to_sets(hash.reject {|key, value| key.eql?(:not_matched)})
  @not_matched = convert_hash_values_to_sets(make_hash(hash[:not_matched]) {|x| Set.new} || {})
end

Instance Attribute Details

#matchedObject

Returns the value of attribute matched.



13
14
15
# File 'lib/aquarium/finders/finder_result.rb', line 13

def matched
  @matched
end

#not_matchedObject

Returns the value of attribute not_matched.



13
14
15
# File 'lib/aquarium/finders/finder_result.rb', line 13

def not_matched
  @not_matched
end

Instance Method Details

#<<(other_result) ⇒ Object



48
49
50
51
52
# File 'lib/aquarium/finders/finder_result.rb', line 48

def << other_result
  append_matched     other_result.matched
  append_not_matched other_result.not_matched
  self
end

#and(other_result) ⇒ Object Also known as: intersection, &

“And” two results together



70
71
72
73
74
75
# File 'lib/aquarium/finders/finder_result.rb', line 70

def and other_result
  result = dup
  result.matched     = hash_intersection(matched,     other_result.matched)
  result.not_matched = hash_intersection(not_matched, other_result.not_matched)
  result
end

#append_matched(other_hash = {}) ⇒ Object



89
90
91
# File 'lib/aquarium/finders/finder_result.rb', line 89

def append_matched other_hash = {}
  @matched = convert_hash_values_to_sets hash_union(matched, other_hash)
end

#append_not_matched(other_hash = {}) ⇒ Object



93
94
95
96
# File 'lib/aquarium/finders/finder_result.rb', line 93

def append_not_matched other_hash = {}
  @not_matched = convert_hash_values_to_sets hash_union(not_matched, other_hash)
  @not_matched.each_key {|key| purge_matched key}
end

#empty?Boolean

Were there no matches?

Returns:

  • (Boolean)


112
113
114
# File 'lib/aquarium/finders/finder_result.rb', line 112

def empty?
  matched.empty?
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/aquarium/finders/finder_result.rb', line 98

def eql? other
  object_id == other.object_id ||
  (matched == other.matched and not_matched == other.not_matched)
end

#inspectObject Also known as: to_s



105
106
107
# File 'lib/aquarium/finders/finder_result.rb', line 105

def inspect 
  "FinderResult: {matched: #{matched.inspect}, not_matched: #{not_matched.inspect}}"
end

#matched_keysObject

Convenience method to get the keys for the matches.



39
40
41
# File 'lib/aquarium/finders/finder_result.rb', line 39

def matched_keys
  @matched.keys
end

#minus(other_result) ⇒ Object Also known as: -



80
81
82
83
84
85
# File 'lib/aquarium/finders/finder_result.rb', line 80

def minus other_result
  result = dup
  result.matched     = matched     - other_result.matched
  result.not_matched = not_matched - other_result.not_matched
  result
end

#not_matched_keysObject

Convenience method to get the keys for the items that did not result in matches.



44
45
46
# File 'lib/aquarium/finders/finder_result.rb', line 44

def not_matched_keys
  @not_matched.keys
end

#or(other_result) ⇒ Object Also known as: union, |

“Or” two results together – We use dup here and in other methods, rather than FinderResult.new, so that new subclass objects are returned correctly! ++



59
60
61
62
63
64
# File 'lib/aquarium/finders/finder_result.rb', line 59

def or other_result
  result = dup
  result.matched     = hash_union(matched,     other_result.matched)
  result.not_matched = hash_union(not_matched, other_result.not_matched)
  result
end