Class: Teepee::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/teepee/commander.rb

Direct Known Subclasses

ActionableCommander

Instance Method Summary collapse

Instance Method Details

#%(*numbers) ⇒ Object



171
172
173
174
# File 'lib/teepee/commander.rb', line 171

def % *numbers
  ensure_numeric numbers
  numbers.inject { |base, percent| base*percent/100.0 }
end

#*(*numbers) ⇒ Object



155
156
157
# File 'lib/teepee/commander.rb', line 155

def * *numbers
  ensure_numeric numbers.inject 1, :*
end

#**(*numbers) ⇒ Object



167
168
169
# File 'lib/teepee/commander.rb', line 167

def ** *numbers
  ensure_numeric numbers.reduce :**
end

#+(*numbers) ⇒ Object




143
144
145
# File 'lib/teepee/commander.rb', line 143

def + *numbers
  ensure_numeric numbers.inject 0, :+
end

#-(*numbers) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/teepee/commander.rb', line 147

def - *numbers
  if numbers.length == 1
    ensure_numeric -numbers.first
  else
    ensure_numeric numbers.reduce :-
  end
end

#/(*numbers) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/teepee/commander.rb', line 159

def / *numbers
  if numbers.length == 1
    ensure_numeric 1 / numbers.first
  else
    ensure_numeric numbers.reduce :/
  end
end

#acos(number) ⇒ Object



191
192
193
# File 'lib/teepee/commander.rb', line 191

def acos number
  ensure_numeric Math.acos number
end

#acosh(number) ⇒ Object



195
196
197
# File 'lib/teepee/commander.rb', line 195

def acosh number
  ensure_numeric Math.acosh number
end

#add_percentage(*numbers) ⇒ Object



176
177
178
179
# File 'lib/teepee/commander.rb', line 176

def add_percentage *numbers
  ensure_numeric numbers
  numbers.inject {|base, percent| base * (1+percent/100.0) }
end

#asin(number) ⇒ Object



199
200
201
# File 'lib/teepee/commander.rb', line 199

def asin number
  ensure_numeric Math.asin number
end

#asinh(number) ⇒ Object



203
204
205
# File 'lib/teepee/commander.rb', line 203

def asinh number
  ensure_numeric Math.asinh number
end

#atan(number) ⇒ Object



207
208
209
# File 'lib/teepee/commander.rb', line 207

def atan number
  ensure_numeric Math.atan number
end

#atanh(number) ⇒ Object



211
212
213
# File 'lib/teepee/commander.rb', line 211

def atanh number
  ensure_numeric Math.atanh number
end

#b(expressions) ⇒ Object



215
216
217
# File 'lib/teepee/commander.rb', line 215

def b expressions
  html_tag :b, expressions
end

#backquoteObject



219
220
221
# File 'lib/teepee/commander.rb', line 219

def backquote
  "`"
end

#backslashObject



223
224
225
# File 'lib/teepee/commander.rb', line 223

def backslash
  "\\"
end

#big(expressions) ⇒ Object



227
228
229
# File 'lib/teepee/commander.rb', line 227

def big expressions
  html_tag :big, expressions
end

#bookmarks_folder_id(id) ⇒ Object



231
232
233
# File 'lib/teepee/commander.rb', line 231

def bookmarks_folder_id id
  id_command_handler id, :Folder, "folder", "folders", "folders/bookmarks_inline", "bookmarks"
end

#boolean_and(expressions) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/teepee/commander.rb', line 235

def boolean_and expressions
  if expressions.empty?
    "true"
  elsif expressions.first.to_s == "false"
    "false"
  elsif expressions.first.to_s == "true" or expressions.first.kind_of? WhitespaceToken
    boolean_and expressions[1..-1]
  else
    command_error "Not a boolean value #{expressions.first}"
  end
end

#boolean_nand(expressions) ⇒ Object



247
248
249
# File 'lib/teepee/commander.rb', line 247

def boolean_nand expressions
  boolean_not boolean_and expressions
end

#boolean_nor(expressions) ⇒ Object



251
252
253
# File 'lib/teepee/commander.rb', line 251

def boolean_nor expressions
  boolean_not boolean_or expressions
end

#boolean_not(expression) ⇒ Object



255
256
257
258
259
260
261
262
263
# File 'lib/teepee/commander.rb', line 255

def boolean_not expression
  if expression.to_s == "true"
    "false"
  elsif expression.to_s == "false"
    "true"
  else
    command_error "Not a boolean value"
  end
