Class: CrowdFund::ProjectManager

Inherits:
Object
  • Object
show all
Defined in:
lib/crowd_fund/project_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ ProjectManager

Returns a new instance of ProjectManager.



9
10
11
12
# File 'lib/crowd_fund/project_manager.rb', line 9

def initialize(title)
  @title = title
  @projects = []
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/crowd_fund/project_manager.rb', line 7

def title
  @title
end

Instance Method Details

#add_project(project) ⇒ Object



14
15
16
# File 'lib/crowd_fund/project_manager.rb', line 14

def add_project(project)
  @projects << project
end

#load_projects(from_file) ⇒ Object



18
19
20
21
22
23
# File 'lib/crowd_fund/project_manager.rb', line 18

def load_projects(from_file)
  CSV.foreach(from_file) do |row|
    project = Project.new(row[0], row[1].to_i)
    add_project(project)
  end
end


49
50
51
# File 'lib/crowd_fund/project_manager.rb', line 49

def print_name_and_total_funding_received(project)
  puts "#{project.name} (#{project.total_funding_received})"
end


25
26
27
28
29
30
31
32
# File 'lib/crowd_fund/project_manager.rb', line 25

def print_pledge_amounts
  pledges = PledgePool::PLEDGES

  puts "\nThere are #{pledges.size} possible pledge amounts:"
  pledges.each do |pledge|
    puts "\tA #{pledge.name} pledge is worth $#{pledge.amount}."
  end
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/crowd_fund/project_manager.rb', line 53

def print_stats 
  puts "\n#{@title} Statistics:"

  fully_funded_projects, under_funded_projects = @projects.partition { |p| p.fully_funded?  }

  puts "\n#{fully_funded_projects.size} fully-funded projects:"
  fully_funded_projects.each do |p|
    print_name_and_total_funding_received(p)
  end

  puts "\n#{under_funded_projects.size} under-funded projects:"
  under_funded_projects.each do |p|
    print_name_and_total_funding_received(p)
  end

  @projects.each do |project|
    puts "\nProject #{project.name}'s pledges:"
    project.each_received_pledge do |pledge|
      puts "$#{pledge.amount} in #{pledge.name} pledges"
    end
  end

  puts "\n#{@title} Needing Contributions:"
  under_funded_projects.sort { |p| p.total_funding_still_needed }.each do |p|
    formatted_name = p.name.ljust(20, '.')
    puts "#{formatted_name} #{p.total_funding_still_needed}"
  end
end

#request_funding(rounds) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crowd_fund/project_manager.rb', line 34

def request_funding(rounds)
  puts "There are #{@projects.size} projects in #{title}:"
  puts @projects

  print_pledge_amounts    
  puts "\n"

  1.upto(rounds) do
    @projects.each do |project|
      FundingRound.fund(project)
      puts project
    end
  end
end

#save_needed_funding(to_file = "needed_funding.txt") ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/crowd_fund/project_manager.rb', line 82

def save_needed_funding(to_file="needed_funding.txt")
  File.open(to_file, "w") do |file|
    file.puts "#{@title} Needing Funding:"
    @projects.select { |project| project.total_funding_still_needed > 0 }.sort.each do |project|
      file.puts(project.funding_needed_entry)
    end
  end
end