Class: CrowdFund::FundRequest
- Inherits:
-
Object
- Object
- CrowdFund::FundRequest
- Defined in:
- lib/crowdfund/fundrequest.rb
Instance Method Summary collapse
- #add_project(project) ⇒ Object
-
#initialize(name) ⇒ FundRequest
constructor
A new instance of FundRequest.
- #load_project(from_file) ⇒ Object
- #print_stats ⇒ Object
- #project_and_fund(project) ⇒ Object
- #request_funding(weeks) ⇒ Object
- #save_fund_stats(to_file = 'fund_stats.txt') ⇒ Object
- #total_rewards ⇒ Object
Constructor Details
#initialize(name) ⇒ FundRequest
Returns a new instance of FundRequest.
10 11 12 13 |
# File 'lib/crowdfund/fundrequest.rb', line 10 def initialize(name) @name = name.upcase @projects = [] end |
Instance Method Details
#add_project(project) ⇒ Object
15 16 17 |
# File 'lib/crowdfund/fundrequest.rb', line 15 def add_project(project) @projects << project end |
#load_project(from_file) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/crowdfund/fundrequest.rb', line 19 def load_project(from_file) CSV.foreach(from_file) do |line| project = Project.new(line[0], Integer(line[1]), Integer(line[2])) add_project(project) end end |
#print_stats ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/crowdfund/fundrequest.rb', line 61 def print_stats fully_funded_projects, under_funded_projects = @projects.partition { |project| project.funded? } puts "\nFully funded projects (funds):" fully_funded_projects.each { |project| puts project_and_fund(project) } puts "\nUnder funded projects (funds):" under_funded_projects.each { |project| puts project_and_fund(project) } puts "\nStill needing funds projects (needs):" under_funded_projects.sort.each { |project| puts "#{project.name.ljust(30, '_')}#{project.need}" } puts "\nThe projects have collected a total amouts of $#{total_rewards} from pledges:" @projects.each do |project| puts "\n#{project.name}'s total rewards:" project.each_earned_pledge do |pledge| puts "$#{pledge.amount} total #{pledge.name} amount" end puts "$#{project.rewards} grand total pledges" end end |
#project_and_fund(project) ⇒ Object
82 83 84 |
# File 'lib/crowdfund/fundrequest.rb', line 82 def project_and_fund(project) "#{project.name.ljust(30, '_')}#{project.fund}" end |
#request_funding(weeks) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/crowdfund/fundrequest.rb', line 39 def request_funding(weeks) puts "The current projects are :" @projects.each do |project| puts project end pledges = PledgePool::PLEDGES puts "\nThere are #{pledges.size} possible pledge amounts:" pledges.each { |pledge| puts "A #{pledge.name} pledge is worth $#{pledge.amount}"} 1.upto(weeks) do |week| if block_given? break if yield end puts "\nFund raising week #{week}:" @projects.each do |project| FundingRound.take_fund(project) puts project end end end |
#save_fund_stats(to_file = 'fund_stats.txt') ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/crowdfund/fundrequest.rb', line 26 def save_fund_stats(to_file='fund_stats.txt') File.open(to_file, 'w') do |file| file.puts "#{@name} Croudfund Stats :" @projects.sort.each do |project| file.puts project_and_fund(project) end end end |
#total_rewards ⇒ Object
35 36 37 |
# File 'lib/crowdfund/fundrequest.rb', line 35 def total_rewards @projects.reduce(0) { |sum, project| sum + project.rewards } end |