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



183
184
185
186
# File 'lib/teepee/commander.rb', line 183

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

#*(*numbers) ⇒ Object



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

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

#**(*numbers) ⇒ Object



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

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

#+(*numbers) ⇒ Object




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

def + *numbers
  ensure_numeric numbers.inject 0, :+
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 -numbers.first
  else
    ensure_numeric numbers.reduce :-
  end
end

#/(*numbers) ⇒ Object



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

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

#acos(number) ⇒ Object



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

def acos number
  ensure_numeric Math.acos number
end

#acosh(number) ⇒ Object



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

def acosh number
  ensure_numeric Math.acosh number
end

#add_percentage(*numbers) ⇒ Object



188
189
190
191
# File 'lib/teepee/commander.rb', line 188

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

#asin(number) ⇒ Object



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

def asin number
  ensure_numeric Math.asin number
end

#asinh(number) ⇒ Object



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

def asinh number
  ensure_numeric Math.asinh number
end

#atan(number) ⇒ Object



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

def atan number
  ensure_numeric Math.atan number
end

#atanh(number) ⇒ Object



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

def atanh number
  ensure_numeric Math.atanh number
end

#b(expressions) ⇒ Object



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

def b expressions
  html_tag :b, expressions
end

#backquoteObject



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

def backquote
  "`"
end

#backslashObject



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

def backslash
  "\\"
end

#big(expressions) ⇒ Object



239
240
241
# File 'lib/teepee/commander.rb', line 239

def big expressions
  html_tag :big, expressions
end

#bookmarks_folder_id(id) ⇒ Object



243
244
245
# File 'lib/teepee/commander.rb', line 243

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

#boolean_and(expressions) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/teepee/commander.rb', line 247

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



259
260
261
# File 'lib/teepee/commander.rb', line 259

def boolean_nand expressions
  boolean_not boolean_and expressions
end

#boolean_nor(expressions) ⇒ Object



263
264
265
# File 'lib/teepee/commander.rb', line 263

def boolean_nor expressions
  boolean_not boolean_or expressions
end

#boolean_not(expression) ⇒ Object



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

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



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/teepee/commander.rb', line 277

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



289
290
291
# File 'lib/teepee/commander.rb', line 289

def boolean_xnor expressions
  boolean_not boolean_xor expressions
end

#boolean_xor(expressions) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/teepee/commander.rb', line 293

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



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

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



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

def cos angle
  ensure_numeric Math.cos angle
end

#cosh(number) ⇒ Object



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

def cosh number
  ensure_numeric Math.cosh number
end

#degrees2radians(degrees) ⇒ Object



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

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

#del(expressions) ⇒ Object



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

def del expressions
  html_tag :del, expressions
end

#do_operator(expressions) ⇒ Object



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

def do_operator expressions
  html_tag :span, expressions
end

#dollarObject



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

def dollar
  "$"
end

#eObject



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

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



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

def enumerate expressions
  html_tag :ol, expressions
end

#enumerate_lowercase(expressions) ⇒ Object



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

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

#enumerate_numeric(expressions) ⇒ Object



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

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

#enumerate_roman_lowercase(expressions) ⇒ Object



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

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

#enumerate_roman_uppercase(expressions) ⇒ Object



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

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

#enumerate_uppercase(expressions) ⇒ Object



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

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

#erf(number) ⇒ Object



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

def erf number
  ensure_numeric Math.erf number
end

#erfc(number) ⇒ Object



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

def erfc number
  ensure_numeric Math.erfc number
end

#false_constantObject



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

def false_constant
  "false"
end

#false_constant?(expression) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/teepee/commander.rb', line 149

def false_constant? expression
  expression.to_s == "false"
end

#folder_id(id) ⇒ Object



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

def folder_id id
  id_command_handler id, :Folder
end

#forum_id(id) ⇒ Object



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

def forum_id id
  id_command_handler id, :Forum
end

#gamma(number) ⇒ Object



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

def gamma number
  ensure_numeric Math.gamma number
end

#h1(expressions) ⇒ Object



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

def h1 expressions
  html_tag :h1, expressions
end

#h2(expressions) ⇒ Object



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

def h2 expressions
  html_tag :h2, expressions
end

#h3(expressions) ⇒ Object



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

def h3 expressions
  html_tag :h3, expressions
end

#h4(expressions) ⇒ Object



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

def h4 expressions
  html_tag :h4, expressions
end

#h5(expressions) ⇒ Object



418
419
420
# File 'lib/teepee/commander.rb', line 418

def h5 expressions
  html_tag :h5, expressions
end

#h6(expressions) ⇒ Object



