Class: Labelized::LabelList
- Inherits:
- 
      Array
      
        - Object
- Array
- Labelized::LabelList
 
- Defined in:
- lib/labelized/label_list.rb
Instance Attribute Summary collapse
- 
  
    
      #owner  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute owner. 
Class Method Summary collapse
- 
  
    
      .from(string)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns a new LabelList using the given string. 
Instance Method Summary collapse
- 
  
    
      #add(*names)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Add labels to the list. 
- 
  
    
      #initialize(*args)  ⇒ LabelList 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of LabelList. 
- 
  
    
      #remove(*names)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Remove specific labels from the list. 
- 
  
    
      #to_s  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Transform the list into a label string suitable for edting in a form. 
Constructor Details
#initialize(*args) ⇒ LabelList
Returns a new instance of LabelList.
| 10 11 12 | # File 'lib/labelized/label_list.rb', line 10 def initialize(*args) add(*args) end | 
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
| 8 9 10 | # File 'lib/labelized/label_list.rb', line 8 def owner @owner end | 
Class Method Details
.from(string) ⇒ Object
Returns a new LabelList using the given string.
LabelList.delimiter can be used to customize how labels in the string are delimited
Example:
LabelList.from("One , Two,  Three") # ["One", "Two", "Three"]
| 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # File 'lib/labelized/label_list.rb', line 21 def self.from(string) glue = delimiter.ends_with?(" ") ? delimiter : "#{delimiter} " string = string.join(glue) if string.respond_to?(:join) new.tap do |tag_list| string = string.to_s.dup # Parse the quoted labels string.gsub!(/(\A|#{delimiter})\s*"(.*?)"\s*(#{delimiter}\s*|\z)/) { tag_list << $2; $3 } string.gsub!(/(\A|#{delimiter})\s*'(.*?)'\s*(#{delimiter}\s*|\z)/) { tag_list << $2; $3 } tag_list.add(string.split(delimiter)) end end | 
Instance Method Details
#add(*names) ⇒ Object
Add labels to the list. Duplicate or blank labels will be ignored. Use the :parse option to add an unparsed label string.
Example:
list.add("Fun", "Happy")
list.add("Fun, Happy", :parse => true)
| 43 44 45 46 47 48 | # File 'lib/labelized/label_list.rb', line 43 def add(*names) (names) concat(names) clean! self end | 
#remove(*names) ⇒ Object
Remove specific labels from the list. Use the :parse option to add an unparsed label string.
Example:
list.remove("Sad", "Lonely")
list.remove("Sad, Lonely", :parse => true)
| 57 58 59 60 61 | # File 'lib/labelized/label_list.rb', line 57 def remove(*names) (names) delete_if { |name| names.include?(name) } self end | 
#to_s ⇒ Object
| 70 71 72 73 74 75 76 77 | # File 'lib/labelized/label_list.rb', line 70 def to_s = frozen? ? self.dup : self .send(:clean!) .map do |name| name.include?(delimiter) ? "\"#{name}\"" : name end.join(delimiter.ends_with?(" ") ? delimiter : "#{delimiter} ") end |