Class: Elus::Rule
- Inherits:
-
Object
- Object
- Elus::Rule
- Defined in:
- lib/elus/rule.rb
Instance Method Summary collapse
- #apply(other) ⇒ Object
-
#initialize(condition, yes, no = nil) ⇒ Rule
constructor
A new instance of Rule.
-
#name ⇒ Object
Returns full text name of this Rule.
- #to_s ⇒ Object
Constructor Details
#initialize(condition, yes, no = nil) ⇒ Rule
Returns a new instance of Rule.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/elus/rule.rb', line 3 def initialize(condition, yes, no=nil) @condition = Piece === condition ? condition : Piece.create(condition) raise Invalid, "Wrong condition" unless @condition @yes = Piece === yes ? yes : Piece.create(yes) raise Invalid, "Wrong yes branch" unless @yes @no = if Piece === no no elsif Piece.create(no) Piece.create(no) elsif no raise( Invalid, "Wrong no branch") else nil end end |
Instance Method Details
#apply(other) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/elus/rule.rb', line 26 def apply(other) piece = Piece === other ? other : Piece.create(other) return nil unless piece if piece == @condition piece * @yes else piece * @no end end |
#name ⇒ Object
Returns full text name of this Rule
20 21 22 |
# File 'lib/elus/rule.rb', line 20 def name "If last Piece is #{@condition.name} Piece, #{@yes.name} Piece is next" + if @no then ", otherwise #{@no.name} Piece is next" else "" end end |
#to_s ⇒ Object
24 |
# File 'lib/elus/rule.rb', line 24 def to_s; name end |