Class: Inform::Ephemeral::Tag

Inherits:
Object
  • Object
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

Constructor Details

#initialize(name) ⇒ Tag

Returns a new instance of Tag.



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

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

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#modified_atObject

Returns the value of attribute modified_at.



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

def modified_at
  @modified_at
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.dirtyObject

rubocop: disable Style/FormatStringToken



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

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



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

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



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

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



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

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

#after_createObject



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

def after_create
  super
  Inform.attributes.reset
end

#after_saveObject



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

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

#before_createObject



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

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

#to_sObject Also known as: to_str



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

def to_s
  name
end