Class: CrowdFund::Project
- Inherits:
-
Object
- Object
- CrowdFund::Project
- Includes:
- Fundable
- Defined in:
- lib/crowdfund/project.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#funding ⇒ Object
Returns the value of attribute funding.
-
#name ⇒ Object
Returns the value of attribute name.
-
#target_funding ⇒ Object
readonly
Returns the value of attribute target_funding.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #each_received_pledge ⇒ Object
-
#initialize(name, target_funding, funding = 0) ⇒ Project
constructor
A new instance of Project.
- #received_pledge(pledge) ⇒ Object
- #to_s ⇒ Object
- #total_pledges ⇒ Object
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
#funding ⇒ Object
Returns the value of attribute funding.
8 9 10 |
# File 'lib/crowdfund/project.rb', line 8 def funding @funding end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/crowdfund/project.rb', line 7 def name @name end |
#target_funding ⇒ Object (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_pledge ⇒ Object
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_s ⇒ Object
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_pledges ⇒ Object
39 40 41 |
# File 'lib/crowdfund/project.rb', line 39 def total_pledges @pledges_received.values.reduce(0, :+) end |