Class: Crowdfund::Collection
- Inherits:
-
Object
- Object
- Crowdfund::Collection
- Defined in:
- lib/crowdfund/collection.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_project(project) ⇒ Object
-
#initialize(name) ⇒ Collection
constructor
this method converts a Collection.new specified title to the correct format when created.
- #load_projects(from_file) ⇒ Object
- #print_stats ⇒ Object
-
#run_projects(rounds = 1) ⇒ Object
play one round by default.
- #save_output(to_file = "crowdfund_output.txt") ⇒ Object
- #show_pledges ⇒ Object
Constructor Details
#initialize(name) ⇒ Collection
this method converts a Collection.new specified title to the correct format when created
8 9 10 11 12 |
# File 'lib/crowdfund/collection.rb', line 8 def initialize(name) #this method converts a Collection.new specified title to the correct format when created @name = name.upcase #upcased all collection titles for now @collection = [] puts "Collection '#{@name}' was created." end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
7 8 9 |
# File 'lib/crowdfund/collection.rb', line 7 def collection @collection end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/crowdfund/collection.rb', line 7 def name @name end |
Instance Method Details
#add_project(project) ⇒ Object
14 15 16 |
# File 'lib/crowdfund/collection.rb', line 14 def add_project(project) @collection << project end |
#load_projects(from_file) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/crowdfund/collection.rb', line 73 def load_projects(from_file) CSV.foreach(from_file, 'r:bom|utf-8') do |row| #this conditional gets tricky! unless row[1].to_i == 0 || row[2].to_i == 0 #unless one of the rows has a nil value... project = Project.new(row[0], row[1].to_i, row[2].to_i) #.. then input all values regularly. else if row[1].to_i == 0 # if amount (second column) is nil >> 0 in csv, project = Project.new(row[0], 0, row[2].to_i) # the default gets set to 0. THIS is where default gets assigned. end if row[2].to_i == 0 # if target_goal (third column) is nil >> 0 in csv, project = Project.new(row[0], row[1].to_i, 10000) # the default target gets set to 10000. THIS is where default gets assigned. end end add_project(project) end end |
#print_stats ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/crowdfund/collection.rb', line 25 def print_stats puts "\n#{@name}" @sorted_list = @collection.sort { |a, b| b.amount <=> a.amount } @sorted_list.each do |p| p.describe puts "Total pledge tier donations for #{p.name}:" p.each_pledge_received do |pledge| puts "#{pledge.name}: $#{pledge.amount}" end puts "Other donations: $#{p.amount}" end met_goal, under_goal = @sorted_list.partition { |project| project.total_amount >= project.target_goal } unless met_goal.empty? puts "\nProjects at/over goal:" met_goal.each do |project| puts "#{project.name}: $#{project.total_amount}/#{project.target_goal}" #NOTE: ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount) end end unless under_goal.empty? puts "\nProjects under goal:" under_goal.each do |project| puts "#{project.name}: $#{project.total_amount}/#{project.target_goal}" #NOTE: ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount) end end end |
#run_projects(rounds = 1) ⇒ Object
play one round by default
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/crowdfund/collection.rb', line 53 def run_projects(rounds=1) #play one round by default puts "\nThere are currently #{@collection.size} projects:" #no sorting called here at first @collection.each do |project| puts "#{@collection.index(project) + 1}) #{project.name}" end show_pledges 1.upto(rounds) do |round| puts "\nRound #{round}:" @collection.each do |p| puts "Start: #{p}" CollectionTurn.take_turn(p) puts "End: #{p}" end # after all rounds, this describes each project's funding (similar to print stats method for the playlist project): end end |
#save_output(to_file = "crowdfund_output.txt") ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/crowdfund/collection.rb', line 88 def save_output(to_file="crowdfund_output.txt") File.open(to_file, "w") do |file| file.puts Time.new.strftime("File updated on %m/%d/%Y at %I:%M %p") file.puts "#{@name}" file.puts "\nCrowdfund Output:" @collection.each do |project| if project.total_amount > project.target_goal file.puts "\nProject #{project.name}: $#{project.total_amount}/#{project.target_goal} (over goal)" elsif project.total_amount == project.target_goal file.puts "\nProject #{project.name}: $#{project.total_amount}/#{project.target_goal} (at goal)" else file.puts "\nProject #{project.name}: $#{project.total_amount}/#{project.target_goal} (under goal)" end file.puts "Total pledge tier donations for #{project.name}:" project.each_pledge_received do |pledge| file.puts "#{pledge.name}: $#{pledge.amount}" end file.puts "Other donations: $#{project.amount}" end met_goal, under_goal = @collection.partition { |project| project.total_amount >= project.target_goal } unless met_goal.empty? file.puts "\nProjects at/over goal:" met_goal.each do |project| file.puts "#{project.name}: $#{project.total_amount}/#{project.target_goal}" #NOTE: ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount) end end unless under_goal.empty? file.puts "\nProjects under goal:" under_goal.each do |project| file.puts "#{project.name}: $#{project.total_amount}/#{project.target_goal}" #NOTE: ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount) end end end end |
#show_pledges ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/crowdfund/collection.rb', line 17 def show_pledges pledges = PledgePool::PLEDGES puts "\nThere are #{pledges.size} pledge tiers:" pledges.each do |p| puts "#{p.name} tier is a donation of $#{p.amount}." end end |