Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 11, 2016
0 parents
commit f577566
Showing
537 changed files
with
99,446 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2008 David Berube | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
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 OR APRESS 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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#Apress Source Code | ||
|
||
This repository accompanies [*Practical Reporting with Ruby and Rails*](http://www.apress.com/9781590599334) by David Berube (Apress, 2008). | ||
|
||
![Cover image](9781590599334.jpg) | ||
|
||
Download the files as a zip using the green button, or clone the repository to your machine using Git. | ||
|
||
##Releases | ||
|
||
Release v1.0 corresponds to the code in the published book, without corrections or updates. | ||
|
||
##Contributions | ||
|
||
See the file Contributing.md for more information on how you can contribute to this repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'active_record' | ||
|
||
ActiveRecord::Base.establish_connection( | ||
:adapter => 'mysql', | ||
:host => 'localhost', | ||
:username => 'root', # This is the default username and password | ||
:password => '', # for MySQL, but note that if you have a | ||
# different username and password, | ||
# you should change it. | ||
:database => 'players') | ||
|
||
|
||
class Player < ActiveRecord::Base | ||
end | ||
|
||
Player.delete_all | ||
|
||
Player.new do |p| | ||
p.name = "Matthew 'm_giff' Gifford" | ||
p.salary = 89000.00 | ||
p.wins = 11 | ||
p.save | ||
end | ||
|
||
Player.new do |p| | ||
p.name = "Matthew 'Iron Helix' Bouley" | ||
p.salary = 75000.00 | ||
p.wins = 4 | ||
p.save | ||
end | ||
|
||
Player.new do |p| | ||
p.name = "Luke 'Cable Boy' Bouley" | ||
p.salary = 75000.50 | ||
p.wins = 7 | ||
p.save | ||
end | ||
|
||
salary_total = 0 | ||
win_total=0 | ||
|
||
players = Player.find(:all) | ||
players.each do |player| | ||
puts "#{player.name}: $#{'%0.2f' % (player.salary/player.wins)} per win" | ||
salary_total = salary_total + player.salary | ||
win_total = win_total + player.wins | ||
end | ||
|
||
puts "\nAverage Cost Per Win : $#{'%0.2f' % (salary_total / win_total )}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE DATABASE players; | ||
USE players; | ||
CREATE TABLE players ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name TEXT, | ||
wins int(11) NOT NULL, | ||
salary DECIMAL(9,2), | ||
PRIMARY KEY (id) | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
DROP DATABASE players_2; | ||
CREATE DATABASE players_2; | ||
USE players_2; | ||
|
||
CREATE TABLE players ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name TEXT, | ||
salary DECIMAL(9,2), | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO players (id, name, salary) VALUES (1, "Matthew 'm_giff' Gifford", 89000.00); | ||
INSERT INTO players (id, name, salary) VALUES (2, "Matthew 'Iron Helix' Bouley", 75000.00); | ||
INSERT INTO players (id, name, salary) VALUES (3, "Luke 'Cable Boy' Bouley", 75000.50); | ||
|
||
CREATE TABLE games ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name TEXT, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO games (id, name) VALUES (1, 'Eagle Beagle Ballad'); | ||
INSERT INTO games (id, name) VALUES (2, 'Camel Tender Redux'); | ||
INSERT INTO games (id, name) VALUES (3, 'Super Dunkball II: The Return'); | ||
INSERT INTO games (id, name) VALUES (4, 'Turn the Corner SE: CRX vs Porche'); | ||
|
||
CREATE TABLE wins ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
player_id int(11) NOT NULL, | ||
game_id int(11) NOT NULL, | ||
quantity int(11) NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 1, 3); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 3, 5); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 2, 9); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 4, 9); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 1, 8); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 3, 5); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 2, 13); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 4, 5); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 1, 2); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 3, 15); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 2, 4); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 4, 6); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'active_record' | ||
|
||
ActiveRecord::Base.establish_connection( | ||
:adapter => 'mysql', | ||
:host => 'localhost', | ||
:username => 'root', # This is the default username and password | ||
:password => '', # for MySQL, but note that if you have a | ||
# different username and password, | ||
# you should change it. | ||
:database => 'players_2') | ||
|
||
|
||
class Player < ActiveRecord::Base | ||
has_many :wins | ||
def total_wins | ||
total_wins = 0 | ||
self.wins.each do |win| | ||
total_wins = total_wins + win.quantity | ||
end | ||
total_wins | ||
end | ||
end | ||
class Game < ActiveRecord::Base | ||
has_many :wins | ||
end | ||
class Win < ActiveRecord::Base | ||
belongs_to :game | ||
belongs_to :player | ||
end | ||
|
||
games = Game.find(:all) | ||
|
||
games.each do |game| | ||
highest_win=nil | ||
game.wins.each do |win| | ||
highest_win = win if highest_win.nil? or | ||
win.quantity > highest_win.quantity | ||
end | ||
|
||
puts "#{game.name}: #{highest_win.player.name} with #{highest_win.quantity} wins" | ||
end | ||
|
||
players = Player.find(:all) | ||
|
||
highest_winning_player = nil | ||
|
||
players.each do |player| | ||
highest_winning_player = player if | ||
highest_winning_player.nil? or | ||
player.total_wins > highest_winning_player.total_wins | ||
end | ||
|
||
puts "Highest Winning Player: #{highest_winning_player.name} with #{highest_winning_player.total_wins} wins" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
DROP DATABASE IF EXISTS players_3 ; | ||
CREATE DATABASE players_3; | ||
USE players_3; | ||
|
||
CREATE TABLE players ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name TEXT, | ||
drink TEXT, | ||
salary DECIMAL(9,2), | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(1, "Matthew 'm_giff' Gifford", "Moxie", 89000.00); | ||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(2, "Matthew 'Iron Helix' Bouley", "Moxie", 75000.00); | ||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(3, "Luke 'Cable Boy' Bouley", "Moxie", 75000.50); | ||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(4, "Andrew 'steven-tyler-xavier' Thomas", 'Fresca', 75000.50); | ||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(5, "John 'dwy_dwy' Dwyer", 'Fresca', 89000.00); | ||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(6, "Ryan 'the_dominator' Peacan", 'Fresca', 75000.50); | ||
INSERT INTO players (id, name, drink, salary) VALUES | ||
(7, "Michael 'Shaun Wiki' Southwick", 'Fresca', 75000.50); | ||
|
||
CREATE TABLE games ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name TEXT, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO games (id, name) VALUES (1, 'Bubble Recyler'); | ||
INSERT INTO games (id, name) VALUES (2, 'Computer Repair King'); | ||
INSERT INTO games (id, name) VALUES (3, 'Super Dunkball II: The Return'); | ||
INSERT INTO games (id, name) VALUES (4, 'Sudden Decelleration: No Time to Think'); | ||
|
||
CREATE TABLE wins ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
player_id int(11) NOT NULL, | ||
game_id int(11) NOT NULL, | ||
quantity int(11) NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 1, 3); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 3, 8); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 2, 3); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (1, 4, 9); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 1, 8); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 3, 10); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 2, 7); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (2, 4, 5); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 1, 8); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 3, 4); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 2, 20); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (3, 4, 8); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (4, 1, 8); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (4, 3, 9); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (4, 2, 6); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (4, 4, 3); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (5, 1, 7); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (5, 3, 1); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (5, 2, 9); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (5, 4, 4); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (6, 1, 2); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (6, 3, 10); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (6, 2, 8); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (6, 4, 9); | ||
|
||
INSERT INTO wins (player_id, game_id, quantity) VALUES (7, 1, 2); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (7, 3, 1); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (7, 2, 4); | ||
INSERT INTO wins (player_id, game_id, quantity) VALUES (7, 4, 9); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'active_record' | ||
|
||
# Establish a connection to the database... | ||
ActiveRecord::Base.establish_connection( | ||
:adapter => 'mysql', | ||
:host => 'localhost', | ||
:username => 'root', # This is the default username and password | ||
:password => '', # for MySQL, but note that if you have a | ||
# different username and password, | ||
# you should change it. | ||
:database => 'players_3') | ||
|
||
# ... set up our models ... | ||
class Player < ActiveRecord::Base | ||
has_many :wins | ||
end | ||
class Game < ActiveRecord::Base | ||
has_many :wins | ||
end | ||
class Win < ActiveRecord::Base | ||
belongs_to :game | ||
belongs_to :player | ||
end | ||
|
||
# ... and perform our calculation: | ||
|
||
puts "Salary\t\tCount" | ||
|
||
Player.calculate(:count, :id, :group=>'salary').each do |player| | ||
salary, count = *player | ||
puts "$#{'%0.2f' % salary}\t#{count} " | ||
|
||
# Note that the '%0.25f' % call formats the value as a float | ||
# with two decimal points; the String's % operator works | ||
# similarly to the C sprintf function. | ||
|
||
end | ||
|
Oops, something went wrong.