Module: Inform::Taggable

Includes:
TagHelpers
Included in:
Object, System::Object
Defined in:
lib/runtime/tag.rb

Overview

The Inform::Taggable module

Instance Method Summary collapse

Methods included from TagHelpers

#attributes, #has, #has?, #hasany?, #hasnt, #hasnt?

Instance Method Details

#nil_safe_tagsObject



247
248
249
250
251
252
253
254
# File 'lib/runtime/tag.rb', line 247

def nil_safe_tags
  attempt_twice do
    log.warn "The tags for #{self} is nil" if self.tags.nil?
    # TODO: FIXME
    # Perhaps try adding a semaphore lock here around tags
    self.tags.compact.map(&:to_s).map(&:intern)
  end || Array::Empty
end

#tag(*args) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/runtime/tag.rb', line 200

def tag(*args)
  # TODO: Generalize workflag to system flags
  workflags.add(self.identity) if args.delete(:workflag)
  a = self.tag_names args
  (a - (self.nil_safe_tags & a)).each do |tag|
    add_tag(Inform::Tag.find_or_create(name: tag.to_s))
  end
  self.save_changes if self.respond_to?(:save_changes)
end

#tag_names(a) ⇒ Object



243
244
245
# File 'lib/runtime/tag.rb', line 243

def tag_names(a)
  a.flatten.map(&:to_s).map(&:intern)
end

#tagged_with?(tag) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/runtime/tag.rb', line 220

def tagged_with?(tag)
  tagged_with_any? tag
end

#tagged_with_all?(*args) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
227
228
229
# File 'lib/runtime/tag.rb', line 224

def tagged_with_all?(*args)
  # TODO: Generalize workflag to system flags
  return workflags.include?(self.identity) if args.delete(:workflag)
  a = self.tag_names(args)
  (self.nil_safe_tags & a).length == a.length
end

#tagged_with_any?(*args) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
234
# File 'lib/runtime/tag.rb', line 231

def tagged_with_any?(*args)
  # TODO: Generalize workflag to system flags
  (args.delete(:workflag) && workflags.include?(self.identity)) || !self.tagged_with_none?(*tag_names(args))
end

#tagged_with_none?(*args) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
239
240
241
# File 'lib/runtime/tag.rb', line 236

def tagged_with_none?(*args)
  # TODO: Generalize workflag to system flags
  # puts "workflags: #{workflags}" if args.include?(:workflag)
  return false if args.delete(:workflag) && workflags.include?(self.identity)
  (nil_safe_tags & tag_names(args)).empty?
end

#untag(*args) ⇒ Object Also known as: tag!



210
211
212
213
214
215
216
217
# File 'lib/runtime/tag.rb', line 210

def untag(*args)
  # TODO: Generalize workflag to system flags
  workflags.delete(self.identity) if args.delete(:workflag)
  (self.nil_safe_tags & tag_names(args)).each do |tag|
    remove_tag Inform::Tag.find(name: tag.to_s)
  end
  self.save_changes if self.respond_to?(:save_changes)
end