lib/rio/matchrecord.rb in rio-0.3.3 vs lib/rio/matchrecord.rb in rio-0.3.4
- old
+ new
@@ -39,11 +39,11 @@
module Match #:nodoc: all
module Record
class Base
def initialize(arg)
- @select_arg = arg
+ @select_arg = arg
end
def inspect
@select_arg.inspect
end
def val()
@@ -53,67 +53,81 @@
def match_none?() false end
end
class All < Base
def match?(val,recno) true end
def match_all?() true end
- def =~(record) true end
end
class None < Base
def match?(val,recno) false end
def match_none?() true end
- def =~(record) false end
end
class RegExp < Base
def match?(val,recno)
@select_arg.match(val)
end
- def =~(record)
- @select_arg =~ record
- end
end
class Range < Base
def match?(val,recno)
#p "match?(#{val},#{recno}) select_arg=#{@select_arg}"
@select_arg === recno
end
- def =~(record)
- #p "=~(#{record},#{record.recno}) select_arg=#{@select_arg}"
- @select_arg === record.recno
- end
end
class Fixnum < Base
def match?(val,recno)
#p "match?(#{val},#{recno}) select_arg=#{@select_arg}"
@select_arg === recno
end
- def =~(record)
- #p "=~(#{record},#{record.recno}) select_arg=#{@select_arg}"
- @select_arg === record.recno
- end
end
class Proc < Base
def initialize(arg,therio)
super(arg)
@therio = therio
end
def match?(val,recno)
#p "match?(#{val},#{recno}) select_arg=#{@select_arg}"
@select_arg.call(val,recno,@therio)
end
- def =~(record)
- @select_arg.call(record,record.recno,@therio)
- end
end
class Symbol < Base
def match?(val,recno)
#p "match?(#{val},#{recno}) select_arg=#{@select_arg}"
val.__send__(@select_arg)
end
- def =~(record)
- record.__send__(@select_arg)
+ end
+ class And < Base
+ def initialize(matches,therio)
+ list = []
+ matches.each do |arg|
+ list << Match::Record.create(therio,arg)
+ end
+ super(list)
+ @therio = therio
end
+ def match?(val,recno)
+ #p "match?(#{val},#{recno}) select_arg=#{@select_arg}"
+ @select_arg.all? { |sel| sel.match?(val,recno) }
+ end
end
+ def create(therio,arg)
+ case arg
+ when ::Regexp
+ Match::Record::RegExp.new(arg)
+ when ::Range
+ Match::Record::Range.new(arg)
+ when ::Proc
+ Match::Record::Proc.new(arg,therio)
+ when ::Symbol
+ Match::Record::Symbol.new(arg)
+ when ::Fixnum
+ Match::Record::Fixnum.new(arg)
+ when ::Array
+ Match::Record::And.new(arg,therio)
+ else
+ raise ArgumentError,"Argument must be a Regexp,Range,Fixnum,Proc, or Symbol"
+ end
+ end
+ module_function :create
end
end
module Match
module Record
@@ -160,24 +174,27 @@
end
@list = (newlist.empty? ? nil : newlist) if newlist.length != @list.length
@list.nil? ? nil : self
end
def create_sel(therio,arg)
- case arg
- when ::Regexp
- Match::Record::RegExp.new(arg)
- when ::Range
- Match::Record::Range.new(arg)
- when ::Proc
- Match::Record::Proc.new(arg,therio)
- when ::Symbol
- Match::Record::Symbol.new(arg)
- when ::Fixnum
- Match::Record::Fixnum.new(arg)
- else
- raise ArgumentError,"Argument must be a Regexp,Range,Fixnum,Proc, or Symbol"
- end
+ Match::Record.create(therio,arg)
+# case arg
+# when ::Regexp
+# Match::Record::RegExp.new(arg)
+# when ::Range
+# Match::Record::Range.new(arg)
+# when ::Proc
+# Match::Record::Proc.new(arg,therio)
+# when ::Symbol
+# Match::Record::Symbol.new(arg)
+# when ::Fixnum
+# Match::Record::Fixnum.new(arg)
+# when ::Array
+# Match::Record::And.new(arg)
+# else
+# raise ArgumentError,"Argument must be a Regexp,Range,Fixnum,Proc, or Symbol"
+# end
end
def match?(val,recno)
# !@list.nil? && (@list.empty? || @list.detect { |sel| sel.match?(val,recno) } || false) && true
return false if @list.nil?
return true if @list.empty?
@@ -188,13 +205,10 @@
}
#p "[SelList.match?] as:#{as.inspect} al:#{al.inspect}"
return as if al
return false
end
- def =~(el)
- !@list.nil? && (@list.empty? || @list.detect { |sel| sel =~ el } || false) && true
- end
def always?() !@list.nil? && @list.empty? end
def never?() @list.nil? end
end
@@ -250,19 +264,9 @@
#p "match?(#{val},#{recno}) #{self.inspect}"
return @always unless @always.nil?
as = nil
ok = ((!@sel.nil? && (as = @sel.match?(val,recno))) && !(!@rej.nil? && @rej.match?(val,recno)))
return (ok ? as : ok)
- end
- def =~(el)
- return @always unless @always.nil?
- (!@sel.nil? && (@sel =~ el)) && !(!@rej.nil? && (@rej =~ el))
-
- #yes = (!@sel.nil? && (@sel =~ el))
- #no = (!@rej.nil? && (@rej =~ el))
- #p "yes=#{yes} no=#{no} el=#{el}"
- #return yes && !no
- #(@sel =~ el) && !(@rej =~ el)
end
end
end
end
end