Class: ScrubDb::Strings

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(criteria = {}) ⇒ Strings

Returns a new instance of Strings.



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

def initialize(criteria={})
  @empty_criteria = criteria&.empty?
  @filter = ScrubDb::Filter.new(criteria) unless @empty_criteria
end

Instance Attribute Details

#empty_criteriaObject

Returns the value of attribute empty_criteria.



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

def empty_criteria
  @empty_criteria
end

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

Instance Method Details

#merge_criteria(hashes) ⇒ Object



28
29
30
31
32
# File 'lib/scrub_db/strings.rb', line 28

def merge_criteria(hashes)
  hashes.map do |hsh|
    hsh.merge({ pos_criteria: [], neg_criteria: [] })
  end
end

#scrub_hash(hsh) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scrub_db/strings.rb', line 34

def scrub_hash(hsh)
  str = hsh[:string]
  prop = hsh[:proper_f]

  if str.present?
    hsh = @filter.scrub_oa(hsh, str, 'neg_criteria', 'include')
    hsh = @filter.scrub_oa(hsh, str, 'pos_criteria', 'include')
  end

  if prop.present?
    hsh = @filter.scrub_oa(hsh, prop, 'neg_criteria', 'include')
    hsh = @filter.scrub_oa(hsh, prop, 'pos_criteria', 'include')
  end
  hsh
end

#scrub_proper_strings(props = []) ⇒ Object



12
13
14
15
16
# File 'lib/scrub_db/strings.rb', line 12

def scrub_proper_strings(props=[])
  prop_hashes = CrmFormatter.format_propers(props)
  prop_hashes = merge_criteria(prop_hashes)
  prop_hashes.map! { |prop_hsh| scrub_hash(prop_hsh) }
end

#scrub_strings(strings = []) ⇒ Object



18
19
20
21
22
# File 'lib/scrub_db/strings.rb', line 18

def scrub_strings(strings=[])
  str_hashes = strings_to_hashes(strings)
  str_hashes = merge_criteria(str_hashes)
  str_hashes.map! { |str_hsh| scrub_hash(str_hsh) }
end

#strings_to_hashes(strings) ⇒ Object



24
25
26
# File 'lib/scrub_db/strings.rb', line 24

def strings_to_hashes(strings)
  str_hashes = strings.map { |str| { string: str } }
end