Class: Flok::UserCompilerAction

Inherits:
Object
  • Object
show all
Includes:
UserCompilerMacro
Defined in:
lib/flok/user_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UserCompilerMacro

#_macro

Constructor Details

#initialize(controller, name, ctx, &block) ⇒ UserCompilerAction

Returns a new instance of UserCompilerAction.



543
544
545
546
547
548
549
550
551
552
# File 'lib/flok/user_compiler.rb', line 543

def initialize controller, name, ctx, &block
  @controller = controller
  @name = name
  @ctx = ctx
  @_on_entry_src = ""
  @_ons = [] #Event handlers
  @every_handlers = []

  self.instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

You can def things in controller and use them as macros inside actions But these defs. live in the UserCompilerController instance and we need to delegate these calls to the controller that are not available in the action



601
602
603
604
605
606
607
608
609
610
# File 'lib/flok/user_compiler.rb', line 601

def method_missing method, *args, &block
  if macro = @controller.macros[method]
    #Call the macro in our context
    @current_action = name
      self.instance_eval(&macro)
    @current_action = nil
  else
    raise "No macro found named: #{method} for controller #{@controller.name} in action #{@name}"
  end
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



540
541
542
# File 'lib/flok/user_compiler.rb', line 540

def controller
  @controller
end

#every_handlersObject

Returns the value of attribute every_handlers.



540
541
542
# File 'lib/flok/user_compiler.rb', line 540

def every_handlers
  @every_handlers
end

#nameObject

Returns the value of attribute name.



540
541
542
# File 'lib/flok/user_compiler.rb', line 540

def name
  @name
end

Instance Method Details

#every(seconds, str) ⇒ Object



590
591
592
593
594
595
596
# File 'lib/flok/user_compiler.rb', line 590

def every seconds, str
  @every_handlers << {
    :name => "#{seconds}_sec_#{SecureRandom.hex[0..6]}",
    :ticks => seconds*4,
    :src => _macro(str)
  }
end

#on(name, js_src) ⇒ Object



563
564
565
566
567
568
569
570
# File 'lib/flok/user_compiler.rb', line 563

def on name, js_src
  #We need this guard because we run a two pass compile on the ons. When 'ons' is accessed, it is assumed that we are now
  #in the compilation phase and we build all the entries. This is because some macros in the ons source code requires
  #prior-knowledge of controller-level information like all possible events in all actions for hooks
  raise "Uh oh, you tried to add an event handler but we already assumed that compilation took place so we cached everything..." if @__ons_did_build or @__ons_is_building

  @_ons << {:name => name, :src => js_src}
end

#on_entry(js_src) ⇒ Object



554
555
556
557
# File 'lib/flok/user_compiler.rb', line 554

def on_entry js_src
  #returns a string
  @_on_entry_src = _macro(js_src)
end

#on_entry_srcObject



559
560
561
# File 'lib/flok/user_compiler.rb', line 559

def on_entry_src
  return @_on_entry_src
end

#onsObject



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/flok/user_compiler.rb', line 572

def ons 
  #Return the un-compiled version as some macros access this data and the real ons
  #would cause infinite recursion
  return @_ons if @__ons_is_building
  @__ons_is_building = true

  #We need this guard because we run a two pass compile on the ons. When 'ons' is accessed, it is assumed that we are now
  #in the compilation phase and we build all the entries. This is because some macros in the ons source code requires
  #prior-knowledge of controller-level information like all possible events in all actions for hooks
  unless @__ons_did_build
    @__ons_did_build = true
    @__ons = @_ons.map{|e| {:name => e[:name], :src => _macro(e[:src])}}
  end

  @__ons_is_building = false
  return @__ons
end