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



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

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

#*(*numbers) ⇒ Object



159
160
161
# File 'lib/teepee/commander.rb', line 159

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

#**(*numbers) ⇒ Object



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

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

#+(*numbers) ⇒ Object




147
148
149
# File 'lib/teepee/commander.rb', line 147

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

#-(*numbers) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/teepee/commander.rb', line 151

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

#/(*numbers) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/teepee/commander.rb', line 163

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

#acos(number) ⇒ Object



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

def acos number
  ensure_numeric Math.acos number
end

#acosh(number) ⇒ Object



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

def acosh number
  ensure_numeric Math.acosh number
end

#add_percentage(*numbers) ⇒ Object



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

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

#asin(number) ⇒ Object



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

def asin number
  ensure_numeric Math.asin number
end

#asinh(number) ⇒ Object



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

def asinh number
  ensure_numeric Math.asinh number
end

#atan(number) ⇒ Object



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

def atan number
  ensure_numeric Math.atan number
end

#atanh(number) ⇒ Object



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

def atanh number
  ensure_numeric Math.atanh number
end

#b(expressions) ⇒ Object



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

def b expressions
  html_tag :b, expressions
end

#backquoteObject



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

def backquote
  "`"
end

#backslashObject



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

def backslash
  "\\"
end

#big(expressions) ⇒ Object



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

def big expressions
  html_tag :big, expressions
end

#bookmarks_folder_id(id) ⇒ Object



235
236
237
# File 'lib/teepee/commander.rb', line 235

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

#boolean_and(expressions) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/teepee/commander.rb', line 239

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



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

def boolean_nand expressions
  boolean_not boolean_and expressions
end

#boolean_nor(expressions) ⇒ Object



255
256
257
# File 'lib/teepee/commander.rb', line 255

def boolean_nor expressions
  boolean_not boolean_or expressions
end

#boolean_not(expression) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/teepee/commander.rb', line 259

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



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/teepee/commander.rb', line 269

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



281
282
283
# File 'lib/teepee/commander.rb', line 281

def boolean_xnor expressions
  boolean_not boolean_xor expressions
end

#boolean_xor(expressions) ⇒ Object



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

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



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

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



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

def cos angle
  ensure_numeric Math.cos angle
end

#cosh(number) ⇒ Object



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

def cosh number
  ensure_numeric Math.cosh number
end

#degrees2radians(degrees) ⇒ Object



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

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

#del(expressions) ⇒ Object



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

def del expressions
  html_tag :del, expressions
end

#dollarObject



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

def dollar
  "$"
end

#eObject



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

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



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

def enumerate expressions
  html_tag :ol, expressions
end

#enumerate_lowercase(expressions) ⇒ Object



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

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

#enumerate_numeric(expressions) ⇒ Object



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

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

#enumerate_roman_lowercase(expressions) ⇒ Object



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

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

#enumerate_roman_uppercase(expressions) ⇒ Object



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

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

#enumerate_uppercase(expressions) ⇒ Object



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

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

#erf(number) ⇒ Object



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

def erf number
  ensure_numeric Math.erf number
end

#erfc(number) ⇒ Object



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

def erfc number
  ensure_numeric Math.erfc number
end

#false_constantObject



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

def false_constant
  "false"
end

#folder_id(id) ⇒ Object



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

def folder_id id
  id_command_handler id, :Folder
end

#forum_id(id) ⇒ Object



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

def forum_id id
  id_command_handler id, :Forum
end

#gamma(number) ⇒ Object



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

def gamma number
  ensure_numeric Math.gamma number
end

#h1(expressions) ⇒ Object



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

def h1 expressions
  html_tag :h1, expressions
end

#h2(expressions) ⇒ Object



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

def h2 expressions
  html_tag :h2, expressions
end

#h3(expressions) ⇒ Object



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

def h3 expressions
  html_tag :h3, expressions
end

#h4(expressions) ⇒ Object



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

def h4 expressions
  html_tag :h4, expressions
end

#h5(expressions) ⇒ Object



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

def h5 expressions
  html_tag :h5, expressions
end

#h6(expressions) ⇒ Object



410
411
412
# File 'lib/teepee/commander.rb', line 410

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



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

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

#iObject



414
415
416
# File 'lib/teepee/commander.rb', line 414

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



418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/teepee/commander.rb', line 418

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



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

def it expressions
  html_tag :i, expressions
end

#item(expressions) ⇒ Object



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

def item expressions
  html_tag :li, expressions
end

#itemize(expressions) ⇒ Object



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

