Module: RightSignature::Helpers::TagsHelper

Defined in:
lib/rightsignature/helpers/normalizing.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.array_and_metadata_to_string_array(array_of_tags, hash_of_metadata) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rightsignature/helpers/normalizing.rb', line 4

def (array_of_tags, )
  array_of_tags ||= []
   ||= {}
  if !(array_of_tags.is_a?(Array) && .is_a?(Hash))
    return raise ArgumentError, "Objects must be an array and a hash"
  end

  tags_array =
    array_of_tags.collect{|t| CGI.escape(t)} +
    .collect{ |k,v|
      "#{CGI.escape(k.to_s)}:#{CGI.escape(v.to_s)}" if ! v.to_s.strip.empty?
    }.select{ |t|
      ! t.to_s.strip.empty?
    }

  tags_array.join ','
end

.array_to_xml_hash(tags_array) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/rightsignature/helpers/normalizing.rb', line 68

def array_to_xml_hash(tags_array)
  tags_array.map do |t|
    if t.is_a? Hash
      name, value = t.first
      {:tag => {:name => name.to_s, :value => value.to_s}}
    else
      {:tag => {:name => t.to_s}}
    end
  end
end

.metadata_hash_from_tags_string(tags_string) ⇒ Object



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

def  tags_string
  tags_string ||= ""

  if ! tags_string.is_a? String
    return raise ArgumentError, "Object must be a string"
  end

  Hash[
    tags_string.split(',')
      .select{ |t| t =~ /:/}
      .collect{ |t|
        md = t.split(':')
        [CGI.unescape(md[0]), CGI.unescape(md[1])]
      }
  ]
end

.mixed_array_to_string_array(array_of_tags) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rightsignature/helpers/normalizing.rb', line 51

def mixed_array_to_string_array(array_of_tags)
  return array_of_tags unless array_of_tags.is_a?(Array)

  tags = []
  array_of_tags.each_with_index do |tag, idx|
    if tag.is_a? Hash
      tags << tag.collect{|k,v| "#{CGI.escape(k.to_s)}:#{CGI.escape(v.to_s)}"}
    elsif tag.is_a? String
      tags << CGI.escape(tag.to_s)
    else
      raise "Tags should be an array of Strings ('tag_name') or Hashes ({'tag_name' => 'value'})"
    end
  end

  tags.join(',')
end

.tags_array_from_tags_string(tags_string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rightsignature/helpers/normalizing.rb', line 22

def tags_array_from_tags_string tags_string
  tags_string ||= ""

  if ! tags_string.is_a? String
    return raise ArgumentError, "Object must be a string"
  end

  tags_string.split(',')
    .select{|t| t !~ /:/}
    .collect{|t| CGI.unescape(t)}
end