Class: CrowdFund::FundRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ FundRequest

Returns a new instance of FundRequest.



11
12
13
14
# File 'lib/crowdfund/fundrequest.rb', line 11

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_project(project) ⇒ Object



16
17
18
# File 'lib/crowdfund/fundrequest.rb', line 16

def add_project(project)
  @projects << project
end

#fully_funded_projectsObject



70
71
72
# File 'lib/crowdfund/fundrequest.rb', line 70

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

#load_projects(from_file) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crowdfund/fundrequest.rb', line 58

def load_projects(from_file)
  CSV.foreach(from_file) do |row|
    # funding (3rd attribute) is optional
    if row[2].nil?
      project = Project.new(row[0], row[1].to_i)
    else
      project = Project.new(row[0], row[1].to_i, row[2].to_i)
    end
    add_project(project)
  end
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/crowdfund/fundrequest.rb', line 40

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

  #fully_funded, under_funded = @projects.partition { |project| project.fully_funded? }

  puts "\n#{@name} Statistics:"

  print_fully_funded_projects

  print_under_funded_projects
end

#request_funding(rounds) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/crowdfund/fundrequest.rb', line 20

def request_funding(rounds)
  puts "There are #{@projects.size} projects in #{@name}:"
  @projects.each do |project|
    puts project.name
  end

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

  1.upto(rounds) do |n|
    puts "\nRound #{n}:"
    @projects.each do |project|
      FundingRound.take_turn(project)
    end
  end
end

#save_projects(to_file = "projects.txt") ⇒ Object



78
79
80
81
82
# File 'lib/crowdfund/fundrequest.rb', line 78

def save_projects(to_file="projects.txt")
  File.open(to_file, "w") do |file|
    print_under_funded_projects(file)
  end
end

#under_funded_projectsObject



74
75
76
# File 'lib/crowdfund/fundrequest.rb', line 74

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