Class: CrowdFund::Project

Inherits:
Object
  • Object
show all
Includes:
Fundable, Printable
Defined in:
lib/crowd_fund/project.rb

Direct Known Subclasses

GrandProject, SecureProject

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Printable

#to_s

Methods included from Fundable

#<=>, #add, #full?, #remove, #stats, #total_to_fund

Constructor Details

#initialize(name, amount = 0, target = 1000) ⇒ Project

Returns a new instance of Project.



13
14
15
16
17
18
# File 'lib/crowd_fund/project.rb', line 13

def initialize(name, amount=0, target=1000)
  @name = name.upcase
  @amount = amount
  @target = target
  @pledges_received = Hash.new(0)
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



11
12
13
# File 'lib/crowd_fund/project.rb', line 11

def amount
  @amount
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/crowd_fund/project.rb', line 11

def name
  @name
end

#targetObject

Returns the value of attribute target.



11
12
13
# File 'lib/crowd_fund/project.rb', line 11

def target
  @target
end

Instance Method Details

#each_pledge_receivedObject



24
25
26
27
28
# File 'lib/crowd_fund/project.rb', line 24

def each_pledge_received
  @pledges_received.each do |name, value|
    yield Pledge.new(name, value)
  end
end

#receive_pledge(pledge) ⇒ Object



30
31
32
33
34
35
# File 'lib/crowd_fund/project.rb', line 30

def receive_pledge(pledge)
  @pledges_received[pledge.name] += pledge.value
  @amount += total_pledges
  puts "#{@name} received a #{pledge.name} pledge worth $#{pledge.value}."
  puts "Project #{@name} pledges: #{@pledges_received}"
end

#total_pledgesObject



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

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