A simple (attribute=value) component simple = attr filtertype value filtertype = equal / approx / greater / less equal = “=” approx = “~=” greater = “>=” less = “<=”
Inverse of the FILTERTYPE_OP mapping (symbol -> name)
The valid values for filtertype
and the equivalent operator
A regex that matches any of the 'simple' operators.
The name of the item's attribute
The item's filter type (one of FILTERTYPE_OP.keys)
The item's value
Create a new 'simple' item filter component with the given
attribute
, filtertype
, and value
.
The filtertype
should be one of: :equal, :approx, :greater,
:less
# File lib/treequel/filter.rb, line 244
def initialize( attribute, value, filtertype=:equal )
self.log.debug "creating a new %s %s for %p and %p" %
[ filtertype, self.class.name, attribute, value ]
# Handle Sequel :attribute.identifier
attribute = attribute.value if attribute.respond_to?( :value )
filtertype = filtertype.to_s.downcase.to_sym
if FILTERTYPE_OP.key?( filtertype )
# no-op
elsif FILTEROP_NAMES.key?( filtertype.to_s )
filtertype = FILTEROP_NAMES[ filtertype.to_s ]
else
raise Treequel::ExpressionError,
"invalid simple item operator %p" % [ filtertype ]
end
@attribute = attribute
@value = value
@filtertype = filtertype
end
Parse a new SimpleItemComponent from
the specified literal
.
# File lib/treequel/filter.rb, line 227
def self::parse_from_string( literal )
parts = literal.split( FILTER_SPLIT_REGEXP, 3 )
unless parts.length == 3
raise Treequel::ExpressionError,
"unable to parse %p as a string literal" % [ literal ]
end
attribute, operator, value = *parts
filtertype = FILTEROP_NAMES[ operator ]
return self.new( attribute, value, filtertype )
end
The operator that is associated with the item's
filtertype
.
# File lib/treequel/filter.rb, line 282
def filtertype_op
FILTERTYPE_OP[ self.filtertype.to_sym ]
end
Stringify the component
# File lib/treequel/filter.rb, line 288
def to_s
# Escape all the filter metacharacters
escaped_val = self.value.to_s.gsub( UNESCAPED ) do |char|
'\' + char.unpack('C*').first.to_s(16)
end
return [ self.attribute, self.filtertype_op, escaped_val ].join
end