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_funds, #remove_funds

Constructor Details

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

Returns a new instance of Project.



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

def initialize(name, funding=0, target)
	@name= name
	@funding= funding
	@target= target
	@pledges_made= Hash.new(0)
end

Instance Attribute Details

#fundingObject (readonly)

Returns the value of attribute funding.



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

def funding
  @funding
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#<=>(other) ⇒ Object



24
25
26
# File 'lib/crowdfund/project.rb', line 24

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

#each_pledge_donatedObject



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

def each_pledge_donated
	@pledges_made.each do |level, amount|
		yield Pledges.new(level, amount)
	end
end

#funded?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/crowdfund/project.rb', line 20

def funded?
	@funding >= @target
end

#funding_neededObject



44
45
46
# File 'lib/crowdfund/project.rb', line 44

def funding_needed
	@target - @funding
end

#fundsObject



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

def funds
	@pledges_made.values.reduce(0, :+)
end

#pledges_made(pledge) ⇒ Object



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

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

#to_sObject



16
17
18
# File 'lib/crowdfund/project.rb', line 16

def to_s
	"Project #{@name} has $#{@funding} in funding towards a goal of $#{@target}, we still need $#{funding_needed} to hit our goal."
end