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.



566
567
568
569
570
571
572
573
574
575
# File 'lib/flok/user_compiler.rb', line 566

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



629
630
631
632
633
634
635
636
637
638
# File 'lib/flok/user_compiler.rb', line 629

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.



563
564
565
# File 'lib/flok/user_compiler.rb', line 563

def controller
  @controller
end

#every_handlersObject

Returns the value of attribute every_handlers.



563
564
565
# File 'lib/flok/user_compiler.rb', line 563

def every_handlers
  @every_handlers
end

#nameObject

Returns the value of attribute name.



563
564
565
# File 'lib/flok/user_compiler.rb', line 563

def name
  @name
end

Instance Method Details

#every(seconds, str) ⇒ Object



618
619
620
621
622
623
624
# File 'lib/flok/user_compiler.rb', line 618

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



586
587
588
589
590
591
592
593
# File 'lib/flok/user_compiler.rb', line 586

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



577
578
579
580
# File 'lib/flok/user_compiler.rb', line 577

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

#on_entry_srcObject



582
583
584
# File 'lib/flok/user_compiler.rb', line 582

def on_entry_src
  return @_on_entry_src
end

#onsObject



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/flok/user_compiler.rb', line 595

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 do |e|
      @handling_event_named = e[:name]
      src = _macro(e[:src])
      @handling_event_named = nil
      {:name => e[:name], :src => src}
    end
  end

  @__ons_is_building = false
  return @__ons
end