Class: Tagtical::TagList

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

Defined Under Namespace

Classes: TagValue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TagList

Returns a new instance of TagList.



29
30
31
# File 'lib/tagtical/tag_list.rb', line 29

def initialize(*args)
  add(*args) unless args.empty?
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



27
28
29
# File 'lib/tagtical/tag_list.rb', line 27

def owner
  @owner
end

Class Method Details

.from(*args) ⇒ Object

Returns a new TagList using the given tag string.

Example:

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


39
40
41
# File 'lib/tagtical/tag_list.rb', line 39

def self.from(*args)
  args[0].is_a?(self) ? args[0] : new(*args)
end

Instance Method Details

#add(*values) ⇒ Object

Add tags to the tag_list. Duplicate or blank tags will be ignored. Use the :parse option to add an unparsed tag string.

Example:

tag_list.add("Fun", "Happy")
tag_list.add("Fun, Happy")
tag_list.add("Fun" => "0.546", "Happy" => 0.465) # add relevance


65
66
67
68
69
70
71
# File 'lib/tagtical/tag_list.rb', line 65

def add(*values)
  extract_and_apply_options!(values)
  clean!(values) do
    concat(values)
  end
  self
end

#concat(values) ⇒ Object



43
44
45
# File 'lib/tagtical/tag_list.rb', line 43

def concat(values)
  super(values.map! { |v| convert_tag_value(v) })
end

#find(value) ⇒ Object

Shorthand



53
54
55
# File 'lib/tagtical/tag_list.rb', line 53

def find(value)
  detect { |t| t==value }
end

#push(value) ⇒ Object Also known as: <<



47
48
49
# File 'lib/tagtical/tag_list.rb', line 47

def push(value)
  super(convert_tag_value(value))
end

#remove(*values) ⇒ 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")


80
81
82
83
84
# File 'lib/tagtical/tag_list.rb', line 80

def remove(*values)
  extract_and_apply_options!(values)
  delete_if { |value| values.include?(value) }
  self
end

#to_s(mode = nil) ⇒ Object

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: 1.3", "Square,Cube")
tag_list.to_s             # 'Round, "Square,Cube"'
tag_list.to_s(:relevance) # 'Round:1.3, "Square,Cube"'


94
95
96
97
98
99
100
101
# File 'lib/tagtical/tag_list.rb', line 94

def to_s(mode=nil)
  tag_list = frozen? ? self.dup : self
  tag_list.send(:clean!)
  tag_list.map do |tag_value|
    value = tag_value.include?(delimiter) ? %{"#{tag_value}"} : tag_value
    [value, (tag_value.relevance if mode==:relevance)].compact.join(TagValue.relevance_delimiter)
  end.join(delimiter.gsub(/(\S)$/, '\1 '))
end

#to_sql_conditions(options = {}) ⇒ Object

Builds an option statement for an ActiveRecord table.



104
105
106
107
# File 'lib/tagtical/tag_list.rb', line 104

def to_sql_conditions(options={})
  options.reverse_merge!(:class => Tagtical::Tag, :column => "value", :operator => "=")
  "(" + map { |t| options[:class].send(:sanitize_sql, ["#{options[:class].table_name}.#{options[:column]} #{options[:operator]} ?", t]) }.join(" OR ") + ")"
end