class ActiveHash::Matcher::Like

Attributes

match_type[RW]
pattern[RW]

Public Class Methods

backward(pattern) click to toggle source
# File lib/active_hash/matcher/like.rb, line 8
def backward(pattern)
  new("%#{pattern}")
end
forward(pattern) click to toggle source
# File lib/active_hash/matcher/like.rb, line 4
def forward(pattern)
  new("#{pattern}%")
end
new(pattern) click to toggle source
# File lib/active_hash/matcher/like.rb, line 19
def initialize(pattern)
  pattern = pattern.to_s
  self.pattern = pattern.gsub(/\A%|%\Z/, '')

  self.match_type = case pattern
                    when /\A%.*%\Z/ then :partial
                    when /\A%/      then :backward
                    when /%\Z/      then :forward
                    else                 :none
                    end
end
partial(pattern) click to toggle source
# File lib/active_hash/matcher/like.rb, line 12
def partial(pattern)
  new("%#{pattern}%")
end

Public Instance Methods

call(value) click to toggle source
# File lib/active_hash/matcher/like.rb, line 31
def call(value)
  match(value.to_s.upcase, pattern.upcase)
end

Private Instance Methods

match(value, pattern) click to toggle source
# File lib/active_hash/matcher/like.rb, line 37
def match(value, pattern)
  case match_type
  when :forward  then value.start_with?(pattern)
  when :backward then value.end_with?(pattern)
  when :partial  then value.include?(pattern)
  else                value == pattern
  end
end