Class: PivotalShell::Cache::Story

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

Constant Summary collapse

ATTRIBUTES =
%w(requested_by_id name id current_state accepted_at labels url estimate description created_at owned_by_id story_type)
STATUSES =
%w(unscheduled unstarted started finished delivered accepted rejected)
TYPES =
%w(feature bug chore)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = {}) ⇒ Story

Returns a new instance of Story.



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

def initialize(source={})
  if source.is_a? PivotalTracker::Story
    raise 'TODO'
  elsif source.is_a? Hash
    create_from_hash(source)
  end
end

Class Method Details

.all(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pivotal_shell/cache/story.rb', line 24

def self.all(params)
  conditions = []
  query_params = []
  if params[:unowned]
    conditions << "owned_by_id IS NULL"
  elsif params[:owner] && (owner = PivotalShell::Cache::User.find(params[:owner]))
    conditions << "owned_by_id==?"
    query_params << owner.id
  end
  
  if params[:state]
    params[:state] = [params[:state]].flatten
    conditions << "current_state IN (#{(["?"]*params[:state].length).join(',')})"
    query_params << params[:state]
  end

  if params[:type]
    params[:type] = [params[:type]].flatten
    conditions << "story_type IN (#{(["?"]*params[:type].length).join(',')})"
    query_params << params[:type]
  end

  query = 'SELECT * FROM stories'
  query << ' WHERE '+conditions.map{|c| "(#{c})"}.join(' AND ') unless conditions.empty?
  PivotalShell::Configuration.cache.db.execute(query, query_params).map {|r| new(r)}
end

.find(id) ⇒ Object



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

def self.find(id)
  hash = PivotalShell::Configuration.cache.db.execute("SELECT * FROM stories WHERE id=?", id).first
  hash && new(hash)
end

Instance Method Details

#owned_byObject



51
52
53
54
# File 'lib/pivotal_shell/cache/story.rb', line 51

def owned_by
  return nil if owned_by_id.nil?
  @owned_by ||= PivotalShell::Cache::User.find(owned_by_id)
end

#requested_byObject



56
57
58
59
# File 'lib/pivotal_shell/cache/story.rb', line 56

def requested_by
  return nil if requested_by_id.nil?
  @requested_by ||= PivotalShell::Cache::User.find(requested_by_id)
end