Sha256: e9c6c209fe7e3157336c288d49a7d2e7d5d2457ad48f35a423de7f4f4dd4612d

Contents?: true

Size: 971 Bytes

Versions: 1

Compression:

Stored size: 971 Bytes

Contents

class RbRules

  attr_reader :rules

  @rule_sets = Hash.new { |hash, key| hash[key] = new }

  def +(other)
    self.class.new do |builder|
      rules.each { |rule| builder.rule(rule) }
      if other.is_a?(self.class)
        other.rules.each { |rule| builder.rule(rule) }
      else
        builder.rule(other)
      end
    end
  end

  def self.[](key)
    @rule_sets[key]
  end

  def initialize(&block)
    @rules = []
    yield(self) if block_given?
  end

  def rule(name_or_rule, &block)
    if name_or_rule.respond_to? :call
      rules << name_or_rule
    else
      rules << Rule.new(name_or_rule, block)
    end
  end

  %w[all? none?].each do |method_name|
    define_method method_name do |*args|
      rules.public_send(method_name) { |rule| rule.call(*args) }
    end
  end

  def any?(*args)
    rules.find do |rule|
      rule.call(*args)
    end
  end

  class Rule < Struct.new(:name, :block)

    def call(*args); block.call(*args) end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rbrules-0.1.1 lib/rbrules.rb