Treequel::Filter::

SimpleItemComponent

class
Superclass
Treequel::Filter::ItemComponent
Included Modules
Treequel::Constants::Patterns

A simple (attribute=value) component simple = attr filtertype value filtertype = equal / approx / greater / less equal = “=” approx = “~=” greater = “>=” less = “<=”

Constants

FILTEROP_NAMES

Inverse of the FILTERTYPE_OP mapping (symbol -> name)

FILTERTYPE_OP

The valid values for filtertype and the equivalent operator

FILTER_SPLIT_REGEXP

A regex that matches any of the 'simple' operators.

Attributes

attribute[RW]

The name of the item's attribute

filtertype[RW]

The item's filter type (one of FILTERTYPE_OP.keys)

value[RW]

The item's value

Public Class Methods

anchor
new( attribute, value, filtertype=:equal )

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
anchor
parse_from_string( literal )

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

Public Instance Methods

anchor
filtertype_op()

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
anchor
to_s()

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