Class: Crowdfund::Project

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, amount = 0, target_goal = 10000) ⇒ Project

Returns a new instance of Project.



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

def initialize(name, amount=0, target_goal=10000)
    @name = name
    @amount = amount.abs() #no negative initial amount
    @target_goal = target_goal.abs() #no negative target value
    puts "New project '#{@name}' ($#{@amount}) is initialized. Target goal is #{@target_goal}."
    @pledges_received = Hash.new(0)
end

Instance Attribute Details

#amountObject

you can now write to amount for project to update it



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

def amount
  @amount
end

#nameObject

you can now write to amount for project to update it



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

def name
  @name
end

#target_goalObject (readonly)

Returns the value of attribute target_goal.



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

def target_goal
  @target_goal
end

Instance Method Details

#defund(value = 0) ⇒ Object

puts “‘#@name’ now has $##total_amount in funding.” #NOTE: ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount)



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

def defund(value=0)
    @amount -= value
    puts "'#{@name}' has lost $#{value} in funding!"
    #puts "'#{@name}' now has $#{total_amount} in funding." #NOTE:  ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount)
end

#describeObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/crowdfund/project.rb', line 56

def describe
    @current_time = Time.new.strftime("%-I:%M %p %-m/%-d/%-y")
        if total_amount > @target_goal #NOTE:  ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount)
        puts "\nProject #{@name}: $#{total_amount}/#{@target_goal} (over goal) as of #{@current_time}."
        elsif total_amount < @target_goal
            puts "\nProject #{@name}: $#{total_amount}/#{@target_goal} (under goal) as of #{@current_time}."
        else
            puts "\nProject #{@name}: $#{total_amount}/#{@target_goal} (at goal) as of #{@current_time}."
    end
end

#each_pledge_receivedObject



67
68
69
70
71
# File 'lib/crowdfund/project.rb', line 67

def each_pledge_received
    @pledges_received.each do |name, amount| # (@pledges_received is the hash)
    yield Pledge.new(name, amount)
    end
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
    total_amount <= 0
end

#fund(value = 0) ⇒ Object



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

def fund(value=0)
    @amount += value
    puts "'#{@name}' received $#{value} in funding!"
    #puts "'#{@name}' now has $#{total_amount} in funding." #NOTE:  ALWAYS CALL TOTAL_AMOUNT (sum of all pledges + @amount)
end

#pledge_received(pledge) ⇒ Object



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

def pledge_received(pledge)
    @pledges_received[pledge.name] += pledge.amount #was missing the "s" on @pledges_received which is for the hash
    puts "#{@name} received a #{pledge.name} pledge worth $#{pledge.amount}."
    puts "#{@name}'s pledge amounts: #{@pledges_received}"
end

#statusObject



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

def status
    "(No funds!)" if empty?
end

#target_goal_met?Boolean

Returns:

  • (Boolean)


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

def target_goal_met?
    total_amount >= @target_goal
end

#to_sObject

defines what happens when you use puts on an object of class “Song”



48
49
50
51
52
53
54
# File 'lib/crowdfund/project.rb', line 48

def to_s #defines what happens when you use puts on an object of class "Song"
    if empty?
    "#{@name} ($#{total_amount}) #{status}"
    else
    "#{@name} ($#{total_amount})"
    end
end

#total_amountObject



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

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