Class: Goodcheck::Pattern::Token::VarPattern
- Inherits:
-
Object
- Object
- Goodcheck::Pattern::Token::VarPattern
- Defined in:
- lib/goodcheck/pattern.rb
Instance Attribute Summary collapse
-
#negated ⇒ Object
readonly
Returns the value of attribute negated.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #cast(str) ⇒ Object
-
#initialize(patterns:, negated:) ⇒ VarPattern
constructor
A new instance of VarPattern.
- #test(str) ⇒ Object
- #test2(pattern, str) ⇒ Object
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
#negated ⇒ Object (readonly)
Returns the value of attribute negated.
53 54 55 |
# File 'lib/goodcheck/pattern.rb', line 53 def negated @negated end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
54 55 56 |
# File 'lib/goodcheck/pattern.rb', line 54 def patterns @patterns end |
#type ⇒ Object
Returns the value of attribute type.
55 56 57 |
# File 'lib/goodcheck/pattern.rb', line 55 def type @type end |
Class Method Details
.empty ⇒ Object
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 |