Sha256: 58fb92e01c4a63f638ac430851234bb406d01e91805f07d26fb0b58447b8c9a8

Contents?: true

Size: 865 Bytes

Versions: 14

Compression:

Stored size: 865 Bytes

Contents

module With
  extend Sharing

  class Call
    include Implementation

    attr_reader :name, :block

    def initialize(name, conditions = {}, &block)
      raise "need to provide a block" unless block

      @name = name
      @conditions = conditions
      @block = block

      @conditions[:if] = With.condition(@conditions[:if]) if @conditions[:if].is_a?(Symbol)
    end

    def applies?(context)
      names = context.parents.map(&:name) << context.name
      With.applies?(names, @conditions)
    end

    def to_proc
      name, block, conditions = self.name, self.block, @conditions
      Proc.new {
        @_with_current_context = name
        if conditions[:if]
          instance_eval(&block) if instance_eval(&conditions[:if])
        else
          instance_eval(&block)
        end
      }
    end

    def call
      to_proc.call
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
adva-0.3.2 test/with/lib/with/call.rb
adva-0.3.1 test/with/lib/with/call.rb
adva-0.3.0 test/with/lib/with/call.rb
adva-0.2.4 test/with/lib/with/call.rb
adva-0.2.3 test/with/lib/with/call.rb
adva-0.2.2 test/with/lib/with/call.rb
adva-0.2.1 test/with/lib/with/call.rb
adva-0.2.0 test/with/lib/with/call.rb
adva-0.1.4 test/with/lib/with/call.rb
adva-0.1.3 test/with/lib/with/call.rb
adva-0.1.2 test/with/lib/with/call.rb
adva-0.1.1 test/with/lib/with/call.rb
adva-0.1.0 test/with/lib/with/call.rb
adva-0.0.1 test/with/lib/with/call.rb