Class: EasyTags::TaggableContext

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_tags/taggable_context.rb

Overview

Handles tag context manipulation

Instance Method Summary collapse

Constructor Details

#initialize(context:, refresh_persisted_tags:, on_change:) ⇒ TaggableContext

Returns a new instance of TaggableContext.

Parameters:

  • context (String, Symbol)
  • refresh_persisted_tags (Proc)
  • on_change (Proc)


7
8
9
10
11
# File 'lib/easy_tags/taggable_context.rb', line 7

def initialize(context:, refresh_persisted_tags:, on_change:)
  self.context = context
  self.refresh_persisted_tags = refresh_persisted_tags
  self.on_change = on_change
end

Instance Method Details

#changed?true, false

Returns:

  • (true, false)


14
15
16
# File 'lib/easy_tags/taggable_context.rb', line 14

def changed?
  tags.sort != persisted_tags.sort
end

#new_tagsTagList

Returns:



37
38
39
# File 'lib/easy_tags/taggable_context.rb', line 37

def new_tags
  tags - persisted_tags
end

#persisted_tagsTagList

Returns:



24
25
26
# File 'lib/easy_tags/taggable_context.rb', line 24

def persisted_tags
  @persisted_tags ||= TagList.new(refresh_persisted_tags.call)
end

#refreshObject

clear memoized info and force a refresh



47
48
49
50
# File 'lib/easy_tags/taggable_context.rb', line 47

def refresh
  @tags = nil
  @persisted_tags = nil
end

#removed_tagsTagList

Returns:



42
43
44
# File 'lib/easy_tags/taggable_context.rb', line 42

def removed_tags
  persisted_tags - tags
end

#tagsTagList

Returns:



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

def tags
  @tags ||= TagList.new(persisted_tags)
end

#update(value) ⇒ TagList

Parameters:

  • value (String, Symbol)

Returns:



30
31
32
33
34
# File 'lib/easy_tags/taggable_context.rb', line 30

def update(value)
  @tags = TagList.new(value)

  on_change.call(self) if changed?
end