Sha256: 6a511ec3a8a6afec51baf6e1d6c6187c07b78b41cb7d95d02b144a09530dd92d

Contents?: true

Size: 708 Bytes

Versions: 3

Compression:

Stored size: 708 Bytes

Contents

require "canal/version"

module Canal
  class Context
    def initialize(functions=[])
      @functions = functions.freeze
    end

    def method_missing(name, *args, **kwargs, &block)
      Canal::Context.new(@functions + [[name, args, kwargs, block]])
    end

    def call(object)
      @functions.reduce(object) do |object, function|
        name, args, kwargs, block = function
        object.send(name, *args, **kwargs, &block)
      end
    end

    def to_proc
      ->(object) { call(object) }
    end

    %w{ to_s ! == <=> != === =~ !~ }.each do |method|
      define_method(method) do |*args|
        method_missing(method, *args)
      end
    end
  end
end

def canal
  Canal::Context.new
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
canal-0.0.6 lib/canal.rb
canal-0.0.5 lib/canal.rb
canal-0.0.4 lib/canal.rb