Class: Tags

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tags.rb,
lib/tags/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(str_or_ary = nil) ⇒ Tags

Returns a new instance of Tags.



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

def initialize(str_or_ary = nil)
  ary = str_or_ary.is_a?(Array) ? str_or_ary : str_or_ary.to_s.split(' ')
  @tags_set = Set.new(ary)
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/tags.rb', line 33

def +(other)
  raise ArgumentError, "must be a #{self.class} object" unless other.is_a?(self.class)
  ary_add = (tags_set + other.to_set).to_a
  self.class.new(ary_add)
end

#-(other) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/tags.rb', line 27

def -(other)
  raise ArgumentError, "must be a #{self.class} object" unless other.is_a?(self.class)
  ary_diff = (tags_set - other.to_set).to_a
  self.class.new(ary_diff)
end

#<=>(other) ⇒ Object



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

def <=>(other)
  tags_set <=> other.to_set
end

#to_aObject



19
20
21
# File 'lib/tags.rb', line 19

def to_a
  tags_set.to_a
end

#to_sObject



15
16
17
# File 'lib/tags.rb', line 15

def to_s
  to_a.join(' ')
end

#to_setObject



23
24
25
# File 'lib/tags.rb', line 23

def to_set
  tags_set.dup
end