end

#boolean_or(expressions) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/teepee/commander.rb', line 265

def boolean_or expressions
  if expressions.empty?
    "false"
  elsif expressions.first.to_s == "true"
    "true"
  elsif expressions.first.to_s == "false" or expressions.first.kind_of? WhitespaceToken
    boolean_or expressions[1..-1]
  else
    command_error "Not a boolean value"
  end
end

#boolean_xnor(expressions) ⇒ Object



277
278
279
# File 'lib/teepee/commander.rb', line 277

def boolean_xnor expressions
  boolean_not boolean_xor expressions
end

#boolean_xor(expressions) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/teepee/commander.rb', line 281

def boolean_xor expressions
  # There are two schools of thought as to what a multi-variable XOR is.
  # 1. Chained XORs, giving a parity check.
  # 2. 'Exclusively' one true for ALL inputs.
  # I'm going with the second: one and only one true, the rest false.
  # It seems therefore that the zero-argument version should be false then.
  if expressions.empty?
    "false"
  else
    any_trues = false
    expressions.each do |expression|
      if expression.to_s == "true"
        if any_trues
          return "false"
        else
          any_trues = true
        end
      elsif expression.to_s == "false"
        # do nothing
      elsif expression.kind_of? WhitespaceToken
        # do nothing
      else
        return command_error "Not a boolean value"
      end
    end
    return any_trues.to_s
  end
end

#brObject



310
311
312
# File 'lib/teepee/commander.rb', line 310

def br
  html_tag :br, nil
end

#command_error(message) ⇒ Object



56
57
58
# File 'lib/teepee/commander.rb', line 56

