tlc_ps_crowdfund 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a406c2852a27d3897874565eaa2d3833db2d7ca
4
+ data.tar.gz: 740b3f68e2e60f006f3947b524c68363085784da
5
+ SHA512:
6
+ metadata.gz: e836e4a9a21b929d3049b0383c9ad0399c75c4ad80c9b653e183a39e0d6b38678abe920f0b9b1f709d1ae2a5d871de046a3479935961dfef3fab8530e6541318
7
+ data.tar.gz: e08fc8bdddf8834aaba1788522b6f3a8fe54af20148b411619ba80228f73906d141c8db373818625306b2fac4da755d0ddc557576765b8b59c4d3a1d268727bc
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2014 Travis Lee Cuzick
2
+
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1 @@
1
+ This is a fundraising app developed as part of the Pragmatic Studio's course on the Ruby programming language.
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ chmod +x bin/crowdfund
3
+
4
+ require_relative '../lib/fundraising_project/project'
5
+ require_relative '../lib/fundraising_project/fundrequest'
6
+ require_relative '../lib/fundraising_project/matched_funds_project'
7
+ require_relative '../lib/fundraising_project/grant_project'
8
+ project1 = CrowdFund::GrantProject.new("ABC", 100, 1000)
9
+ project2 = CrowdFund::MatchedFundsProject.new("LMN", 500, 3000)
10
+ project3 = CrowdFund::Project.new("XYZ", 25, 75)
11
+ project_queue1 = CrowdFund::FundRequest.new("Q1 Projects")
12
+ project_queue1.add_project(project1)
13
+ project_queue1.add_project(project2)
14
+ project_queue1.add_project(project3)
15
+
16
+ default_project_file = File.join(File.dirname(__FILE__), 'projects.csv')
17
+ project_queue1.load_projects(ARGV.shift || default_project_file)
18
+
19
+ loop do
20
+ puts "\nHow many funding rounds? ('quit' to exit)"
21
+ rounds = gets.chomp.downcase
22
+
23
+ case rounds
24
+ when /^\d+$/
25
+ rounds = rounds.to_i
26
+ puts "\nHow much funding should be added or removed?"
27
+ funds = gets.chomp
28
+ case funds
29
+ when /^\d+$/
30
+ funds = funds.to_i
31
+ project_queue1.alter_funding(funds, rounds)
32
+ when nil
33
+ project_queue1.alter_funding(rounds)
34
+ else
35
+ puts "Please enter a valid number"
36
+ end
37
+ when 'quit', 'exit'
38
+ project_queue1.print_stats
39
+ break
40
+ else
41
+ puts "Please enter a valid number of rounds or 'quit'"
42
+ end
43
+ end
44
+
45
+ project_queue1.print_funding_summary("to_csv")
@@ -0,0 +1,3 @@
1
+ Project1,0,100
2
+ Project2,50,500
3
+ Project3,100,1000
@@ -0,0 +1,13 @@
1
+ module CrowdFund
2
+ class Die
3
+ attr_reader :number
4
+
5
+ def initialize
6
+ @number = roll
7
+ end
8
+
9
+ def roll
10
+ rand(1..6)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module CrowdFund
2
+ module Fundable
3
+ def add_funds(amount=25)
4
+ self.current_funding_amount += amount
5
+ self.current_funding_amount = self.current_funding_amount.to_i
6
+ puts "Project #{self.name} got more funds!"
7
+ end
8
+
9
+ def remove_funds(amount=15)
10
+ self.current_funding_amount -= amount
11
+ puts "Project #{self.name} lost some funds!"
12
+ end
13
+
14
+ def fully_funded?
15
+ self.total_funds >= self.target_funding_amount
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'project'
2
+ require_relative 'die'
3
+ require_relative 'pledge_pool'
4
+
5
+ module CrowdFund
6
+ module FundingRound
7
+ def self.randomize_funds (project, amount)
8
+ die = Die.new()
9
+ if die.roll % 2 == 0 || die.roll % 3 == 0
10
+ project.add_funds(amount)
11
+ else
12
+ project.remove_funds(amount)
13
+ end
14
+ end
15
+ def self.random_pledge(project)
16
+ pledge = PledgePool::PLEDGES.sample
17
+ project.pledges[pledge.name] += pledge.amount
18
+ puts "Project #{project.name} received a #{pledge.name} pledge worth $#{pledge.amount}."
19
+ puts "Project #{project.name}'s pledges: #{project.pledges}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,79 @@
1
+ require_relative 'project'
2
+ require_relative 'die'
3
+ require_relative 'funding_round'
4
+ require 'csv'
5
+
6
+ module CrowdFund
7
+ class FundRequest
8
+ def initialize(project_name)
9
+ @project_name = project_name
10
+ @projects = []
11
+ end
12
+ def add_project(project)
13
+ @projects << project
14
+ end
15
+ def load_projects(from_file)
16
+ CSV.foreach(from_file) do |row|
17
+ add_project(Project.new(row[0], row[1].to_i, row[2].to_i))
18
+ end
19
+ end
20
+ def save_projects(to_file)
21
+ File.open(to_file, "w") do |file|
22
+ file.puts
23
+ end
24
+ end
25
+ def print_funds_needs(project)
26
+
27
+ end
28
+ def print_funding_summary(method, to_file="funding_needed.csv")
29
+ funded, not_funded = @projects.partition { |project| project.fully_funded?}
30
+ messages = [
31
+ "\n",
32
+ "#{funded.size} projects are fully funded.\n",
33
+ "\n#{not_funded.size} projects are under-funded.\n",
34
+ "\nProjects needing more funds:\n"
35
+ ]
36
+ not_funded.sort { |a, b| b.funding_needed <=> a.funding_needed}.each do |project|
37
+ messages.push("Project #{project.name} ($#{project.funding_needed} still needed)")
38
+ end
39
+ if method == "to_csv"
40
+ File.open(to_file, "w") do |file|
41
+ messages.shift
42
+ file.puts messages
43
+ end
44
+ else
45
+ puts messages
46
+ end
47
+ end
48
+ def print_stats
49
+ print_funding_summary("to_screen")
50
+ @projects.each do |project|
51
+ puts "\nProject #{project.name}'s pledges:"
52
+ project.each_pledge do |pledge|
53
+ puts "$#{pledge.amount} in #{pledge.name} pledges"
54
+ end
55
+ puts "$#{project.total_pledges} in total pledges"
56
+ end
57
+ end
58
+
59
+ def alter_funding(amount=25, rounds)
60
+ puts "There are #{@projects.size} projects in the queue: "
61
+ @projects.each do |project|
62
+ puts project
63
+ end
64
+ puts "There are 3 possible pledge amounts:"
65
+ pledges = PledgePool::PLEDGES
66
+ pledges.each do |pledge|
67
+ puts "A #{pledge.name} pledge is worth $#{pledge.amount}."
68
+ end
69
+ 1.upto(rounds) do |n|
70
+ puts "Round #{n}:\n"
71
+ @projects.each do |project|
72
+ FundingRound.randomize_funds(project, amount)
73
+ FundingRound.random_pledge(project)
74
+ puts project
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'project'
2
+
3
+ module CrowdFund
4
+ class GrantProject < Project
5
+ def remove_funds(amount)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'project'
2
+
3
+ module CrowdFund
4
+ class MatchedFundsProject < Project
5
+ def half_funded?
6
+ (@target_funding_amount - total_funds) <= total_funds
7
+ end
8
+
9
+ def add_funds(amount=25)
10
+ amount *= 2 if half_funded?
11
+ super(amount)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module CrowdFund
2
+ Pledge = Struct.new(:name, :amount)
3
+ module PledgePool
4
+ PLEDGES = [
5
+ Pledge.new(:silver, 75),
6
+ Pledge.new(:gold, 100),
7
+ Pledge.new(:bronze, 50)
8
+ ]
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "pledge_pool"
2
+ require_relative 'fundable'
3
+
4
+ module CrowdFund
5
+ class Project
6
+ include Fundable
7
+
8
+ attr_reader :target_funding_amount, :pledges
9
+ attr_accessor :name, :current_funding_amount
10
+
11
+ def initialize(name, current_funding_amount=0, target_funding_amount=1000)
12
+ @name = name
13
+ @current_funding_amount = current_funding_amount
14
+ @target_funding_amount = target_funding_amount
15
+ @pledges = Hash.new(0)
16
+ end
17
+
18
+ def funding_needed
19
+ @target_funding_amount - total_funds
20
+ end
21
+
22
+ def total_pledges
23
+ @pledges.values.reduce(0) {|sum, value| sum + value}
24
+ end
25
+
26
+ def total_funds
27
+ @current_funding_amount + total_pledges
28
+ end
29
+
30
+ def to_s
31
+ "Project #{@name} has $#{total_funds} in funding towards a goal of $#{@target_funding_amount}."
32
+ end
33
+
34
+ def each_pledge
35
+ @pledges.each do |name, amount|
36
+ yield(Pledge.new(name, amount))
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ require 'fundraising_project/fundrequest'
2
+
3
+ module CrowdFund
4
+ describe FundRequest do
5
+
6
+ before do
7
+ @request = FundRequest.new("BBN")
8
+ @initial_funding = 1000
9
+ @target_funding = 10000
10
+ @project = Project.new("RuppArena", @initial_funding, @target_funding)
11
+ @request.add_project(@project)
12
+ end
13
+
14
+ it "loses funding when a number not divisible by 2 or 3 is rolled" do
15
+ allow_any_instance_of(Die).to receive(:roll).and_return(5)
16
+ delta = 25
17
+ @request.alter_funding(delta, 2)
18
+ expect(@project.current_funding_amount).to eq(@initial_funding - (delta * 2))
19
+ end
20
+
21
+ it "gains funding when an even number is rolled" do
22
+ allow_any_instance_of(Die).to receive(:roll).and_return(4)
23
+ delta = 25
24
+ @request.alter_funding(delta, 1)
25
+ expect(@project.current_funding_amount).to eq((@initial_funding + (delta)).to_i)
26
+ end
27
+
28
+ it "gains funding when a number divisible by 3 is rolled" do
29
+ allow_any_instance_of(Die).to receive(:roll).and_return(3)
30
+ delta = 25
31
+ @request.alter_funding(delta, 1)
32
+ expect(@project.current_funding_amount).to eq((@initial_funding + (delta)).to_i)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,64 @@
1
+ require 'fundraising_project/project'
2
+
3
+ module CrowdFund
4
+ describe Project do
5
+
6
+ before do
7
+ @project = Project.new("my_project")
8
+ @current_funding_amount = @project.current_funding_amount
9
+ end
10
+
11
+ before do
12
+ $stdout = StringIO.new
13
+ end
14
+
15
+ it "should have an initial target funding amount" do
16
+ expect(@project.target_funding_amount).not_to be_nil
17
+ end
18
+
19
+ it 'should have "funding needed"" equal to "target funding" - "initial funding"' do
20
+ expect(@project.funding_needed).to eq(@project.target_funding_amount - @project.current_funding_amount)
21
+ end
22
+
23
+ it "should increase funds by 25 when funds are added" do
24
+ @project.add_funds
25
+ expect(@project.current_funding_amount).to eq(@current_funding_amount + 25)
26
+ end
27
+
28
+ it "should decrease funds by 15 when funds are removed" do
29
+ @project.remove_funds
30
+ expect(@project.current_funding_amount).to eq(@current_funding_amount - 15)
31
+ end
32
+
33
+ it "should have a default value of 0 for funding amount" do
34
+ expect(@project.current_funding_amount).to eq(0)
35
+ end
36
+
37
+ context "with excess funds" do
38
+ before do
39
+ @project = Project.new("test_project", 1111, 1000)
40
+ end
41
+ it "should have 'fully funded' equal to true" do
42
+ expect(@project).to be_fully_funded
43
+ end
44
+ end
45
+
46
+ context "with exactly the target amount of funds" do
47
+ before do
48
+ @project = Project.new("test_project", 1000, 1000)
49
+ end
50
+ it "should have 'fully funded' equal to true" do
51
+ expect(@project).to be_fully_funded
52
+ end
53
+ end
54
+
55
+ context "with less than the target amount of funds" do
56
+ before do
57
+ @project = Project.new("test_project", 800, 1000)
58
+ end
59
+ it "should have 'fully funded' equal to false" do
60
+ expect(@project).not_to be_fully_funded
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tlc_ps_crowdfund
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Travis Lee Cuzick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This is a fundraising app developed as part of the Pragmatic Studio's
28
+ course on the Ruby programming language.
29
+ email: tlcuzick@gmail.com
30
+ executables:
31
+ - crowdfund
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README
37
+ - bin/crowdfund
38
+ - bin/projects.csv
39
+ - lib/fundraising_project/die.rb
40
+ - lib/fundraising_project/fundable.rb
41
+ - lib/fundraising_project/funding_round.rb
42
+ - lib/fundraising_project/fundrequest.rb
43
+ - lib/fundraising_project/grant_project.rb
44
+ - lib/fundraising_project/matched_funds_project.rb
45
+ - lib/fundraising_project/pledge_pool.rb
46
+ - lib/fundraising_project/project.rb
47
+ - spec/fundraising_project/fundrequest_spec.rb
48
+ - spec/fundraising_project/project_spec.rb
49
+ homepage: http://www.pragmaticstudio.com
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.2.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: This is a simple fundraising app developed for the Pragmatic Studio Ruby
73
+ course.
74
+ test_files:
75
+ - spec/fundraising_project/fundrequest_spec.rb
76
+ - spec/fundraising_project/project_spec.rb