Module: Cosm::Helpers

Instance Method Summary collapse

Instance Method Details

#join_tags(tags) ⇒ Object



35
36
37
38
# File 'lib/cosm-rb/helpers.rb', line 35

def join_tags(tags)
  return tags unless tags.is_a?(Array)
  tags.sort{|a,b| a.downcase <=> b.downcase}.join(',')
end

#parse_tag_string(string) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cosm-rb/helpers.rb', line 3

def parse_tag_string(string)
  return [] if string.blank?
  string = string.join(',') if string.is_a?(Array)
  tags = []
  quoted_mode = false
  tags << string.chars.reduce("") do |buffer, char|
    if char == ','
      if !quoted_mode
        tags << buffer
        buffer = ""
      else
        buffer << char
      end
    elsif char == '"'
      if buffer.length > 0 && buffer[-1,1] == '\\'
        buffer[-1] = char
      else
        quoted_mode = !quoted_mode
        buffer << char
      end
    else
      buffer << char
    end
    buffer
  end
  # strip the tags
  tags.map { |t|
    /\A\s*("(.*)"|(.*))\s*\Z/.match(t)
    $2 || $3.strip
  }.sort{|a,b| a.downcase <=> b.downcase}
end