Class: Tagtical::TagList
- Inherits:
-
Array
- Object
- Array
- Tagtical::TagList
- Defined in:
- lib/tagtical/tag_list.rb
Defined Under Namespace
Classes: TagValue
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
Class Method Summary collapse
-
.from(*args) ⇒ Object
Returns a new TagList using the given tag string.
Instance Method Summary collapse
-
#add(*values) ⇒ Object
Add tags to the tag_list.
- #concat(values) ⇒ Object
-
#find(value) ⇒ Object
Shorthand.
-
#initialize(*args) ⇒ TagList
constructor
A new instance of TagList.
- #push(value) ⇒ Object (also: #<<)
-
#remove(*values) ⇒ Object
Remove specific tags from the tag_list.
-
#to_s(mode = nil) ⇒ Object
Transform the tag_list into a tag string suitable for edting in a form.
-
#to_sql_conditions(options = {}) ⇒ Object
Builds an option statement for an ActiveRecord table.
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
#owner ⇒ Object
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) (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) (values) delete_if { |value| values.include?(value) } self end |
#to_s(mode = nil) ⇒ Object
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(={}) .reverse_merge!(:class => Tagtical::Tag, :column => "value", :operator => "=") "(" + map { |t| [:class].send(:sanitize_sql, ["#{[:class].table_name}.#{[:column]} #{[:operator]} ?", t]) }.join(" OR ") + ")" end |