Sha256: 1968a5562b6c89a3b0353671491ff9aeb806651bbdee813cec09c229940a04d8

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Trestle
  class Hook
    attr_reader :name, :options, :block

    def initialize(name, options={}, &block)
      @name, @options, @block = name, options, block
    end

    def ==(other)
      other.is_a?(self.class) && name == other.name && options == other.options && block == other.block
    end

    def visible?(context)
      if options[:if]
        context.instance_exec(&options[:if])
      elsif options[:unless]
        !context.instance_exec(&options[:unless])
      else
        true
      end
    end

    def evaluate(context, *args)
      context.instance_exec(*args, &block)
    end

    class Set
      attr_reader :hooks

      def initialize
        @hooks = {}
      end

      def append(name, options={}, &block)
        hooks[name.to_s] ||= []
        hooks[name.to_s] << Hook.new(name.to_s, options, &block)
      end

      def any?(name)
        hooks.key?(name.to_s) && hooks[name.to_s].any?
      end

      def for(name)
        hooks.fetch(name.to_s) { [] }
      end

      def empty?
        hooks.empty?
      end

      def ==(other)
        other.is_a?(self.class) && hooks == other.hooks
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trestle-0.9.3 lib/trestle/hook.rb