Sha256: 04c4454dae6e7616887b61497238c4cabfefec475bbe4b16fab24c9d067dd274

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module QED
  require 'yaml'

  require 'facets/dir/ascend'

  require 'qed/parser'
  require 'qed/evaluator'

  # = Demo
  #
  class Demo

    #
    attr :applique

    # Demonstrandum file.
    attr :file

    #
    attr :mode

    #
    attr :scope

    # New Script
    def initialize(file, applique, options={})
      @file     = file
      @applique = applique.dup # localize copy of applique
      @scope    = options[:scope] || Scope.new(applique, file)
      @mode     = options[:mode]
      @binding  = @scope.__binding__
      #@loadlist = []
      #apply_environment
    end

    # One binding per script.
    def binding
      @binding #||= @scope.__binding__
    end

    # TODO: demo advice vs. applique advice
    def advice
      #@scope.__advice__
      @applique.__advice__
    end

    #
    def advise(signal, *args)
      advice.call(@scope, signal, *args)
    end

    # Expanded dirname of +file+.
    def directory
      @directory ||= File.expand_path(File.dirname(file))
    end

    # File basename less extension.
    def name
      @name ||= File.basename(file).chomp(File.extname(file))
    end

    #
    def evaluate(code, line)
      eval(code, @binding, @file, line)
      #@scope.module_eval(section.text, @file, section.line)
    end

    #
    #def source
    #  @source ||= (
    #    #case file
    #    #when /^http/
    #    #  ext  = File.extname(file).sub('.','')
    #    #  open(file)
    #    #else
    #      File.read(file)
    #    #end
    #  )
    #end

    # Parse script.
    # Retruns an abstract syntax tree.
    def parse
      Parser.new(file, :mode=>mode).parse
    end

    #
    def run(*observers)
      evaluator = Evaluator.new(self, *observers)
      evaluator.run
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qed-2.4.0 lib/qed/demo.rb