Class: Inform::Ephemeral::Tag

Inherits:
Tag show all
Defined in:
lib/story_teller/inform/ephemeral/tag.rb

Overview

class Tag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

adapter_class, new

Methods included from AdapterClassDelegation

#===, #delegated_adapter_class, #method_missing, #respond_to_missing?

Constructor Details

#initialize(name) ⇒ Tag

Returns a new instance of Tag.



31
32
33
34
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 31

def initialize(name)
  super()
  @name = name
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



29
30
31
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 29

def created_at
  @created_at
end

#modified_atObject

Returns the value of attribute modified_at.



29
30
31
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 29

def modified_at
  @modified_at
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 29

def name
  @name
end

Class Method Details

.dirtyObject

rubocop: disable Style/FormatStringToken



70
71
72
73
74
75
76
77
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 70

def self.dirty
  results = db.fetch %(select a.id, a.name from tag a group by a.id, a.name having
((select count(b.id) from tagged b where b.tag_id = a.id) = 0))
  return nil if results.empty?
  s = [format('%5s %20s', *results.first.keys)]
  s.concat(results.collect { |row| format('%5d %20s', *row.values) })
  s.join("\n")
end

.statsObject

rubocop: disable Style/FormatStringToken



81
82
83
84
85
86
87
88
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 81

def self.stats
  results = db.fetch %(select a.*, count(a.id) as "number tagged"
from tag a, tagged b where b.tag_id = a.id group by a.id, a.name)
  return nil if results.empty?
  s = [format('%5s %20s %15s', *results.first.keys)]
  s.concat(results.collect { |row| format('%5d %20s %15s', *row.values) })
  s.join("\n")
end

.tidyObject



60
61
62
63
64
65
66
67
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 60

def self.tidy
  db << %(delete from tag where id in
(select a.id from tag a group by a.id, a.name
having ((select count(b.id) from tagged b
where b.tag_id = a.id) = 0)))
  Inform.attributes.reset
  return nil
end

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 56

def <=>(other)
  self.name <=> other.name
end

#after_createObject



41
42
43
44
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 41

def after_create
  super
  Inform.attributes.reset
end

#after_saveObject



46
47
48
49
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 46

def after_save
  super
  self.modified_at = Time.now.utc
end

#before_createObject



36
37
38
39
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 36

def before_create
  self.created_at ||= Time.now
  super
end

#to_sObject Also known as: to_str



51
52
53
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 51

def to_s
  name
end