Class: Goodcheck::Pattern::Token::VarPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/goodcheck/pattern.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns:, negated:) ⇒ VarPattern

Returns a new instance of VarPattern.



57
58
59
60
# File 'lib/goodcheck/pattern.rb', line 57

def initialize(patterns:, negated:)
  @patterns = patterns
  @negated = negated
end

Instance Attribute Details

#negatedObject (readonly)

Returns the value of attribute negated.



53
54
55
# File 'lib/goodcheck/pattern.rb', line 53

def negated
  @negated
end

#patternsObject (readonly)

Returns the value of attribute patterns.



54
55
56
# File 'lib/goodcheck/pattern.rb', line 54

def patterns
  @patterns
end

#typeObject

Returns the value of attribute type.



55
56
57
# File 'lib/goodcheck/pattern.rb', line 55

def type
  @type
end

Class Method Details

.emptyObject



92
93
94
# File 'lib/goodcheck/pattern.rb', line 92

def self.empty
  VarPattern.new(patterns: [], negated: false)
end

Instance Method Details

#cast(str) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/goodcheck/pattern.rb', line 62

def cast(str)
  case type
  when :int
    str.to_i
  when :float, :number
    str.to_f
  else
    str
  end
end

#test(str) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/goodcheck/pattern.rb', line 73

def test(str)
  return true if patterns.empty?

  unless negated
    patterns.any? {|pattern| test2(pattern, str) }
  else
    patterns.none? {|pattern| test2(pattern, str) }
  end
end

#test2(pattern, str) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/goodcheck/pattern.rb', line 83

def test2(pattern, str)
  case pattern
  when Numeric
    pattern == cast(str)
  else
    pattern === str
  end
end