Sha256: 2418cfd1660437a71f517ca576e40b54763045581d28d7089a05b0db0bab8770

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8

module Selector

  # The condition checks if a value matches the regexp
  #
  # @example (see #[])
  #
  class Regexp < Condition

    # @!method initialize(regexp)
    # Initializes the condition with the regexp
    #
    # @param [::Regexp] regexp
    #
    def initialize(_)
      super
    end

    # Checks if the stringified value matches the regexp
    #
    # @example
    #   condition = Selector::Regexp.new /1/
    #   condition[11] # => true
    #   condition[22] # => false
    #
    # @param (see Selector::Condition#[])
    #
    # @return (see Selector::Condition#[])
    #
    def [](value)
      value.to_s[attribute] ? true : false
    end

    # Creates an OR composition
    #
    # If other value is a regexp, then creates modified regexp to avoid nesting
    #
    # @param (see Selector::Composition#|)
    #
    # @return (see Selector::Composition#|)
    #
    def |(other)
      return super unless other.instance_of? Regexp
      Regexp.new(/(#{attribute})|(#{other.attribute})/)
    end

  end # class Regexp < Condition

end # module Selector

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
selector-0.0.3 lib/selector/regexp.rb
selector-0.0.2 lib/selector/regexp.rb