Sha256: 51fc75b727adf92e54934ca6343ae97ddfb6a83283db9d0e88c2611394e7c8bf

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

$:.unshift(File.dirname(__FILE__)) unless
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

module DslBlockEngine
  VERSION = '0.0.4'

  class Context
    def attr_accessor *names
      names.each do |name|
        self.class.send :attr_accessor, name
      end
    end
  end
  
  class DslBlockEngine
    attr_accessor :categories

    def initialize
      @blocks = {}
      @categories = []
    end
    
    def load code
      self.instance_eval code
    end

    def blocks
      @blocks
    end

    def create_context *attrs
      o = Object.new

      def o.add_attribute name
        self.class.send :attr_accessor, name
      end

      attrs.each do |attr|
        o.add_attribute attr
      end
      
      o
    end

    def execute_in_context ctx, category, event
      b = @blocks[category][event]
      ctx.instance_eval &b
    end

    def method_missing name, *args, &block
      category = name
      event = args[0]
      super.method_missing(name, args, &block) unless @categories.include? category
      @blocks[category] ||= {}
      @blocks[category][event] = block
    end

  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dsl-block-engine-0.1.0 lib/dsl-block-engine.rb