-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeriodicTableNav.rb
216 lines (184 loc) · 6.62 KB
/
PeriodicTableNav.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require 'sinatra' #meaningless comment
require 'sinatra/activerecord'
require 'active_record'
require './environments'
=begin
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/elements')
db = URI.parse('postgres://jemonat@localhost/elements')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
=end
SITE_TITLE = "Periodic Table Navigator"
SITE_DESCRIPTION = "See how the elements are related to each other"
class Element < ActiveRecord::Base
end
class Base < ActiveRecord::Base
end
class Orb < ActiveRecord::Base
end
class Orbital < ActiveRecord::Base
end
=begin
get "/index" do
load_elements(params[:name])
@element = Element.find(2)
@elements = Element.order("atomic_num ASC")
erb :index
end
=end
get "/" do
load_elements(params[:name])
@title = 'All Elements'
erb :home
end
get '/element/:atomic_num' do |atomic_num| # load element page
load_elements(params[:name])
atomic_num = atomic_num.to_i
redirect('/') if (atomic_num > @max_element) or (atomic_num <= 0) # redirect to home page if user tries to compose a URL to a non-existent element
@origin = Element.find(atomic_num)
@title = "Element ##{atomic_num}"
erb :element
end
get '/period/:period' do |period| # load period page
load_elements(params[:name])
period = period.to_i
@period = period
redirect('/') if (@period > @max_period) or (@period <= 0) # redirect to home page if user tries to compose a URL to a non-existent period
@title = "Period ##{period}"
erb :period
end
get '/group/:group' do |traditional_group| # load group page
load_elements(params[:name])
@traditional_group = traditional_group
@linear_group = group_trad_to_lin(traditional_group)["num"]
if (@linear_group > @max_group) or (@linear_group <= 0)
redirect('/') # redirect to home page if user tries to compose a URL to a non-existent group
end
@group_elements = Element.where(:grouplin => @linear_group).order(period: :asc)
@title = "Group ##{traditional_group}"
erb :group
end
get '*' do # redirect any other requests to homepage
redirect('/')
end
=begin
get '/test/:name' do
@elementAR = Element.find(2)
erb :'inactive/test'
end
=end
helpers do
def init_constants
# Using linear group numbering of 1-32 (f-block columns numbered as groups):
@main_pauses_group = 2 # where main part of periodic table (non-f block) leaves off
@main_resumes_group = 17 # where main part of periodic table (non-f block) picks back up
#HTML codes for special characters
@star = "★" # 5-pointed star, ★
@space = " " # non-breaking space, used as a spacer
@left_arrow = "◀" # "<" # "⇐" # left double arrow, ⇐
@left_jump_arrow = "◀◀" # "<<" # "⇚" # left triple arrow, ⇚
@right_arrow = "▶" # ">" # "⇒" # right double arrow, ⇒
@right_jump_arrow = "▶▶" # ">>" # "⇛" # right triple arrow, ⇛
@up_arrow = "▲" # "^" # "Λ" # "⇑" # up double arrow, ⇑
@down_arrow = "▼" # "V" # "⇓" # down double arrow, ⇓
end
def group_lin_to_trad(linear_group)
init_constants
case linear_group
when 1..@main_pauses_group #linear groups 1-2 = traditional groups 1-2
trad_group_num = linear_group
trad_group_type = "main"
when (@main_pauses_group+1)..(@main_resumes_group-1) #linear groups 3-16 = f groups 1-14
trad_group_num = linear_group - 2 # e.g. linear group 3 is f group 1
trad_group_type = "f"
when @main_resumes_group..@max_group #linear groups 17-32 = traditional groups 3-18
trad_group_num = linear_group - @num_f_groups # e.g. linear group 17 is traditional group 3
trad_group_type = "main"
else #error
trad_group_num = -1
trad_group_type = "error"
end
@result = Hash["num" => trad_group_num, "type" => trad_group_type]
end
def group_trad_to_lin(traditional_group)
init_constants
if traditional_group.to_s[0].match(/^[[:alpha:]]$/) && (traditional_group.to_s[1].match(/^[[:digit:]]$/))
#if group starts with a letter and second character is a number, e.g. f5
#if is an f group, e.g. f12 (starts with a letter)
if (traditional_group.to_s[0].match(/^[[f]]$/))
lin_group_type = traditional_group[0] # f
lin_group_num = traditional_group[1..traditional_group.length-1].to_i + @main_pauses_group
if (lin_group_num > @num_f_groups + @main_pauses_group) || (lin_group_num <= @main_pauses_group)
#if the linear group number is outside the bounds (3-16) for an f group (corresponding to f1-f14)
lin_group_num = -1
lin_group_type = "error"
end
else
lin_group_num = -3 # error: group starts with a letter other than f
lin_group_type = "error"
end
else
traditional_group = traditional_group.to_i
case traditional_group
when 1..@main_pauses_group #traditional groups 1-2 = linear groups 1-2
lin_group_num = traditional_group
lin_group_type = "main"
when (@main_pauses_group+1)..(@max_group-@num_f_groups) #traditional groups 3-18 = linear groups 17-32
lin_group_num = traditional_group + @num_f_groups # e.g. traditional group 3 is linear group 17
lin_group_type = "main"
else #error
lin_group_num = -2
lin_group_type = "error"
end
end
@result = Hash["num" => lin_group_num, "type" => lin_group_type]
end
def load_elements(name)
init_constants
@elements = Element.order("atomic_num ASC")
@max_period = Element.last(1)[0].period
@max_group = Element.order(grouplin: :desc)[0].grouplin
@max_element = Element.last(1)[0].atomic_num
@ebyp = Array.new(@max_period) # array to hold elements
@ebyp.each_index do |period| # build a 2D array: by period, then by element
@ebyp[period] = @elements.select{|element| element.period == period+1}
end
@bases = Base.all
#Build array of groups in main groups (non-f block)
ongroup = 1
@main_groups = Array.new
while ongroup <= @main_pauses_group do
@main_groups << ongroup
ongroup += 1
end
ongroup = @main_resumes_group
while ongroup <= @max_group do
@main_groups << ongroup
ongroup += 1
end
#Build array of f groups
@f_groups = Array ((@main_pauses_group + 1) .. (@main_resumes_group -1))
@num_f_groups = @f_groups.size #14 # number of f groups
end
def load_orbitals(name)
@orbitals = Orbital.order("id ASC")
#Build hash of orbitals (e.g. id 11 = 5p)
@orbital_hash = Hash.new
orbital_id = 1
@orbitals.each do |orbital|
@orbital_hash[orbital_id] = Hash.new
@orbital_hash[orbital_id]["n"] = orbital.n
@orbital_hash[orbital_id]["l"] = orbital.l
orbital_id += 1
end
end
end # helpers
before do
load_orbitals("")
end