Class: CrowdFund::Project

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

Direct Known Subclasses

GrantProject, MatchingProject

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fundable

#add_fund, #fully_funded?, #funding_needed, #remove_fund

Constructor Details

#initialize(name, target_funding, 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, target_funding, funding=0)
  @name = name
  @target_funding = target_funding
  @funding = funding
  @pledges_received = Hash.new(0)
end

Instance Attribute Details

#fundingObject

Returns the value of attribute funding.



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

def funding
  @funding
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#target_fundingObject (readonly)

Returns the value of attribute target_funding.



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

def target_funding
  @target_funding
end

Instance Method Details

#<=>(other) ⇒ Object



22
23
24
# File 'lib/crowdfund/project.rb', line 22

def <=>(other)
  other.funding_needed <=> funding_needed
end

#each_received_pledgeObject



33
34
35
36
37
# File 'lib/crowdfund/project.rb', line 33

def each_received_pledge
  @pledges_received.each do |name, amount|
    yield Pledge.new(name, amount)
  end
end

#received_pledge(pledge) ⇒ Object



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

def received_pledge(pledge)
  @pledges_received[pledge.name] += pledge.amount
  @funding += pledge.amount
  puts "#{@name} received a #{pledge.name} pledge worth $#{pledge.amount}."
  puts "#{@name}'s pledges: #{@pledges_received}"
end

#to_sObject



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

def to_s
  "#{@name} has $#{@funding} in funding towards a goal of $#{@target_funding}, i.e. $#{funding_needed} is still needed."
end

#total_pledgesObject



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

def total_pledges
  @pledges_received.values.reduce(0, :+)
end