Sha256: 2d47d19c9d4f27270736c9565304cc460fef60dc8477cecd6ef933d8ed51473d

Contents?: true

Size: 1.98 KB

Versions: 21

Compression:

Stored size: 1.98 KB

Contents

#Compile a controller ruby file into a javascript string

require 'erb'
module Flok
  module ServicesCompiler
    #Compile a ruby file containing flok controller definitions (from the services)
    def self.compile rb_src
      #Execute code in this context, the context will hold all the information
      #that is used to then generate code
      context = ServicesCompilerContext.new
      context.instance_eval(rb_src, __FILE__, __LINE__)

      @src = ""
      services_erb = File.read File.join(File.dirname(__FILE__), "./service_compiler_templates/services.js.erb")
      services_renderer = ERB.new(services_erb)
      @src << services_renderer.result(context.get_binding)

      #puts @src

      return @src
    end
  end
end

#Compiler executes all rb code inside this context
module Flok
  class ServicesCompilerContext
    attr_accessor :services

    def initialize
      @services = []
    end

    def get_binding
      return binding
    end

    def service name, &block
      @services << Service.new(name, &block)
    end
  end

  class Service
    attr_accessor :name
    def initialize name, &block
      @name = name
      @block = block

      self.instance_eval(&block)
    end

    def get_on_init
      return @on_init
    end

    def get_on_request
      return @on_request
    end

    def on_init str
      @on_init = macro(str)
    end

    def on_request str
      @on_request = macro(str)
    end

    def macro text
      out = StringIO.new

      text.split("\n").each do |l|
        ##Request(vc_name, spot_name, context) macro
        #if l =~ /Request/
          #l.strip!
          #l.gsub!(/Request\(/, "")
          #l.gsub! /\)$/, ""
          #l.gsub! /\);$/, ""
          #o = l.split(",").map{|e| e.strip}

          #service_name = o.shift.gsub(/"/, "")
          #info = o.shift.gsub(/"/, "")
          #event_name = o.shift.gsub(/"/, "")

          #out << %{
          #}
        #else
          out.puts l
        #end
      end

      return out.string
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
flok-0.0.36 lib/flok/services_compiler.rb
flok-0.0.35 lib/flok/services_compiler.rb
flok-0.0.34 lib/flok/services_compiler.rb
flok-0.0.33 lib/flok/services_compiler.rb
flok-0.0.32 lib/flok/services_compiler.rb
flok-0.0.31 lib/flok/services_compiler.rb
flok-0.0.30 lib/flok/services_compiler.rb
flok-0.0.29 lib/flok/services_compiler.rb
flok-0.0.28 lib/flok/services_compiler.rb
flok-0.0.27 lib/flok/services_compiler.rb
flok-0.0.26 lib/flok/services_compiler.rb
flok-0.0.25 lib/flok/services_compiler.rb
flok-0.0.24 lib/flok/services_compiler.rb
flok-0.0.23 lib/flok/services_compiler.rb
flok-0.0.21 lib/flok/services_compiler.rb
flok-0.0.20 lib/flok/services_compiler.rb
flok-0.0.19 lib/flok/services_compiler.rb
flok-0.0.18 lib/flok/services_compiler.rb
flok-0.0.17 lib/flok/services_compiler.rb
flok-0.0.16 lib/flok/services_compiler.rb