Class: Crowdfund::Project

Inherits:
Object
  • Object
show all
Includes:
Fundable
Defined in:
lib/crowdfund/project.rb

Direct Known Subclasses

GrantProject, MatchProject

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fundable

#add_funds, #fully_funded?, #funds_needed, #lose_funds

Constructor Details

#initialize(name, goal, funding = 0) ⇒ Project

Returns a new instance of Project.



11
12
13
14
15
16
# File 'lib/crowdfund/project.rb', line 11

def initialize (name, goal, funding=0)
  @name = name
  @goal = goal
  @funding = funding
  @pledges = Hash.new(0)
end

Instance Attribute Details

#fundingObject

Returns the value of attribute funding.



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

def funding
  @funding
end

#goalObject (readonly)

Returns the value of attribute goal.



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

def goal
  @goal
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#pledgesObject (readonly)

Returns the value of attribute pledges.



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

def pledges
  @pledges
end

Instance Method Details

#<=>(other_project) ⇒ Object



18
19
20
# File 'lib/crowdfund/project.rb', line 18

def <=>(other_project)
  other_project.funding <=> funding
end

#each_pledgeObject



32
33
34
# File 'lib/crowdfund/project.rb', line 32

def each_pledge
  @pledges.sort_by { |name, amount| amount }.each { |name, amount| yield Pledge.new(name, amount) }
end

#receive_pledge(match_factor = 1) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/crowdfund/project.rb', line 23

def receive_pledge(match_factor=1)
  pledge = PledgePool.random
  puts "#{name} received a #{pledge.name} pledge worth $#{pledge.amount}."
  @pledges[pledge.name] += pledge.amount * match_factor
  @funding += pledge.amount * match_factor
  puts "#{name}'s pledge amounts:"
  each_pledge { |pledge| puts "$#{pledge.amount} in #{pledge.name} pledges" }
end

#to_sObject



36
37
38
# File 'lib/crowdfund/project.rb', line 36

def to_s
  "#{@name} has $#{@funding} in funding towards a goal of $#{@goal}."
end