Class: Livetext::Functions

Inherits:
Object show all
Defined in:
lib/livetext/functions.rb

Overview

Class Functions is where ‘$$func’ functions are stored dynamically… user-def AND pre-def??

Constant Summary collapse

Formats =
::Livetext::Standard::SimpleFormats

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFunctions

Returns a new instance of Functions.



18
19
20
21
# File 'lib/livetext/functions.rb', line 18

def initialize
  @live = nil
  @vars = nil
end

Class Attribute Details

.apiObject

Returns the value of attribute api.



12
13
14
# File 'lib/livetext/functions.rb', line 12

def api
  @api
end

.paramObject

kill this?



11
12
13
# File 'lib/livetext/functions.rb', line 11

def param
  @param
end

Instance Attribute Details

#liveObject

Instance variables for accessing the Livetext instance and its variables



16
17
18
# File 'lib/livetext/functions.rb', line 16

def live
  @live
end

#varsObject

Instance variables for accessing the Livetext instance and its variables



16
17
18
# File 'lib/livetext/functions.rb', line 16

def vars
  @vars
end

Instance Method Details

#apiObject

Simple method to access the class variable



24
25
26
# File 'lib/livetext/functions.rb', line 24

def api
  self.class.api
end

#b(param = nil) ⇒ Object



120
# File 'lib/livetext/functions.rb', line 120

def b(param=nil);    simple_format(param, :b); end

#bi(param = nil) ⇒ Object



124
# File 'lib/livetext/functions.rb', line 124

def bi(param=nil);   simple_format(param, :b, :i); end

#bis(param = nil) ⇒ Object



131
# File 'lib/livetext/functions.rb', line 131

def bis(param=nil);  simple_format(param, :b, :i, :s); end

#bit(param = nil) ⇒ Object



130
# File 'lib/livetext/functions.rb', line 130

def bit(param=nil);  simple_format(param, :b, :i, :t); end

#bits(param = nil) ⇒ Object



134
# File 'lib/livetext/functions.rb', line 134

def bits(param=nil); simple_format(param, :b, :i, :t, :s); end

#br(n = "1") ⇒ Object



100
101
102
103
# File 'lib/livetext/functions.rb', line 100

def br(n="1")
  n = n.to_i
  "<br>"*n
end

#bs(param = nil) ⇒ Object



126
# File 'lib/livetext/functions.rb', line 126

def bs(param=nil);   simple_format(param, :b, :s); end

#bt(param = nil) ⇒ Object



125
# File 'lib/livetext/functions.rb', line 125

def bt(param=nil);   simple_format(param, :b, :t); end

#bts(param = nil) ⇒ Object



132
# File 'lib/livetext/functions.rb', line 132

def bts(param=nil);  simple_format(param, :b, :t, :s); end

#code_lines(param = nil) ⇒ Object



46
47
48
# File 'lib/livetext/functions.rb', line 46

def code_lines(param = nil)
  $code_lines.to_i  # FIXME pleeease
end

#date(param = nil) ⇒ Object



78
79
80
# File 'lib/livetext/functions.rb', line 78

def date(param=nil)
  Time.now.strftime("%F")
end

#get_var(name) ⇒ Object

Helper method to access variables with fallback to global Livetext::Vars



29
30
31
32
33
# File 'lib/livetext/functions.rb', line 29

def get_var(name)
  return @vars.get(name) if @vars
  return @live&.vars.get(name) if @live&.vars
  Livetext::Vars[name]
end

#i(param = nil) ⇒ Object



121
# File 'lib/livetext/functions.rb', line 121

def i(param=nil);    simple_format(param, :i); end

#is(param = nil) ⇒ Object



128
# File 'lib/livetext/functions.rb', line 128

def is(param=nil);   simple_format(param, :i, :s); end

#isqrt(param = nil) ⇒ Object

FIXME Function parameters need to be fixed…



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/livetext/functions.rb', line 56

def isqrt(param = nil)      # "integer square root" - Just for testing
  arg = num = param         #  Takes any number
  if num.nil? || num.empty?
    arg = "NO PARAM"        # Just for error text
  end
  # Integer()/Float() can raise error
  num = num.include?(".") ? Float(num) : Integer(num)   
  # Returns truncated integer
  Math.sqrt(num).to_i       # user need not do to_s
rescue => err               # Malformed number? negative?
  "[Error evaluating $$isqrt(#{arg})]"
end

#it(param = nil) ⇒ Object



127
# File 'lib/livetext/functions.rb', line 127

def it(param=nil);   simple_format(param, :i, :t); end

#its(param = nil) ⇒ Object



133
# File 'lib/livetext/functions.rb', line 133

def its(param=nil);  simple_format(param, :i, :t, :s); end


95
96
97
98
# File 'lib/livetext/functions.rb', line 95

def link(param=nil)
  text, url = param.split("|", 2)  # reverse these?
  "<a style='text-decoration: none' href='#{url}'>#{text}</a>"
end

#ns(param = nil) ⇒ Object



50
51
52
# File 'lib/livetext/functions.rb', line 50

def ns(param = nil)
  "\b"
end

#pwd(param = nil) ⇒ Object



86
87
88
# File 'lib/livetext/functions.rb', line 86

def pwd(param=nil)
  ::Dir.pwd
end

#rand(param = nil) ⇒ Object



90
91
92
93
# File 'lib/livetext/functions.rb', line 90

def rand(param=nil)
  n1, n2 = param.split.map(&:to_i)
  ::Kernel.rand(n1..n2).to_s
end

#reverse(param = nil) ⇒ Object

again, just for testing



69
70
71
72
73
74
75
76
# File 'lib/livetext/functions.rb', line 69

def reverse(param=nil)      # again, just for testing
  arg = param
  if arg.nil? || arg.empty?
    return "(reverse: No parameter)" 
  else
    return arg.reverse
  end
end

#s(param = nil) ⇒ Object



123
# File 'lib/livetext/functions.rb', line 123

def s(param=nil);    simple_format(param, :s); end

#set_var(name, value) ⇒ Object

Helper method to set variables



36
37
38
39
40
41
42
43
44
# File 'lib/livetext/functions.rb', line 36

def set_var(name, value)
  if @vars
    @vars.set(name, value)
  elsif @live&.vars
    @live.vars.set(name, value)
  else
    Livetext::Vars[name] = value
  end
end

#simple_format(param = nil, *args) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/livetext/functions.rb', line 110

def simple_format(param=nil, *args)
  param ||= "NO PARAMETER"
  pairs = Formats.values_at(*args)
  str = param.dup
  pairs.reverse.each do |pair|
    str = "#{pair.first}#{str}#{pair.last}"
  end
  str
end

#t(param = nil) ⇒ Object



122
# File 'lib/livetext/functions.rb', line 122

def t(param=nil);    simple_format(param, :t); end

#time(param = nil) ⇒ Object



82
83
84
# File 'lib/livetext/functions.rb', line 82

def time(param=nil)
  Time.now.strftime("%T")
end

#ts(param = nil) ⇒ Object



129
# File 'lib/livetext/functions.rb', line 129

def ts(param=nil);   simple_format(param, :t, :s); end

#yt(param) ⇒ Object

FIXME uh, this is crap



105
106
107
108
# File 'lib/livetext/functions.rb', line 105

def yt(param)   # FIXME uh, this is crap
  param = self.class.param
  "https://www.youtube.com/watch?v=#{param}"
end