422
423
424
# File 'lib/teepee/commander.rb', line 422

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



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

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

#iObject



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

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

#if_operator(expressions) ⇒ Object



430
431
432
433
434
435
436
437
# File 'lib/teepee/commander.rb', line 430

def if_operator expressions
  conditional, _, true_clause, _, false_clause = expressions
  if true_constant? conditional
    true_clause.to_html
  elsif false_clause
    false_clause.to_html
  end
end

#image(expressions) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/teepee/commander.rb', line 439

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



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

def it expressions
  html_tag :i, expressions
end

#item(expressions) ⇒ Object



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

def item expressions
  html_tag :li, expressions
end

#itemize(expressions) ⇒ Object



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

def itemize expressions
  html_tag :ul, expressions
end

#itemize_circle(expressions) ⇒ Object



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

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

#itemize_disc(expressions) ⇒ Object



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

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

#itemize_none(expressions) ⇒ Object



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

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

#itemize_square(expressions) ⇒ Object



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

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

#keyword_id(id) ⇒ Object



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

def keyword_id id
  id_command_handler id, :Keyword
end

#ld(n) ⇒ Object



490
491
492
# File 'lib/teepee/commander.rb', line 490

def ld n
  ensure_numeric Math.log2 n
end

#ldexp(fraction, exponent) ⇒ Object



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

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

#left_braceObject



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

def left_brace
  "{"
end

#left_bracketObject



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

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



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

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


510
511
512
513
514
515
516
517
518
519
520
# File 'lib/teepee/commander.rb', line 510

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


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

def link_id id
  id_command_handler id, :Link
end

#ln(number) ⇒ Object



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

def ln number
  ensure_numeric Math.log number
end

#log(base, number) ⇒ Object



530
531
532
533
534
535
536
537
# File 'lib/teepee/commander.rb', line 530

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



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

def log10 number
  ensure_numeric Math.log10 number
end

#mailto(email_address) ⇒ Object



543
544
545
546
547
548
549
550
# File 'lib/teepee/commander.rb', line 543

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



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

def mod *numbers
  ensure_numeric numbers.reduce :%
end

#nbsp(count) ⇒ Object



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

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

#note_id(id) ⇒ Object



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

def note_id id
  id_command_handler id, :Note
end

#percent_total(*numbers) ⇒ Object



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

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

#piObject



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

def pi
  Math::PI
end

#pipeObject



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

def pipe
  "|"
end

#radians2degrees(radians) ⇒ Object



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

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

#right_braceObject



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

def right_brace
  "}"
end

#right_bracketObject



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

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



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

def sin angle
  ensure_numeric Math.sin angle
end

#sinh(number) ⇒ Object



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

def sinh number
  ensure_numeric Math.sinh number
end

#small(expressions) ⇒ Object



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

def small expressions
  html_tag :small, expressions
end

#spaceObject



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

def space
  " "
end

#sqrt(number) ⇒ Object



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

def sqrt number
  ensure_numeric Math.sqrt number
end

#squiggleObject



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

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



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

def sub expressions
  html_tag :sub, expressions
end

#subtract_percentage(*numbers) ⇒ Object



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

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

#sup(expressions) ⇒ Object



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

def sup expressions
  html_tag :sup, expressions
end

#table(expressions) ⇒ Object



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

def table expressions
  html_tag :table, expressions
end

#table_data(expressions) ⇒ Object



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

def table_data expressions
  html_tag :td, expressions
end

#table_header(expressions) ⇒ Object



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

def table_header expressions
  html_tag :th, expressions
end

#table_row(expressions) ⇒ Object



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

def table_row expressions
  html_tag :tr, expressions
end

#tag_id(id) ⇒ Object



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

def tag_id id
  id_command_handler id, :Tag
end

#tan(angle) ⇒ Object



637
638
639
# File 'lib/teepee/commander.rb', line 637

def tan angle
  ensure_numeric Math.tan angle
end

#tanh(number) ⇒ Object



641
642
643
# File 'lib/teepee/commander.rb', line 641

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



645
646
647
# File 'lib/teepee/commander.rb', line 645

def true_constant
  "true"
end

#true_constant?(expression) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/teepee/commander.rb', line 145

def true_constant? expression
  expression.to_s == "true"
end

#tt(expressions) ⇒ Object



649
650
651
# File 'lib/teepee/commander.rb', line 649

def tt expressions
  html_tag :tt, expressions
end

#u(expressions) ⇒ Object



653
654
655
# File 'lib/teepee/commander.rb', line 653

def u expressions
  html_tag :u, expressions
end

#user(user) ⇒ Object



657
658
659
660
661
662
663
# File 'lib/teepee/commander.rb', line 657

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