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.
-
#goal ⇒ Object
readonly
Returns the value of attribute goal.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pledges ⇒ Object
readonly
Returns the value of attribute pledges.
Instance Method Summary collapse
- #<=>(other_project) ⇒ Object
- #each_pledge ⇒ Object
-
#initialize(name, goal, funding = 0) ⇒ Project
constructor
A new instance of Project.
- #receive_pledge(match_factor = 1) ⇒ Object
- #to_s ⇒ Object
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
#funding ⇒ Object
Returns the value of attribute funding.
9 10 11 |
# File 'lib/crowdfund/project.rb', line 9 def funding @funding end |
#goal ⇒ Object (readonly)
Returns the value of attribute goal.
8 9 10 |
# File 'lib/crowdfund/project.rb', line 8 def goal @goal end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/crowdfund/project.rb', line 9 def name @name end |
#pledges ⇒ Object (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_pledge ⇒ Object
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_s ⇒ Object
36 37 38 |
# File 'lib/crowdfund/project.rb', line 36 def to_s "#{@name} has $#{@funding} in funding towards a goal of $#{@goal}." end |