Class: FundraisingProgram::FundRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ FundRequest

Returns a new instance of FundRequest.



10
11
12
13
# File 'lib/fundraising_program/fundrequest.rb', line 10

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/fundraising_program/fundrequest.rb', line 8

def name
  @name
end

Instance Method Details

#add_project(project) ⇒ Object



31
32
33
# File 'lib/fundraising_program/fundrequest.rb', line 31

def add_project(project)
	@projects << project
end

#fully_funded_projectsObject



62
63
64
# File 'lib/fundraising_program/fundrequest.rb', line 62

def fully_funded_projects
	@projects.select { |project| project.fully_funded? }
end

#load_projects(from_file) ⇒ Object



15
16
17
18
19
# File 'lib/fundraising_program/fundrequest.rb', line 15

def load_projects(from_file)
	File.readlines(from_file).each do |line|
		add_project(Project.from_csv(line))
	end 
end


54
55
56
# File 'lib/fundraising_program/fundrequest.rb', line 54

def print_project_funding_left(project)
	"#{project.name}".ljust(30, '.') + "#{project.funding_left}"
end


50
51
52
# File 'lib/fundraising_program/fundrequest.rb', line 50

def print_project_stats(project)
	"#{project.name}".ljust(30, '.') + "#{project.funding}".ljust(9,'.') + "#{project.target_funding}"
end


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fundraising_program/fundrequest.rb', line 66

def print_stats
	puts "\nStats for #{@name}"
	# fully_funded, under_funded = @projects.partition { |project| project.fully_funded? }
	
	fully_funded = fully_funded_projects
	under_funded = under_funded_projects

	puts "\nFully funded projecs:"
	fully_funded.each { |project| puts print_project_stats(project) }

	puts "\nUnder funded projecs:"
	under_funded.each { |project| puts print_project_stats(project) }

	puts "\nProjects with more fundings left"
	under_funded.sort.each { |project| puts print_project_funding_left(project) }

	@projects.each do |project|
		puts "\nProject #{project.name} pledges:"
		project.each_pledge do |pledge|
			puts "$#{pledge.amount} in #{pledge.name} pledges"
		end
		puts "$#{project.pledges_amount} in total pledges"
	end

	# @projects.sort.each { |project| puts print_project_funding_left(project) unless project.fully_funded? }
end

#request_funding(rounds) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fundraising_program/fundrequest.rb', line 35

def request_funding(rounds)
	pledges = PledgePool::PLEDGES
	puts "There are #{pledges.size} pledges:"
	pledges.each do |pledge|
		puts "\tA #{pledge.name} plage is worth #{pledge.amount}"
	end

	1.upto(rounds) do |round|
		puts "\nRequest Funding on Round: #{round}"
		@projects.each do |project|
			FundingRound.take_turn(project)
		end
	end
end

#save_under_funded_projects(to_file) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/fundraising_program/fundrequest.rb', line 21

def save_under_funded_projects(to_file)
	File.open(to_file, "w") do |file|
		file.puts "Under funded projecs:"
		under_funded_projects.each do |project|
			file.puts print_project_stats(project) 
			# file << print_project_stats(project) 
		end
	end
end

#total_pledges_amountObject

def print_project_stats(project) puts “#FundraisingProgram::FundRequest.projectproject.name”.ljust(30,‘.’) + “#FundraisingProgram::FundRequest.projectproject.funding_left” end



97
98
99
# File 'lib/fundraising_program/fundrequest.rb', line 97

def total_pledges_amount
	@projects.each.reduce(0) { |sum, project| sum += project.pledges_amount }
end

#under_funded_projectsObject



58
59
60
# File 'lib/fundraising_program/fundrequest.rb', line 58

def under_funded_projects
	under_funded = @projects.reject { |project| project.fully_funded? }
end