Class: Inform::Tag

Inherits:
Sequel::Model show all
Defined in:
lib/story_teller/inform/relational/tag.rb

Overview

class Tag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sequel::Model

implicit_table_name

Methods included from StoryTeller::InheritanceListener

included

Class Method Details

.dirtyObject

rubocop: disable Style/FormatStringToken



91
92
93
94
95
96
97
98
# File 'lib/story_teller/inform/relational/tag.rb', line 91

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



102
103
104
105
106
107
108
109
# File 'lib/story_teller/inform/relational/tag.rb', line 102

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



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

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



77
78
79
# File 'lib/story_teller/inform/relational/tag.rb', line 77

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

#after_createObject



62
63
64
65
# File 'lib/story_teller/inform/relational/tag.rb', line 62

def after_create
  super
  Inform.attributes.reset
end

#after_saveObject



67
68
69
70
# File 'lib/story_teller/inform/relational/tag.rb', line 67

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

#before_createObject



57
58
59
60
# File 'lib/story_teller/inform/relational/tag.rb', line 57

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

#to_sObject Also known as: to_str



72
73
74
# File 'lib/story_teller/inform/relational/tag.rb', line 72

def to_s
  name
end