Skip to content

Commit 8ae2040

Browse files
committed
Notes and code for 2015-02-19 open office hours session
1 parent e3fda4e commit 8ae2040

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

session-notes/2015-02-19/README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Fundamentals of Web Development
2+
3+
**February 19, 2015**
4+
5+
Questions:
6+
7+
- What are procs
8+
- Usage of instance variables, local variables, and symbols in Sinatra
9+
- Schema design in SQL & data modeling in Ruby
10+
- When to use helper functions in Sinatra
11+
- How much / what kind of code is conventionally allowed in views
12+
13+
## Data Modeling / Schema Design
14+
15+
Cities
16+
- name
17+
- population
18+
- elevation
19+
- mayor
20+
21+
GasStations / PointsOfInterest
22+
- name
23+
- location (road)
24+
25+
Roads
26+
- length
27+
- speed limit
28+
29+
Route
30+
- start (city)
31+
- destination (city)
32+
- roads [sequence of ordered (roads)]
33+
34+
35+
```sql
36+
cities
37+
- id[key]
38+
- name[string]
39+
- population[int]
40+
- elevation[int]
41+
- mayor[string]
42+
43+
gas_stations
44+
- id[int]
45+
- name[string]
46+
- road_id[foreign key]
47+
48+
roads
49+
- id[int]
50+
- length[int]
51+
- speed_limit[int]
52+
53+
route_sequence
54+
- id[key]
55+
- order[int]
56+
- route_id[foreign key -> routes]
57+
- road_id[foreign key -> roads]
58+
59+
routes
60+
- id[int]
61+
- start_id[foreign key -> cities]
62+
- destination_id[foreign key -> cities]
63+
```
64+
65+
```ruby
66+
class City
67+
attr_accessor :name, :population, :elevation, :mayor
68+
end
69+
70+
class PointOfInterest
71+
# location is a Road instance
72+
attr_accessor :name, :location
73+
end
74+
75+
class Road
76+
attr_accessor :length, :speed_limit
77+
end
78+
79+
class Route
80+
# start and destination are instances of City
81+
# roads is an array of roads
82+
attr_accessor :start, :destination, :roads
83+
end
84+
85+
boston = City.new(name: "Boston")
86+
philly = City.new(name: "Philly")
87+
88+
i95 = Road.new(length: 55, speed_limit: 65)
89+
i42 = Road.new(length: 45, speed_limit: 55)
90+
91+
boston_to_philly = Route.new
92+
boston_to_philly.roads = [i42, i95]
93+
94+
boston_to_philly.directions => "go on i42 for 45 miles then i95 for 55 miles"
95+
```
96+
97+
```ruby
98+
# DataMapper implementation of the SQL schema
99+
100+
class City
101+
include DataMapper::Resource
102+
103+
property :name, String
104+
property :population, Integer
105+
property :elevation, Integer
106+
property :mayor, String
107+
end
108+
109+
class PointOfInterest
110+
include DataMapper::Resource
111+
112+
property :name,
113+
property :location
114+
end
115+
116+
class Road
117+
include DataMapper::Resource
118+
119+
property :length,
120+
property :speed_limit
121+
122+
has n, :route_sequences
123+
has n, :routes, through: :route_sequences
124+
end
125+
126+
class RouteSequence
127+
property :order, Integer
128+
129+
belongs_to :road
130+
belongs_to :route
131+
end
132+
133+
class Route
134+
include DataMapper::Resource
135+
136+
belongs_to :start, ['City']
137+
belongs_to :destination, ['City']
138+
139+
has n, :route_sequences
140+
has n, :roads, through: :route_sequences
141+
end
142+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class User
2+
include DataMapper::Resource
3+
4+
get_time = lambda { DateTime.now }
5+
6+
self.created_at_config = GET_TIME
7+
8+
@run_when_created = GET_TIME
9+
10+
before :create do
11+
self.created_at = GET_TIME.call
12+
end
13+
14+
def get_time
15+
DateTime.now
16+
end
17+
18+
property :created_at, DateTime
19+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'sinatra'
2+
3+
helpers do
4+
def current_user
5+
puts 'helper method'
6+
@user ||= params[:user_id]
7+
end
8+
end
9+
10+
# def current_user
11+
# puts 'global method'
12+
# @user ||= params[:user_id]
13+
# end
14+
#
15+
# current_user
16+
17+
get '/' do
18+
# erb("<%= current_user %>")
19+
# erb = "#{current_user}"
20+
current_user
21+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
input_person = {
2+
id: '213', # => "213"
3+
name: 'buddy', # => "buddy"
4+
birthday: 'June 30, 1982' # => "June 30, 1982"
5+
} # => {:id=>"213", :name=>"buddy", :birthday=>"June 30, 1982"}
6+
7+
require 'date' # => true
8+
9+
class User
10+
attr_accessor :id, :name, :birthday # => nil
11+
12+
PARSER_FOR = {
13+
id: lambda { |id| id.to_i }, # => #<Proc:0x007fbba98fb788@/Users/tanner/Clients/codeunion/fundamentals-of-web-development/session-notes/2015-02-19/lambda-example.rb:13 (lambda)>
14+
name: lambda { |name| name.capitalize }, # => #<Proc:0x007fbba98fb620@/Users/tanner/Clients/codeunion/fundamentals-of-web-development/session-notes/2015-02-19/lambda-example.rb:14 (lambda)>
15+
birthday: lambda { |date| Date.parse(date) }, # => #<Proc:0x007fbba98fb530@/Users/tanner/Clients/codeunion/fundamentals-of-web-development/session-notes/2015-02-19/lambda-example.rb:15 (lambda)>
16+
} # => {:id=>#<Proc:0x007fbba98fb788@/Users/tanner/Clients/codeunion/fundamentals-of-web-development/session-notes/2015-02-19/lambda-example.rb:13 (lambda)>, :name=>#<Proc:0x007fbba98fb620@/Users/tanner/Clients/codeunion/fundamentals-of-web-development/session-notes/2015-02-19/lambda-example.rb:14 (lambda)>, :birthday=>#<Proc:0x007fbba98fb530@/Users/tanner/Clients/codeunion/fundamentals-of-web-development/session-notes/2015-02-19/lambda-example.rb:15 (lambda)>}
17+
18+
def self.from_hash(input_hash)
19+
user = self.new # => #<User:0x007fbba98fb030>
20+
21+
input_hash.each do |property, value| # => {:id=>"213", :name=>"buddy", :birthday=>"June 30, 1982"}
22+
PARSER_FOR[property].call(value) # => 213, "Buddy", #<Date: 1982-06-30 ((2445151j,0s,0n),+0s,2299161j)>
23+
end # => {:id=>"213", :name=>"buddy", :birthday=>"June 30, 1982"}
24+
25+
user # => #<User:0x007fbba98fb030>
26+
end
27+
end
28+
29+
User.from_hash(input_person) # => #<User:0x007fbba98fb030>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
helpers do
3+
def current_fruit
4+
@fruit = 'bananas'
5+
end
6+
7+
def double_age
8+
age * 2
9+
end
10+
end
11+
12+
13+
get '/' do
14+
current_fruit # => bananas
15+
@fruit = 'apple'
16+
car = 'Buick'
17+
18+
erb(:home, locals: { age: 2 }) # => '<html>....'
19+
erb :index # => '<html>....'
20+
erb :save_user # => '<html>....'
21+
end
22+
23+
# ----- home.erb
24+
25+
# <%= @fruit %>
26+
# <%= car %>
27+
# <%= age %>

0 commit comments

Comments
 (0)