Class: ScrubDb::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Filter

Returns a new instance of Filter.



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

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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#empty_criteriaObject

Returns the value of attribute empty_criteria.



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

def empty_criteria
  @empty_criteria
end

Instance Method Details

#fetch_criteria(oa_name) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#target_to_tars(target) ⇒ Object



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

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