module Eco module Language class MatchModifier < Eco::Language::Models::Modifier VALUE_MODE = [ :value, :is, ] PATTERN_MODE = [ :pattern, :contains, :regexp ] REVERSE_MODE = [ :reverse, :at_to_value, :<< ] NON_REVERSE_MODE = [ :normal, :value_to_at, :non_reverse, :>> ] ALL_MODE = [ :all, :every ] ANY_MODE = [ :any, :some ] AND_MODE = [ :AND, :and, :&, :intersection, :intersect ] OR_MODE = [ :OR, :or, :|, :union ] CASE_MODE = [ :c, :case_sensitive, :case, :sensitive ] INCASE_MODE = [ :i, :case_insensitive, :insensitive ] YES_MODE = [ :==, :YES, :yes, :true, :include, :eq ] NOT_MODE = [ :!, :NOT, :not, :false, :exclude, :diff ] def to_regex(value) case when value.is_a?(Array) return value.map { |v| self.to_regex(v) } when value.is_a?(Regexp) return value when value.is_a?(String) esub = Regexp.escape(value) if self.insensitive? return /#{esub}/i else return /#{esub}/ end end end def reset_pattern self < self.mode - (PATTERN_MODE | VALUE_MODE) end def reset_reverse self < self.mode - (REVERSE_MODE | NON_REVERSE_MODE) end def reset_every self < self.mode - (ALL_MODE | ANY_MODE) end def reset_and self < self.mode - (AND_MODE | OR_MODE) end def reset_case self < self.mode - (INCASE_MODE | CASE_MODE) end def reset_not self < self.mode - (NOT_MODE | YES_MODE) end def pattern self.push(:pattern) end def value self.push(:value) end def reverse self.push(:reverse) end def normal self.push(:normal) end def every self.push(:every) end def all every end def some self.push(:some) end def any some end def and self.push(:and) end def or self.push(:or) end def case self.push(:sensitive) end def insensitive self.push(:insensitive) end def eq self.push(:eq) end def not self.push(:not) end def pattern? mode.any? { |m| PATTERN_MODE.include?(m) } end def value? mode.any? { |m| VALUE_MODE.include?(m) } end def reverse? mode.any? { |m| REVERSE_MODE.include?(m) } end def non_reverse? mode.any? { |m| NON_REVERSE_MODE.include?(m) } end def any? mode.any? { |m| ANY_MODE.include?(m) } end def some? self.any? end def all? mode.any? { |m| ALL_MODE.include?(m) } end def every? self.all? end def and? mode.any? { |m| AND_MODE.include?(m) } end def or? mode.any? { |m| OR_MODE.include?(m) } end def insensitive? mode.any? { |m| INCASE_MODE.include?(m) } end def case_sensitive? mode.any? { |m| CASE_MODE.include?(m) } end def not? mode.any? { |m| NOT_MODE.include?(m) } end def yes? mode.any? { |m| YES_MODE.include?(m) } end protected def resolve_mode modifiers = self.to_a modifiers = resolve(modifiers, PATTERN_MODE | VALUE_MODE) modifiers = resolve(modifiers, REVERSE_MODE | NON_REVERSE_MODE) modifiers = resolve(modifiers, ALL_MODE | ANY_MODE) modifiers = resolve(modifiers, AND_MODE | OR_MODE) modifiers = resolve(modifiers, INCASE_MODE | CASE_MODE) modifiers = resolve(modifiers, NOT_MODE | YES_MODE) modifiers end end end end