project_ruby_exemplu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c81370a308027920d751c8bfb952570729fc2d6d
4
+ data.tar.gz: ffdf2a4df6017adaeeeb1756116924acfa4a6b4f
5
+ SHA512:
6
+ metadata.gz: 1cd9b0fe24b37cc72a9e5b2a22b27190f27ac9865ddd71b76e0a850569a00f635838343a7a3604890f1e3dfde49fe06763403b6307db97b8ee7f7ee339d81828
7
+ data.tar.gz: 2f864a622893470ec75da9470595c630abc407b093d4d9faf7d87f3515b53da016c6b930997966bcbb31e6de65c86c961ef98d28cf1e42e965be32d7a865a78a
data/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ Copyright (c) 2013 Nicolae Turean
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software .
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
12
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
15
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,4 @@
1
+ This is an example of application for learning ruby used in The Pragmatic Studio's
2
+ Ruby Programming course .
3
+
4
+ This code is Copyright 2013 Nicolae Turean. See the LICENSE file.
data/bin/crowdfund ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/rubyProjects'
3
+ require_relative '../lib/grand_project'
4
+ require_relative '../lib/matching_project'
5
+
6
+ # project1 = Project.new("turean")
7
+ # project2 = Project.new("mario",25,75)
8
+ # project3 = Project.new("nabuco",100,1000)
9
+ # project4 = Project.new("alba",125,750)
10
+ # project5 = Project.new("Project TBD",200, 1000)
11
+
12
+ romania = Pr::RubyProjects.new("romania")
13
+ default_project_file = File.join(File.dirname(__FILE__), 'projects.csv')
14
+ romania.load_project(ARGV.shift || default_project_file)
15
+
16
+ grand = GrandProject.new("stong",100,1000)
17
+ romania.add_project(grand)
18
+
19
+ match1 = MatchingProject.new("Match Me", 200, 25)
20
+ romania.add_project(match1)
21
+
22
+ loop do
23
+ puts "\nHow many requests ? ('quit' to exit)"
24
+ answer = gets.chomp.downcase
25
+ case answer
26
+ when /^\d+$/
27
+ romania.request_funding(answer.to_i)
28
+ when 'quit', 'exit'
29
+ romania.print_stats
30
+ break
31
+ else
32
+ puts "Please enter a number or 'quit'"
33
+ end
34
+ end
35
+
36
+ romania.save_projects_need_funding
37
+
38
+
39
+ # italia = RubyProjects.new("italia")
40
+ # italia.add_project(project3)
41
+ # italia.add_project(project4)
42
+ # italia.add_project(project5)
43
+ # italia.request_funding(2)
44
+ # italia.print_stats
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
data/bin/projects.csv ADDED
@@ -0,0 +1,3 @@
1
+ Project1,0,100
2
+ Project2,50,500
3
+ Project3,100,1000
@@ -0,0 +1,4 @@
1
+ These projects still need your help:
2
+ Project3............ $425 target $1000
3
+ Stong............... $350 target $1000
4
+ Project2............ $255 target $500
@@ -0,0 +1,14 @@
1
+ require_relative 'fundable'
2
+ class CollegeAccount
3
+ include Fundable
4
+
5
+ attr_accessor :amount, :target
6
+ attr_accessor :name
7
+
8
+ def initialize(name,amount=0,target=3000)
9
+ @name = name.capitalize
10
+ @amount = amount
11
+ @target = target
12
+
13
+ end
14
+ end
data/lib/die.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Pr
2
+ class Die
3
+ attr_reader :number
4
+ def initialize
5
+ roll
6
+ end
7
+ def roll
8
+ @number = rand(1..6)
9
+ end
10
+ end
11
+ end
data/lib/fundable.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Fundable
2
+ def lost
3
+ @amount -= 15
4
+ puts "Project #{@name} lost some founds !"
5
+ end
6
+ def got
7
+ @amount += 25
8
+ puts "Project #{@name} lost some founds !"
9
+ end
10
+ def total_funding_outstanding
11
+ @target - total_funds
12
+ end
13
+ def fully_funded?
14
+ total_funding_outstanding <= 0
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'die'
2
+ require_relative 'pledge'
3
+ module FundingRound
4
+ def self.take_turn(p)
5
+ die = Pr::Die.new
6
+ number_rolled = die.roll
7
+ case number_rolled
8
+ when 1..2
9
+ p.lost
10
+ when 3..4
11
+ puts "#{p.name} project was skipped"
12
+ else
13
+ p.got
14
+ end
15
+ prize = Pr::Pledge.random
16
+ p.received_pledge(prize)
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'project'
2
+
3
+ class GrandProject < Pr::Project
4
+ def lost
5
+ @amount -= 0
6
+ puts "#{@name} has not lost or gained any new funds."
7
+ end
8
+ end
9
+
10
+ if __FILE__ == $0
11
+ grant = GrantProject.new("Project 123", 500, 100)
12
+
13
+ puts "#{grant.name} has $#{grant.total_funds} in funding towards a goal of $#{grant.target}."
14
+ grant.remove_funds
15
+ puts "#{grant.name} has $#{grant.total_funds} in funding towards a goal of $#{grant.target}."
16
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'project'
2
+
3
+ class MatchingProject < Pr::Project
4
+
5
+ def initialize(name, target_funding_amount, funding=0)
6
+ super(name, target_funding_amount, funding)
7
+ @halfway_funded = target_funding_amount / 2
8
+ end
9
+
10
+ def halfway_funded?
11
+ @halfway_funded <= total_funds
12
+ end
13
+
14
+ def add_funds
15
+ if halfway_funded?
16
+ @amount += (25*2)
17
+ puts "#{@name} has received at least half its funding!" if halfway_funded?
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ if __FILE__ == $0
26
+ matchingproject = MatchingProject.new("Matching 123", 0, 100)
27
+ 3.times { matchingproject.add_funds }
28
+ puts matchingproject.amount
29
+ end
30
+
data/lib/pledge.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Pr
2
+ Prize = Struct.new(:name, :amount)
3
+ module Pledge
4
+ PRIZES = [
5
+ Prize.new(:bronze, 50),
6
+ Prize.new(:silver, 75),
7
+ Prize.new(:gold, 100)
8
+ ]
9
+ def self.random
10
+ PRIZES.sample
11
+ end
12
+ end
13
+ end
data/lib/project.rb ADDED
@@ -0,0 +1,59 @@
1
+ require_relative 'pledge'
2
+ require_relative 'fundable'
3
+ module Pr
4
+ class Project
5
+ include Fundable
6
+ attr_accessor :amount, :target
7
+ attr_accessor :name
8
+
9
+ def initialize(name,funding_a=0,funding_t=300)
10
+ @name = name.capitalize
11
+ @amount = funding_a
12
+ @target = funding_t
13
+ @received_pledge = Hash.new(0)
14
+ end
15
+ def self.from_csv(line)
16
+ name, amount,target = line.split(',')
17
+ project = Project.new(name, Integer(amount), Integer(target))
18
+ end
19
+ def time
20
+ current_time = Time.new
21
+ current_time.strftime("%A, %B %d, %Y")
22
+ end
23
+ def <=>(other)
24
+ other.amount <=> @amount
25
+ end
26
+ def to_s
27
+ "Project #{@name} has #{total_funds}$ in funding as of #{time} , towards a goal of $#{@target}
28
+ Total funding still needed is : $#{total_funding_outstanding}
29
+ ------------------------"
30
+ end
31
+
32
+ def received_pledge(pledge)
33
+ @received_pledge[pledge.name] += pledge.amount
34
+ puts "#{@name} received a #{pledge.name} pledge worth $#{pledge.amount}."
35
+ puts "#{@name}'s pledges: #{@received_pledge}"
36
+ end
37
+
38
+ def pledges
39
+ @received_pledge.values.reduce(0, :+)
40
+ end
41
+
42
+ def total_funds
43
+ @amount + pledges
44
+ end
45
+
46
+ def each_received_pledge
47
+ @received_pledge.each do |name, funds|
48
+ pledge = Prize.new(name, funds)
49
+ yield pledge
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ if __FILE__ == $0
56
+ project7 = Project.new("cluj")
57
+ puts project7
58
+ end
59
+ end
@@ -0,0 +1,83 @@
1
+ require_relative 'project'
2
+ require_relative 'die'
3
+ require_relative 'funding_round'
4
+
5
+ module Pr
6
+ class RubyProjects
7
+ def initialize(name)
8
+ @name = name.capitalize
9
+ @rp = []
10
+ end
11
+ def load_project(from_file)
12
+ File.readlines(from_file).each do |line|
13
+ add_project(Pr::Project.from_csv(line))
14
+ end
15
+ end
16
+ def add_project(p)
17
+ @rp << p
18
+ end
19
+ def request_funding(rounds)
20
+ puts "\n#{@name} have #{@rp.size} projects "
21
+ puts @rp
22
+ prizes = Pledge::PRIZES
23
+ puts "\nThere are #{prizes.size} possible amounts:"
24
+ prizes.each do |p|
25
+ puts "A #{p.name} pledge is worth $#{p.amount}"
26
+ end
27
+ def total_funds_projects
28
+ @rp.reduce(0) { |sum,p| sum + p.total_funds}
29
+ end
30
+ 1.upto(rounds)do |round|
31
+ puts"\nRound #{round}:"
32
+ @rp.each do |p|
33
+ puts "Target funding amount for #{p.name} is $#{p.target}"
34
+ FundingRound.take_turn(p)
35
+ puts p
36
+ end
37
+ end
38
+ end
39
+ def print_name_funds(p)
40
+ puts "#{p.name} ($#{p.total_funds}) target ($#{p.target})"
41
+ end
42
+ def fully_funded_projects
43
+ @rp.select { |project| project.fully_funded? }
44
+ end
45
+
46
+ def under_funded_projects
47
+ @rp.reject { |project| project.fully_funded? }
48
+ end
49
+ def print_stats
50
+ puts "\n#{@name}'s projects statistic"
51
+ puts "\n#{fully_funded_projects.size} full funding projects"
52
+ fully_funded_projects.each do |p|
53
+ print_name_funds(p)
54
+ end
55
+ puts "\n#{under_funded_projects.size} part funding projects"
56
+ under_funded_projects.each do |p|
57
+ print_name_funds(p)
58
+ end
59
+ @rp.each do |p|
60
+ puts "\nProject #{p.name}'s pledges : "
61
+ p.each_received_pledge do |rp|
62
+ puts "$#{rp.amount}in #{rp.name} pledges"
63
+ end
64
+ puts "$#{p.pledges} in total pledges"
65
+ end
66
+ puts"\n#{@name} High Founding Amount:"
67
+ @rp.sort.each do |p|
68
+ formatted_name = p.name.ljust(20,'.')
69
+ puts "#{formatted_name} $#{p.total_funds}"
70
+ end
71
+ puts "\nTotal funds for #{@name} projects is $#{total_funds_projects} "
72
+ end
73
+ def save_projects_need_funding(to_file="projects_need_funding.txt")
74
+ File.open(to_file, "w") do |file|
75
+ file.puts "These projects still need your help:"
76
+ under_funded_projects.sort.each do |project|
77
+ formatted_name = project.name.ljust(20, '.')
78
+ file.puts "#{formatted_name} $#{project.total_funds} target $#{project.target}"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,55 @@
1
+ require 'project'
2
+ module Pr
3
+ describe Project do
4
+ before do
5
+ @funding_amount = 1000
6
+ @project = Project.new("mario",@funding_amount,5000)
7
+ $stdout = StringIO.new
8
+ end
9
+
10
+ it "has a initial target funding amount" do
11
+ @project.target.should == 5000
12
+ end
13
+ it "computes the total funds outstanding as the target funding amount minus the funding amount" do
14
+ @project.total_funding_outstanding.should == (5000 - 1000)
15
+ end
16
+ it "increases funds by 25 when funds are added" do
17
+ @project.got
18
+
19
+ @project.amount.should == @funding_amount + 25
20
+ end
21
+
22
+ it "decreases funds by 15 when funds are removed" do
23
+ @project.lost
24
+
25
+ @project.amount.should == @funding_amount - 15
26
+ end
27
+
28
+ context "created without a funding amount" do
29
+ before do
30
+ @project = Project.new("Project ABC")
31
+ end
32
+
33
+ it "has a default funding amount of 0" do
34
+ @project.amount.should == 0
35
+ end
36
+ end
37
+ context "with a founding amount greater or equal than founding target" do
38
+ before do
39
+ @project = Project.new("Project ABC",250,200)
40
+ end
41
+ it "is fully funded" do
42
+ @project.should be_fully_funded
43
+ end
44
+ context "with a founding amount less than founding target" do
45
+ before do
46
+ @project = Project.new("Project ABC",150,200)
47
+ end
48
+ it "is not fully funded" do
49
+ @project.should_not be_fully_funded
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,41 @@
1
+ require 'rubyProjects'
2
+ require 'die'
3
+ module Pr
4
+ describe "RubyProjects" do
5
+ before do
6
+ @rubyProjects = RubyProjects.new("romania")
7
+ @amount = 100
8
+ @target = 1000
9
+ @project = Project.new("nico",@amount,@target)
10
+ @rubyProjects.add_project(@project)
11
+ end
12
+ it "got funds if a high number is rolled" do
13
+ Die.any_instance.stub(:roll).and_return(5)
14
+ @rubyProjects.request_funding(2)
15
+ @project.amount.should == @amount + (25*2)
16
+ end
17
+ it "skip project if a medium number is rolled" do
18
+ Die.any_instance.stub(:roll).and_return(3)
19
+ @rubyProjects.request_funding(2)
20
+ @project.amount.should == @amount
21
+ end
22
+ it "lost funds if a high number is rolled" do
23
+ Die.any_instance.stub(:roll).and_return(1)
24
+ @rubyProjects.request_funding(2)
25
+ @project.amount.should == @amount - (15*2)
26
+ end
27
+
28
+ context "in a collection of projects" do
29
+ before do
30
+ @project2 = Project.new("mario",25,75)
31
+ @project3 = Project.new("nabuco",100,1000)
32
+ @project4 = Project.new("alba",125,750)
33
+
34
+ @projects = [@project2 ,@project3 ,@project4 ]
35
+ end
36
+ it "is sorted by decreasing score" do
37
+ @projects.sort.should == [@project4 ,@project3 ,@project2 ]
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: project_ruby_exemplu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolae Turean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-11 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 an example of application for learning ruby used in The Pragmatic
28
+ Studio's \nRuby Programming course .\n\nThis code is Copyright 2013 Nicolae Turean.
29
+ See the LICENSE file."
30
+ email: n_turean@yahoo.com
31
+ executables:
32
+ - crowdfund
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - bin/crowdfund
37
+ - bin/projects.csv
38
+ - bin/projects_need_funding.txt
39
+ - lib/college_account.rb
40
+ - lib/die.rb
41
+ - lib/fundable.rb
42
+ - lib/funding_round.rb
43
+ - lib/grand_project.rb
44
+ - lib/matching_project.rb
45
+ - lib/pledge.rb
46
+ - lib/project.rb
47
+ - lib/rubyProjects.rb
48
+ - spec/project_spec.rb
49
+ - spec/rubyProjects_spec.rb
50
+ - LICENSE
51
+ - README
52
+ homepage:
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '1.9'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.0.6
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Learn ruby example
75
+ test_files:
76
+ - spec/project_spec.rb
77
+ - spec/rubyProjects_spec.rb