Class: Taglish::TagList

Inherits:
Array
  • Object
show all
Defined in:
lib/taglish/tag_list.rb

Overview

Contains a list of strings. Works like an array.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_type, *args) ⇒ TagList

Returns a new instance of TagList.



10
11
12
13
# File 'lib/taglish/tag_list.rb', line 10

def initialize(tag_type, *args)
  self.tag_type = tag_type  or raise "tag_type is required"
  add(*args)
end

Instance Attribute Details

#tag_typeObject

Returns the value of attribute tag_type.



7
8
9
# File 'lib/taglish/tag_list.rb', line 7

def tag_type
  @tag_type
end

#taggableObject

Returns the value of attribute taggable.



8
9
10
# File 'lib/taglish/tag_list.rb', line 8

def taggable
  @taggable
end

Class Method Details

.from(tag_type, string) ⇒ Object

Returns a new TagList using the given tag string.

Example:

tag_list = TagList.from("One , Two,  Three")
tag_list # ["One", "Two", "Three"]


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

def self.from(tag_type, string)
  string = string.join(tag_type.glue) if string.respond_to?(:join)

  new(tag_type).tap do |tag_list|
    string = string.to_s.dup

    # Parse the quoted tags
    d = tag_type.delimiter
    d = d.join("|") if d.kind_of?(Array) 
    string.gsub!(/(\A|#{d})\s*"(.*?)"\s*(#{d}\s*|\z)/) { tag_list << $2; $3 }
    string.gsub!(/(\A|#{d})\s*'(.*?)'\s*(#{d}\s*|\z)/) { tag_list << $2; $3 }

    tag_list.add(string.split(Regexp.new d))
  end
end

Instance Method Details

#add(*names) ⇒ Object

Add tags to the tag_list. Duplicate or blank tags will be ignored.

Example:

tag_list.add("Fun", "Happy")
tag_list.add("Fun, Happy", :parse => true)


21
22
23
24
25
26
# File 'lib/taglish/tag_list.rb', line 21

def add(*names)
  extract_and_apply_options!(names)
  concat(names)
  clean!
  self
end

#remove(*names) ⇒ Object

Remove specific tags from the tag_list. Use the :parse option to add an unparsed tag string.

Example:

tag_list.remove("Sad", "Lonely")
tag_list.remove("Sad, Lonely", :parse => true)


57
58
59
60
61
62
63
64
65
# File 'lib/taglish/tag_list.rb', line 57

def remove(*names)
  extract_and_apply_options!(names)
  if tag_type.scored
    delete_if { |name| names.include?(name.sub(Taglish::Taggable::SCORED_TAG_REGEX, '\1')) }
  else
    delete_if { |name| names.include?(name) }
  end
  self
end

#to_sObject

Transform the tag_list into a tag string suitable for edting in a form. The tags are joined with TagList.delimiter and quoted if necessary.

Example:

tag_list = TagList.new("Round", "Square,Cube")
tag_list.to_s # 'Round, "Square,Cube"'


74
75
76
77
78
79
80
81
82
83
# File 'lib/taglish/tag_list.rb', line 74

def to_s
  tags = frozen? ? self.dup : self
  tags.send(:clean!)

  tags.map do |name|
    d = tag_type.delimiter
    d = Regexp.new d.join("|") if d.kind_of? Array
    name.index(d) ? "\"#{name}\"" : name
  end.join(tag_type.glue)
end

#to_tagging_arrayObject



85
86
87
88
89
90
# File 'lib/taglish/tag_list.rb', line 85

def to_tagging_array
  map { |name|
    ar = tag_type.name_and_score(name)
    Taglish::Tagging.new(:name => ar[0], :score => ar[1])
  }
end