def itemize expressions
  html_tag :ul, expressions
end

#itemize_circle(expressions) ⇒ Object



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

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

#itemize_disc(expressions) ⇒ Object



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

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

#itemize_none(expressions) ⇒ Object



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

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

#itemize_square(expressions) ⇒ Object



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

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

#keyword_id(id) ⇒ Object



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

def keyword_id id
  id_command_handler id, :Keyword
end

#ld(n) ⇒ Object



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

def ld n
  ensure_numeric Math.log2 n
end

#ldexp(fraction, exponent) ⇒ Object



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

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

#left_braceObject



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

def left_brace
  "{"
end

#left_bracketObject



481
482
483
# File 'lib/teepee/commander.rb', line 481

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



485
486
487
# File 'lib/teepee/commander.rb', line 485

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


489
490
491
492
493
494
495
496
497
498
499
# File 'lib/teepee/commander.rb', line 489

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


501
502
503
# File 'lib/teepee/commander.rb', line 501

def link_id id
  id_command_handler id, :Link
end

#ln(number) ⇒ Object



505
506
507
# File 'lib/teepee/commander.rb', line 505

def ln number
  ensure_numeric Math.log number
end

#log(base, number) ⇒ Object



509
510
511
512
513
514
515
516
# File 'lib/teepee/commander.rb', line 509

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



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

def log10 number
  ensure_numeric Math.log10 number
end

#mailto(email_address) ⇒ Object



522
523
524
525
526
527
528
529
# File 'lib/teepee/commander.rb', line 522

def mailto email_address
  email_address = email_address.to_s
  if valid_email_address? email_address
    html_tag :a, [email_address], {href: "mailto:#{email_address}"}
  else
    command_error "I'm not sure that's a valid email address."
  end
end

#mod(*numbers) ⇒ Object



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

def mod *numbers
  ensure_numeric numbers.reduce :%
end

#nbsp(count) ⇒ Object



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

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

#note_id(id) ⇒ Object



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

def note_id id
  id_command_handler id, :Note
end

#percent_total(*numbers) ⇒ Object



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

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

#piObject



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

def pi
  Math::PI
end

#pipeObject



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

def pipe
  "|"
end

#radians2degrees(radians) ⇒ Object



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

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

#right_braceObject



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

def right_brace
  "}"
end

#right_bracketObject



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

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



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

def sin angle
  ensure_numeric Math.sin angle
end

#sinh(number) ⇒ Object



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

def sinh number
  ensure_numeric Math.sinh number
end

#small(expressions) ⇒ Object



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

def small expressions
  html_tag :small, expressions
end

#spaceObject



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

def space
  " "
end

#sqrt(number) ⇒ Object



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

def sqrt number
  ensure_numeric Math.sqrt number
end

#squiggleObject



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

def squiggle
  "~"
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



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

def sub expressions
  html_tag :sub, expressions
end

#subtract_percentage(*numbers) ⇒ Object



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

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

#sup(expressions) ⇒ Object



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

def sup expressions
  html_tag :sup, expressions
end

#table(expressions) ⇒ Object



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

def table expressions
  html_tag :table, expressions
end

#table_data(expressions) ⇒ Object



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

def table_data expressions
  html_tag :td, expressions
end

#table_header(expressions) ⇒ Object



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

def table_header expressions
  html_tag :th, expressions
end

#table_row(expressions) ⇒ Object



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

def table_row expressions
  html_tag :tr, expressions
end

#tag_id(id) ⇒ Object



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

def tag_id id
  id_command_handler id, :Tag
end

#tan(angle) ⇒ Object



616
617
618
# File 'lib/teepee/commander.rb', line 616

def tan angle
  ensure_numeric Math.tan angle
end

#tanh(number) ⇒ Object



620
621
622
# File 'lib/teepee/commander.rb', line 620

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



624
625
626
# File 'lib/teepee/commander.rb', line 624

def true_constant
  "true"
end

#tt(expressions) ⇒ Object



628
629
630
# File 'lib/teepee/commander.rb', line 628

def tt expressions
  html_tag :tt, expressions
end

#u(expressions) ⇒ Object



632
633
634
# File 'lib/teepee/commander.rb', line 632

def u expressions
  html_tag :u, expressions
end

#user(user) ⇒ Object



636
637
638
639
640
641
642
# File 'lib/teepee/commander.rb', line 636

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

#valid_email_address?(email_address) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/teepee/commander.rb', line 141

def valid_email_address? email_address
  email_address =~ /\A[[:graph:]]+@[\w.]+\z/
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