Class: Livetext::Handler::Mixin

Inherits:
Object
  • Object
show all
Includes:
GlobalHelpers, Livetext::Helpers
Defined in:
lib/livetext/handler/mixin.rb

Overview

Handle a .mixin

Constant Summary

Constants included from Livetext::Helpers

Livetext::Helpers::Comment, Livetext::Helpers::DollarDot, Livetext::Helpers::DotCmd, Livetext::Helpers::ESCAPING, Livetext::Helpers::Sigil, Livetext::Helpers::Space, Livetext::Helpers::TTY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GlobalHelpers

#check_disallowed, #check_file_exists, #cwd_root?, #grab_file, #search_upward

Methods included from Livetext::Helpers

#check_disallowed, #debug, #escape_html, #file_exists?, #find_file, #friendly_error, #get_name_data, #grab_file, #graceful_error, #handle_dollar_dot, #handle_dotcmd, #handle_scomment, #include_file, #invoke_dotcmd, #onoff, #process_file, #process_line, #read_variables, rx, #search_upward, #set_variables, #setfile, #setfile!, #setvar, #showme

Constructor Details

#initialize(name, parent) ⇒ Mixin

Livetext::Handler::Mixin



11
12
13
14
15
# File 'lib/livetext/handler/mixin.rb', line 11

def initialize(name, parent)     # Livetext::Handler::Mixin
  @name = name
  @file = find_file(name, ".rb", "plugin")
  parent.graceful_error FileNotFound(name) if @file.nil?
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/livetext/handler/mixin.rb', line 9

def file
  @file
end

Class Method Details

.get_module(filename, parent) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/livetext/handler/mixin.rb', line 17

def self.get_module(filename, parent)
  handler = self.new(filename, parent)
# STDERR.puts "handler was passed: #{filename}"
  modname, code = handler.read_mixin
# STDERR.puts "Modname was: #{modname}\n\n "
# STDERR.puts "Code was:\n=============\n#{code}\n==============\n "
  eval(code)   # Avoid in the future
# STDERR.puts "After eval"
  newmod = Object.const_get("::" + modname)
# STDERR.puts "After const_get"
  
  # Register functions from the mixin with the registry
  handler.register_mixin_functions(newmod, parent)
  
  newmod   # return actual module
end

Instance Method Details

#read_mixinObject



34
35
36
37
38
# File 'lib/livetext/handler/mixin.rb', line 34

def read_mixin
  modname = @name.gsub("/","_").capitalize
  meths = grab_file(@file)  # already has .rb?
  [modname, "module ::#{modname}; #{meths}\nend"]
end

#register_mixin_functions(module_obj, parent) ⇒ Object



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
# File 'lib/livetext/handler/mixin.rb', line 40

def register_mixin_functions(module_obj, parent)
  # Get all instance methods from the module
  methods = module_obj.instance_methods(false)
  
  methods.each do |method_name|
    # Create a lambda that calls the method on the parent (Livetext instance)
    function = ->(param) do
      # Check if the method expects parameters
      method = parent.method(method_name)
      if method.parameters.empty?
        parent.send(method_name)
      else
        parent.send(method_name, param)
      end
    end
    
    # Register with the function registry
    parent.function_registry.register_user(method_name.to_s, function, source: :mixin, filename: @file)
  end
  
  # Also look for methods defined in Livetext::Functions class
  # Get all methods from Livetext::Functions
  functions_class_methods = Livetext::Functions.instance_methods(false)
  
  functions_class_methods.each do |method_name|
    # Skip methods that are already built-in (defined in the original functions.rb)
    builtin_methods = [:code_lines, :ns, :isqrt, :reverse, :date, :time, :pwd, :rand, :link, :br, :yt, :simple_format, 
                      :b, :i, :t, :s, :bi, :bt, :bs, :it, :is, :ts, :bit, :bis, :bts, :its, :bits]
    next if builtin_methods.include?(method_name)
    
    # Create a lambda that calls the method on a new Livetext::Functions instance
    function = ->(param) do
      fobj = ::Livetext::Functions.new
      # Set the Livetext instance and its variables for access in functions
      fobj.live = parent
      fobj.vars = parent.vars
      method = fobj.method(method_name)
      if method.parameters.empty?
        fobj.send(method_name)
      else
        fobj.send(method_name, param)
      end
    end
    
    # Register with the function registry
    parent.function_registry.register_user(method_name.to_s, function, source: :mixin, filename: @file)
  end
end