Class: SimpleTaggable::TagList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simple_taggable/tag_list.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*tags, filters: [], converters: []) ⇒ TagList

Returns a new instance of TagList.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/simple_taggable/tag_list.rb', line 27

def initialize(*tags, filters: [], converters: [])
  @raw_data = []

  @filters = filters.freeze
  @converters = converters.freeze

  tags.flatten.each do |tag|
    if tag.is_a?(TagList)
      tag.each { |t| add(t) }
    else
      add(tag)
    end
  end
end

Class Method Details

.[](*tags) ⇒ Object



22
23
24
# File 'lib/simple_taggable/tag_list.rb', line 22

def [](*tags)
  new(*tags)
end

Instance Method Details

#add(*tags) ⇒ Object Also known as: <<



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/simple_taggable/tag_list.rb', line 42

def add(*tags)
  tags.flatten.each do |t|
    tag = @converters.inject(t.to_s) do |pre_tag, converter|
      converter.call(self, pre_tag)
    end

    if @filters.all? { |filter| filter.call(self, tag) }
      tag.freeze
      @raw_data << tag
    end
  end
  @raw_data.uniq!

  self
end

#inspectObject



63
64
65
# File 'lib/simple_taggable/tag_list.rb', line 63

def inspect
  "SimpleTaggable::TagList#{raw_data.inspect}"
end

#to_aObject



59
60
61
# File 'lib/simple_taggable/tag_list.rb', line 59

def to_a
  @raw_data.deep_dup
end