Class: CrowdFund::FundRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/crowdfund/fundrequest.rb

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ FundRequest

Returns a new instance of FundRequest.



9
10
11
12
# File 'lib/crowdfund/fundrequest.rb', line 9

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

Instance Method Details

#add_project(name) ⇒ Object



14
15
16
# File 'lib/crowdfund/fundrequest.rb', line 14

def add_project(name)
	@projects << name
end

#load_project(from_file = 'projects.csv') ⇒ Object



18
19
20
21
22
23
# File 'lib/crowdfund/fundrequest.rb', line 18

def load_project(from_file='projects.csv')
	CSV.foreach(from_file).each do |row|
		project = Project.new(row[0], row[1].to_i, row[2].to_i)
		add_project(project)
	end
end

#pledge_contributions(project) ⇒ Object



25
26
27
28
# File 'lib/crowdfund/fundrequest.rb', line 25

def pledge_contributions(project)
	formatted_name= project.name.ljust(20, '.')
	puts "#{formatted_name} #{project.funding_needed}"
end


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/crowdfund/fundrequest.rb', line 43

def print_stats
	fully_funded, under_funded= @projects.partition { |project| project.funded?}
	puts "\n#{@title} Statistics:\n"

	puts "\n#{fully_funded.size} has been fully_funded:\n"
	fully_funded.each do |project|
		puts "#{project.name} (#{project.funding})\n"
	end

	puts "\n#{under_funded.size} has been under_funded:\n"
	under_funded.each do |project|
		puts "#{project.name} (#{project.funding})"
	end

	puts "\n#{under_funded.size} projects needing contributions:"
	@projects.sort.each do |project|
		pledge_contributions(project)
	end

	@projects.each do |project|
		puts "#{project.name}'s total funds:\n"
		puts "\n$#{project.funds} total funds."
	end
	puts "\n$#{total_funds} total funds from pledges."

	@projects.each do |project|
		puts "\nProject #{project.name} pledges:\n"
		project.each_pledge_donated do |pledge|
			puts "$#{pledge.amount} in #{pledge.level} pledges"
		end
		puts "$#{project.funds} in total pledges\n"
	end
end

#request_funding(rounds) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/crowdfund/fundrequest.rb', line 77

def request_funding(rounds)
	puts "There are #{@projects.size} projects:"
	@projects.each do |project|
		puts project
	end
	pledges= Pledge::PLEDGES
	puts "There are #{pledges.size} possible pledge amounts:\n"
	pledges.each do |pledge|
		puts "A #{pledge.level} pledge is worth $#{pledge.amount}."
	end
	1.upto(rounds).each do |round|
		puts "\n Round #{round}:"
		@projects.each do |project|
			FundingRound.donations_made(project)
			puts project
		end
	end	
end

#save_pledge_contributions(to_file = 'funding_needed.txt') ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/crowdfund/fundrequest.rb', line 30

def save_pledge_contributions(to_file= 'funding_needed.txt')
	File.open(to_file, "w") do |file|
		file.puts "#{@title} still needing funding:"
		@projects.sort.each do |project|
			file.puts pledge_contributions(project)
		end
	end
end

#total_fundsObject



39
40
41
# File 'lib/crowdfund/fundrequest.rb', line 39

def total_funds
	@projects.reduce(0) {|sum, p| sum + p.funds}
end