Class: CrowdFund::FundRequest

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

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ FundRequest

Returns a new instance of FundRequest.



8
9
10
11
# File 'lib/crowdfund/fund_request.rb', line 8

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

Instance Method Details

#add_project(project) ⇒ Object



13
14
15
# File 'lib/crowdfund/fund_request.rb', line 13

def add_project(project)
  @projects.push(project)
end

#fully_funded_projectsObject



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

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

#load_projects(from_file) ⇒ Object



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

def load_projects(from_file)
  CSV.foreach(from_file) do |row|
    name, target, funding = row[0], Integer(row[1]), Integer(row[2])
    project = Project.new(name, target, funding)
    add_project(project)
  end
end


49
50
51
# File 'lib/crowdfund/fund_request.rb', line 49

def print_project_status(project)
  puts project.status
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/crowdfund/fund_request.rb', line 53

def print_stats
  puts "\nFunding Results:"
  puts "\n#{fully_funded_projects.size} Fully-funded Project(s):"
  fully_funded_projects.each do |project|
    print_project_status(project)
  end
  puts "\n#{under_funded_projects.size} Under-funded Project(s):"
  under_funded_projects.each do |project|
    print_project_status(project)
  end
  
  puts "\nProjects still needing funding:"
  under_funded_projects.sort.each do |project|
    puts "#{project.name.ljust(20, '.')} Funding: $#{project.funding}, Target: $#{project.target}"
  end
  
  puts "\nTotal Funds Outstanding:"
  puts @projects.reduce(0) { |sum, project| sum + project.outstanding_funding }
end

#process_funding(rounds) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/crowdfund/fund_request.rb', line 25

def process_funding(rounds)
  1.upto(rounds) do |round|
    puts "\nProcessing round #{round} of funding for #{@title}."
    @projects.each do |project|
      FundingRound.fund_project(project)
      puts project
    end
  end
end

#save_project_funding_statusObject



43
44
45
46
47
# File 'lib/crowdfund/fund_request.rb', line 43

def save_project_funding_status
  File.open("project_status.txt", "wb") do |file|
    @projects.each { |project| file.puts project.status }
  end
end

#under_funded_projectsObject



35
36
37
# File 'lib/crowdfund/fund_request.rb', line 35

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