Class: Pr::RubyProjects
- Inherits:
-
Object
- Object
- Pr::RubyProjects
- Defined in:
- lib/rubyProjects.rb
Instance Method Summary collapse
- #add_project(p) ⇒ Object
- #fully_funded_projects ⇒ Object
-
#initialize(name) ⇒ RubyProjects
constructor
A new instance of RubyProjects.
- #load_project(from_file) ⇒ Object
- #print_name_funds(p) ⇒ Object
- #print_stats ⇒ Object
- #request_funding(rounds) ⇒ Object
- #save_projects_need_funding(to_file = "projects_need_funding.txt") ⇒ Object
- #total_funds_projects ⇒ Object
- #under_funded_projects ⇒ Object
Constructor Details
#initialize(name) ⇒ RubyProjects
Returns a new instance of RubyProjects.
7 8 9 10 |
# File 'lib/rubyProjects.rb', line 7 def initialize(name) @name = name.capitalize @rp = [] end |
Instance Method Details
#add_project(p) ⇒ Object
16 17 18 |
# File 'lib/rubyProjects.rb', line 16 def add_project(p) @rp << p end |
#fully_funded_projects ⇒ Object
42 43 44 |
# File 'lib/rubyProjects.rb', line 42 def fully_funded_projects @rp.select { |project| project.fully_funded? } end |
#load_project(from_file) ⇒ Object
11 12 13 14 15 |
# File 'lib/rubyProjects.rb', line 11 def load_project(from_file) File.readlines(from_file).each do |line| add_project(Pr::Project.from_csv(line)) end end |
#print_name_funds(p) ⇒ Object
39 40 41 |
# File 'lib/rubyProjects.rb', line 39 def print_name_funds(p) puts "#{p.name} ($#{p.total_funds}) target ($#{p.target})" end |
#print_stats ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rubyProjects.rb', line 49 def print_stats puts "\n#{@name}'s projects statistic" puts "\n#{fully_funded_projects.size} full funding projects" fully_funded_projects.each do |p| print_name_funds(p) end puts "\n#{under_funded_projects.size} part funding projects" under_funded_projects.each do |p| print_name_funds(p) end @rp.each do |p| puts "\nProject #{p.name}'s pledges : " p.each_received_pledge do |rp| puts "$#{rp.amount}in #{rp.name} pledges" end puts "$#{p.pledges} in total pledges" end puts"\n#{@name} High Founding Amount:" @rp.sort.each do |p| formatted_name = p.name.ljust(20,'.') puts "#{formatted_name} $#{p.total_funds}" end puts "\nTotal funds for #{@name} projects is $#{total_funds_projects} " end |
#request_funding(rounds) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rubyProjects.rb', line 19 def request_funding(rounds) puts "\n#{@name} have #{@rp.size} projects " puts @rp prizes = Pledge::PRIZES puts "\nThere are #{prizes.size} possible amounts:" prizes.each do |p| puts "A #{p.name} pledge is worth $#{p.amount}" end def total_funds_projects @rp.reduce(0) { |sum,p| sum + p.total_funds} end 1.upto(rounds)do |round| puts"\nRound #{round}:" @rp.each do |p| puts "Target funding amount for #{p.name} is $#{p.target}" FundingRound.take_turn(p) puts p end end end |
#save_projects_need_funding(to_file = "projects_need_funding.txt") ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/rubyProjects.rb', line 73 def save_projects_need_funding(to_file="projects_need_funding.txt") File.open(to_file, "w") do |file| file.puts "These projects still need your help:" under_funded_projects.sort.each do |project| formatted_name = project.name.ljust(20, '.') file.puts "#{formatted_name} $#{project.total_funds} target $#{project.target}" end end end |
#total_funds_projects ⇒ Object
27 28 29 |
# File 'lib/rubyProjects.rb', line 27 def total_funds_projects @rp.reduce(0) { |sum,p| sum + p.total_funds} end |
#under_funded_projects ⇒ Object
46 47 48 |
# File 'lib/rubyProjects.rb', line 46 def under_funded_projects @rp.reject { |project| project.fully_funded? } end |