Class: ScrubDb::Filter

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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Filter



6
7
8
9
# File 'lib/scrub_db/filter.rb', line 6

def initialize(args={})
  @args = args
  @empty_criteria = args.empty?
end

Instance Method Details

#fetch_criteria(oa_name) ⇒ Object



33
34
35
36
# File 'lib/scrub_db/filter.rb', line 33

def fetch_criteria(oa_name)
  criteria = @args.fetch(oa_name.to_sym, [])
  criteria = criteria&.map(&:downcase)
end

#match_criteria(tars, include_or_equal, criteria) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/scrub_db/filter.rb', line 39

def match_criteria(tars, include_or_equal, criteria)
  scrub_matches = tars.map do |tar|
    if include_or_equal == 'include'
      criteria.map { |crit| crit if tar.include?(crit) }
    elsif include_or_equal == 'equal'
      criteria.map { |crit| crit if tar == crit }
    end
  end
  scrub_matches = scrub_matches.flatten.compact
end

#match_to_hash(hsh, match, oa_name) ⇒ Object



23
24
25
26
27
# File 'lib/scrub_db/filter.rb', line 23

def match_to_hash(hsh, match, oa_name)
  return hsh unless match.present?
  hsh[oa_name.to_sym] << match
  hsh
end

#prep_target(target) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/scrub_db/filter.rb', line 50

def prep_target(target)
  target = target.join if target.is_a?(Array)
  target = target.downcase
  target = target.gsub(',', ' ')
  target = target.gsub('-', ' ')
  target = target.squeeze(' ')
end

#scrub_oa(hash, target, oa_name, include_or_equal) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/scrub_db/filter.rb', line 11

def scrub_oa(hash, target, oa_name, include_or_equal)
  return hash unless oa_name.present? && !@empty_criteria && target.present?
  criteria = fetch_criteria(oa_name)

  return hash unless criteria.any?
  target = prep_target(target)
  tars = target_to_tars(target)
  scrub_matches = match_criteria(tars, include_or_equal, criteria)
  string_match = stringify_matches(scrub_matches)
  hash = match_to_hash(hash, string_match, oa_name)
end

#stringify_matches(matches = []) ⇒ Object



29
30
31
# File 'lib/scrub_db/filter.rb', line 29

def stringify_matches(matches=[])
  string_match = matches&.uniq&.sort&.join(', ') if matches.any?
end

#target_to_tars(target) ⇒ Object



58
59
60
# File 'lib/scrub_db/filter.rb', line 58

def target_to_tars(target)
  tars = target.is_a?(::String) ? target.split(' ') : target
end