Class: PivotalShell::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/pivotal_shell/cache.rb,
lib/pivotal_shell/cache/user.rb,
lib/pivotal_shell/cache/story.rb

Defined Under Namespace

Classes: Story, User

Constant Summary collapse

STORY_ATTRIBUTES =
%w(requested_by_id name id current_state accepted_at labels url estimate description created_at owned_by_id story_type)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Cache

Returns a new instance of Cache.



11
12
13
14
15
16
17
# File 'lib/pivotal_shell/cache.rb', line 11

def initialize(filename)
  @filename = filename
  @db = SQLite3::Database.new(@filename)
  @db.results_as_hash = true
  create_tables
  refresh_if_needed
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



9
10
11
# File 'lib/pivotal_shell/cache.rb', line 9

def db
  @db
end

Instance Method Details

#[](key) ⇒ Object



34
35
36
37
# File 'lib/pivotal_shell/cache.rb', line 34

def [](key)
  resultset = db.execute('SELECT value FROM settings WHERE name=?', key.to_s)
  resultset.empty? ? nil : YAML.load(resultset.first['value'])
end

#[]=(key, value) ⇒ Object



39
40
41
42
# File 'lib/pivotal_shell/cache.rb', line 39

def []=(key, value)
  db.execute('REPLACE INTO settings (name, value) VALUES (?,?)', key.to_s, YAML.dump(value))
  value
end

#inspectObject



19
20
21
# File 'lib/pivotal_shell/cache.rb', line 19

def inspect
  "#<PivotalShell::Cache #{@filename}>"
end

#refreshObject



23
24
25
# File 'lib/pivotal_shell/cache.rb', line 23

def refresh
  load_stories(:modified_since => self[:last_updated_at])
end

#reloadObject



27
28
29
30
31
32
# File 'lib/pivotal_shell/cache.rb', line 27

def reload
  db.execute('DELETE FROM users')
  db.execute('DELETE FROM stories')
  @users = nil
  load_stories
end