def command_error message
  %{<span style="color: red">[#{message}]</span>}
end

#command_not_yet_implemented(command) ⇒ Object



60
61
62
# File 'lib/teepee/commander.rb', line 60

def command_not_yet_implemented command
  command_error "The command #{command} is not yet implemented."
end

#cos(angle) ⇒ Object



314
315
316
# File 'lib/teepee/commander.rb', line 314

def cos angle
  ensure_numeric Math.cos angle
end

#cosh(number) ⇒ Object



318
319
320
# File 'lib/teepee/commander.rb', line 318

def cosh number
  ensure_numeric Math.cosh number
end

#degrees2radians(degrees) ⇒ Object



322
323
324
# File 'lib/teepee/commander.rb', line 322

def degrees2radians degrees
  ensure_numeric degrees * Math::PI / 180.0
end

#del(expressions) ⇒ Object



326
327
328
# File 'lib/teepee/commander.rb', line 326

def del expressions
  html_tag :del, expressions
end

#eObject



330
331
332
# File 'lib/teepee/commander.rb', line 330

def e
  Math::E
end

#ensure_boolean(boolean) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/teepee/commander.rb', line 74

def ensure_boolean boolean
  if boolean.to_s == "true" or boolean.to_s == "false"
    boolean
  else
    command_error "Non-boolean value."
  end
end

#ensure_numeric(number) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/teepee/commander.rb', line 64

def ensure_numeric number
  if number.kind_of? Complex
    command_error "Complex numbers are not yet supported."
  elsif not number.kind_of? Numeric
    command_error "Non-numeric result."
  else
    number
  end
end

#enumerate(expressions) ⇒ Object



334
335
336
# File 'lib/teepee/commander.rb', line 334

def enumerate expressions
  html_tag :ol, expressions
end

#enumerate_lowercase(expressions) ⇒ Object



346
347
348
# File 'lib/teepee/commander.rb', line 346

def enumerate_lowercase expressions
  html_tag :ol, expressions, {type: "a"}
end

#enumerate_numeric(expressions) ⇒ Object



338
339
340
# File 'lib/teepee/commander.rb', line 338

def enumerate_numeric expressions
  html_tag :ol, expressions, {type: "1"}
end

#enumerate_roman_lowercase(expressions) ⇒ Object



354
355
356
# File 'lib/teepee/commander.rb', line 354

def enumerate_roman_lowercase expressions
  html_tag :ol, expressions, {type: "i"}
end

#enumerate_roman_uppercase(expressions) ⇒ Object



350
351
352
# File 'lib/teepee/commander.rb', line 350

def enumerate_roman_uppercase expressions
  html_tag :ol, expressions, {type: "I"}
end

#enumerate_uppercase(expressions) ⇒ Object



342
343
344
# File 'lib/teepee/commander.rb', line 342

def enumerate_uppercase expressions
  html_tag :ol, expressions, {type: "A"}
end

#erf(number) ⇒ Object



358
359
360
# File 'lib/teepee/commander.rb', line 358

def erf number
  ensure_numeric Math.erf number
end

#erfc(number) ⇒ Object



362
363
364
# File 'lib/teepee/commander.rb', line 362

def erfc number
  ensure_numeric Math.erfc number
end

#false_constantObject



366
367
368
# File 'lib/teepee/commander.rb', line 366

def false_constant
  "false"
end

#folder_id(id) ⇒ Object



370
371
372
# File 'lib/teepee/commander.rb', line 370

def folder_id id
  id_command_handler id, :Folder
end

#forum_id(id) ⇒ Object



374
375
376
# File 'lib/teepee/commander.rb', line 374

def forum_id id
  id_command_handler id, :Forum
end

#gamma(number) ⇒ Object



378
379
380
# File 'lib/teepee/commander.rb', line 378

def gamma number
  ensure_numeric Math.gamma number
end

#h1(expressions) ⇒ Object



382
383
384
# File 'lib/teepee/commander.rb', line 382

def h1 expressions
  html_tag :h1, expressions
end

#h2(expressions) ⇒ Object



386
387
388
# File 'lib/teepee/commander.rb', line 386

def h2 expressions
  html_tag :h2, expressions
end

#h3(expressions) ⇒ Object



390
391
392
# File 'lib/teepee/commander.rb', line 390

def h3 expressions
  html_tag :h3, expressions
end

#h4(expressions) ⇒ Object



394
395
396
# File 'lib/teepee/commander.rb', line 394

def h4 expressions
  html_tag :h4, expressions
end

#h5(expressions) ⇒ Object



398
399
400
# File 'lib/teepee/commander.rb', line 398

def h5 expressions
  html_tag :h5, expressions
end

#h6(expressions) ⇒ Object



402
403
404
# File 'lib/teepee/commander.rb', line 402

def h6 expressions
  html_tag :h6, expressions
end

#html_tag(tag, expressions, attribs = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/teepee/commander.rb', line 82

def html_tag tag, expressions, attribs=nil
  opening_tag = if attribs
                  attribs_string = attribs.map {|k,v| %{#{k}="#{v}"}}.join " "
                  if expressions.nil?
                    "<#{tag} #{attribs_string}/>"
                  else
                    "<#{tag} #{attribs_string}>"
                  end
                else
                  if expressions.nil?
                    "<#{tag}/>"
                  else
                    "<#{tag}>"
                  end
                end
  if expressions.nil?
    opening_tag
  else
    opening_tag + expressions.map(&:to_html).join.strip + "</#{tag}>"
  end
end

#hypot(numbers) ⇒ Object



453
454
455
# File 'lib/teepee/commander.rb', line 453

def hypot numbers
  ensure_numeric Math.sqrt numbers.map {|n| n**2}
end

#iObject



406
407
408
# File 'lib/teepee/commander.rb', line 406

def i
  command_error "Complex numbers are not yet supported."
end

#id_command_handler(id, klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view = "") ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/teepee/commander.rb', line 108

def id_command_handler(id,
                       klass,
                       singular = klass.to_s.camelcase_to_snakecase,
                       plural = singular.pluralize,
                       partial = "#{plural}/inline",
                       view="")
  if not id
    command_error "#{singular}_id: error: no #{singular} ID specified"
  elsif not id.to_s =~ /\A[0-9]+\z/
    command_error "#{singular}_id: error: invalid #{singular} ID specified"
  else
    tb_href "#{plural}/#{id.to_s}/#{view}", "#{klass.to_s} ##{id.to_s}"
  end
end

#image(expressions) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/teepee/commander.rb', line 410

def image expressions
  expressions = strip expressions
  uri, *alt_text = expressions
  uri = ERB::Util.html_escape uri.to_s
  if not valid_uri? uri
    command_error "Not a valid URI for the image."
  else
    if alt_text.empty?
      html_tag :img, nil, {src: uri}
    else
      html_tag :img, nil, {src: uri, alt: alt_text.map(&:to_s).join.strip}
    end
  end
end

#it(expressions) ⇒ Object



425
426
427
# File 'lib/teepee/commander.rb', line 425

def it expressions
  html_tag :i, expressions
end

#item(expressions) ⇒ Object



429
430
431
# File 'lib/teepee/commander.rb', line 429

def item expressions
  html_tag :li, expressions
end

#itemize(expressions) ⇒ Object



433
434
435
# File 'lib/teepee/commander.rb', line 433

def itemize expressions
  html_tag :ul, expressions
end

#itemize_circle(expressions) ⇒ Object



441
442
443
# File 'lib/teepee/commander.rb', line 441

def itemize_circle expressions
  html_tag :ul, expressions, {style: "list-style-type:circle"}
end

#itemize_disc(expressions) ⇒ Object



437
438
439
# File 'lib/teepee/commander.rb', line 437

def itemize_disc expressions
  html_tag :ul, expressions, {style: "list-style-type:disc"}
end

#itemize_none(expressions) ⇒ Object



449
450
451
# File 'lib/teepee/commander.rb', line 449

def itemize_none expressions
  html_tag :ul, expressions, {style: "list-style-type:none"}
end

#itemize_square(expressions) ⇒ Object



445
446
447
# File 'lib/teepee/commander.rb', line 445

def itemize_square expressions
  html_tag :ul, expressions, {style: "list-style-type:square"}
end

#keyword_id(id) ⇒ Object



457
458
459
# File 'lib/teepee/commander.rb', line 457

def keyword_id id
  id_command_handler id, :Keyword
end

#ld(n) ⇒ Object



461
462
463
# File 'lib/teepee/commander.rb', line 461

def ld n
  ensure_numeric Math.log2 n
end

#ldexp(fraction, exponent) ⇒ Object



465
466
467
# File 'lib/teepee/commander.rb', line 465

def ldexp fraction, exponent
  ensure_numeric Math.ldexp fraction, exponent
end

#left_braceObject



469
470
471
# File 'lib/teepee/commander.rb', line 469

def left_brace
  "{"
end

#left_bracketObject



473
474
475
# File 'lib/teepee/commander.rb', line 473

def left_bracket
  "["
end

#left_strip(expressions) ⇒ Object



123
124
125
126
127
128
# File 'lib/teepee/commander.rb', line 123

def left_strip expressions
  while expressions.first.kind_of? WhitespaceToken
    expressions.shift
  end
  expressions
end

#lgamma(n) ⇒ Object



477
478
479
# File 'lib/teepee/commander.rb', line 477

def lgamma n
  ensure_numeric Math::lgamma(n).first
end


481
482
483
484
485
486
487
488
489
490
491
# File 'lib/teepee/commander.rb', line 481

def link expressions
  expressions = strip expressions
  uri, *desc = expressions
  uri = ERB::Util.html_escape uri.to_s
  if not valid_uri? uri
    command_error "Not a valid URI."
  else
    desc = [uri] if desc.empty?
    html_tag :a, desc, {href: uri}
  end
end


493
494
495
# File 'lib/teepee/commander.rb', line 493

def link_id id
  id_command_handler id, :Link
end

#ln(number) ⇒ Object



497
498
499
# File 'lib/teepee/commander.rb', line 497

def ln number
  ensure_numeric Math.log number
end

#log(base, number) ⇒ Object



501
502
503
504
505
506
507
508
# File 'lib/teepee/commander.rb', line 501

def log base, number
  if number.nil?
    number, base = base, number
    ensure_numeric Math.log10 number # default to log base 10
  else
    ensure_numeric Math.log number, base
  end
end

#log10(number) ⇒ Object



510
511
512
# File 'lib/teepee/commander.rb', line 510

def log10 number
  ensure_numeric Math.log10 number
end

#mod(*numbers) ⇒ Object



514
515
516
# File 'lib/teepee/commander.rb', line 514

def mod *numbers
  ensure_numeric numbers.reduce :%
end

#nbsp(count) ⇒ Object



518
519
520
521
# File 'lib/teepee/commander.rb', line 518

def nbsp count
  count = 1 unless count and count.kind_of? Numeric and count > 0
  "&nbsp;" * count
end

#note_id(id) ⇒ Object



523
524
525
# File 'lib/teepee/commander.rb', line 523

def note_id id
  id_command_handler id, :Note
end

#percent_total(*numbers) ⇒ Object



186
187
188
189
# File 'lib/teepee/commander.rb', line 186

def percent_total *numbers
  ensure_numeric numbers
  numbers.inject {|total, part| Float(part)/Float(total)*100.0 }
end

#piObject



527
528
529
# File 'lib/teepee/commander.rb', line 527

def pi
  Math::PI
end

#pipeObject



531
532
533
# File 'lib/teepee/commander.rb', line 531

def pipe
  "|"
end

#radians2degrees(radians) ⇒ Object



535
536
537
# File 'lib/teepee/commander.rb', line 535

def radians2degrees radians
  ensure_numeric(radians * 180.0 / Math::PI)
end

#right_braceObject



539
540
541
# File 'lib/teepee/commander.rb', line 539

def right_brace
  "}"
end

#right_bracketObject



543
544
545
# File 'lib/teepee/commander.rb', line 543

def right_bracket
  "]"
end

#right_strip(expressions) ⇒ Object



130
131
132
133
134
135
# File 'lib/teepee/commander.rb', line 130

def right_strip expressions
  while expressions.last.kind_of? WhitespaceToken
    expressions.pop
  end
  expressions
end

#sin(angle) ⇒ Object



547
548
549
# File 'lib/teepee/commander.rb', line 547

def sin angle
  ensure_numeric Math.sin angle
end

#sinh(number) ⇒ Object



551
552
553
# File 'lib/teepee/commander.rb', line 551

def sinh number
  ensure_numeric Math.sinh number
end

#small(expressions) ⇒ Object



555
556
557
# File 'lib/teepee/commander.rb', line 555

def small expressions
  html_tag :small, expressions
end

#spaceObject



563
564
565
# File 'lib/teepee/commander.rb', line 563

def space
  " "
end

#sqrt(number) ⇒ Object



559
560
561
# File 'lib/teepee/commander.rb', line 559

def sqrt number
  ensure_numeric Math.sqrt number
end

#strip(expressions) ⇒ Object



137
138
139
# File 'lib/teepee/commander.rb', line 137

def strip expressions
  left_strip right_strip expressions
end

#sub(expressions) ⇒ Object



567
568
569
# File 'lib/teepee/commander.rb', line 567

def sub expressions
  html_tag :sub, expressions
end

#subtract_percentage(*numbers) ⇒ Object



181
182
183
184
# File 'lib/teepee/commander.rb', line 181

def subtract_percentage *numbers
  ensure_numeric numbers
  numbers.inject {|base, percent| base * (1-percent/100.0) }
end

#sup(expressions) ⇒ Object



571
572
573
# File 'lib/teepee/commander.rb', line 571

def sup expressions
  html_tag :sup, expressions
end

#table(expressions) ⇒ Object



575
576
577
# File 'lib/teepee/commander.rb', line 575

def table expressions
  html_tag :table, expressions
end

#table_data(expressions) ⇒ Object



579
580
581
# File 'lib/teepee/commander.rb', line 579

def table_data expressions
  html_tag :td, expressions
end

#table_header(expressions) ⇒ Object



583
584
585
# File 'lib/teepee/commander.rb', line 583

def table_header expressions
  html_tag :th, expressions
end

#table_row(expressions) ⇒ Object



587
588
589
# File 'lib/teepee/commander.rb', line 587

def table_row expressions
  html_tag :tr, expressions
end

#tag_id(id) ⇒ Object



591
592
593
# File 'lib/teepee/commander.rb', line 591

def tag_id id
  id_command_handler id, :Tag
end

#tan(angle) ⇒ Object



595
596
597
# File 'lib/teepee/commander.rb', line 595

def tan angle
  ensure_numeric Math.tan angle
end

#tanh(number) ⇒ Object



599
600
601
# File 'lib/teepee/commander.rb', line 599

def tanh number
  ensure_numeric Math.tanh number
end

#tb_href(target, string) ⇒ Object



104
105
106
# File 'lib/teepee/commander.rb', line 104

def tb_href target, string
  %{<a href="#{TB_COM}/#{target}">#{string}</a>}
end

#true_constantObject



603
604
605
# File 'lib/teepee/commander.rb', line 603

def true_constant
  "true"
end

#tt(expressions) ⇒ Object



607
608
609
# File 'lib/teepee/commander.rb', line 607

def tt expressions
  html_tag :tt, expressions
end

#u(expressions) ⇒ Object



611
612
613
# File 'lib/teepee/commander.rb', line 611

def u expressions
  html_tag :u, expressions
end

#user(user) ⇒ Object



615
616
617
618
619
620
621
# File 'lib/teepee/commander.rb', line 615

def user user
  if not user
    command_error "user: error: no user specified"
  else
    tb_href "users/#{user}", user.to_s
  end
end

#valid_uri?(uri) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/teepee/commander.rb', line 50

def valid_uri? uri
  (!! (u = URI.parse(uri))) and not u.scheme.nil?
rescue URI::InvalidURIError
  false
end