Class: RbRules

Inherits:
Object
  • Object
show all
Defined in:
lib/rbrules.rb

Defined Under Namespace

Classes: Rule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ RbRules

Returns a new instance of RbRules.

Yields:

  • (_self)

Yield Parameters:

  • _self (RbRules)

    the object that the method was called on



22
23
24
25
# File 'lib/rbrules.rb', line 22

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

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



3
4
5
# File 'lib/rbrules.rb', line 3

def rules
  @rules
end

Class Method Details

.[](key) ⇒ Object



18
19
20
# File 'lib/rbrules.rb', line 18

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

Instance Method Details

#+(other) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/rbrules.rb', line 7

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

#any?(*args) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/rbrules.rb', line 41

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

#rule(name_or_rule, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rbrules.rb', line 27

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