Module: Dump::Snapshot::CleanNParse

Included in:
Dump::Snapshot, Dump::Snapshot
Defined in:
lib/dump/snapshot.rb

Overview

Cleanup name of dump

Instance Method Summary collapse

Instance Method Details

#clean_description(description) ⇒ Object



157
158
159
# File 'lib/dump/snapshot.rb', line 157

def clean_description(description)
  clean_str(description, '()#')[0, 50].strip
end

#clean_str(str, additional = nil) ⇒ Object



153
154
155
# File 'lib/dump/snapshot.rb', line 153

def clean_str(str, additional = nil)
  str.to_s.strip.gsub(/\s+/, ' ').gsub(/[^A-Za-z0-9 \-_#{Regexp.escape(additional.to_s) if additional}]+/, '_')
end

#clean_tag(tag) ⇒ Object



161
162
163
# File 'lib/dump/snapshot.rb', line 161

def clean_tag(tag)
  clean_str(tag).downcase.sub(/^\-+/, '')[0, 20].strip
end

#clean_tags(tags) ⇒ Object



165
166
167
# File 'lib/dump/snapshot.rb', line 165

def clean_tags(tags)
  tags.to_s.split(',').map{ |tag| clean_tag(tag) }.uniq.reject(&:blank?).sort
end

#get_filter_tags(tags) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dump/snapshot.rb', line 169

def get_filter_tags(tags)
  groups = Hash.new{ |hash, key| hash[key] = SortedSet.new }
  tags.to_s.split(',').each do |tag|
    next unless (m = tag.strip.match(/^(\-|\+)?(.*)$/))
    type = {'+' => :mandatory, '-' => :forbidden}[m[1]] || :simple
    next unless (cleaned_tag = clean_tag(m[2])).present?
    groups[type] << cleaned_tag
  end
  [:simple, :mandatory].each do |type|
    if (clashing = (groups[type] & groups[:forbidden])).present?
      fail "#{type} tags clashes with forbidden ones: #{clashing}"
    end
  end
  groups.each_with_object({}){ |(key, value), hsh| hsh[key] = value.to_a }
end