Class: Logster::Pattern

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

Direct Known Subclasses

GroupingPattern, SuppressionPattern

Defined Under Namespace

Classes: PatternError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, store: Logster.store) ⇒ Pattern

Returns a new instance of Pattern.



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

def initialize(pattern, store: Logster.store)
  self.pattern = pattern
  @store = store
end

Class Method Details

.child_classesObject



15
16
17
# File 'lib/logster/pattern.rb', line 15

def self.child_classes
  @child_classes
end

.find(pattern, store: Logster.store) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/logster/pattern.rb', line 43

def self.find(pattern, store: Logster.store)
  pattern = parse_pattern(pattern).inspect
  return nil unless pattern
  pattern = find_all(raw: true, store: store).find { |p| p == pattern }
  return nil unless pattern
  new(pattern)
end

.find_all(raw: false, store: Logster.store) ⇒ Object



36
37
38
39
40
41
# File 'lib/logster/pattern.rb', line 36

def self.find_all(raw: false, store: Logster.store)
  patterns = store.get_patterns(set_name) || []
  patterns.map! { |p| parse_pattern(p) } unless raw
  patterns.compact!
  patterns
end

.inherited(subclass) ⇒ Object



10
11
12
13
# File 'lib/logster/pattern.rb', line 10

def self.inherited(subclass)
  @child_classes << subclass
  super
end

.parse_pattern(string) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logster/pattern.rb', line 23

def self.parse_pattern(string)
  return string if Regexp === string
  return unless String === string
  if string[0] == "/"
    return unless string =~ %r{/(.+)/(.*)}
    string = $1
    flag = Regexp::IGNORECASE if $2 && $2.include?("i")
  end
  Regexp.new(string, flag)
rescue RegexpError
  nil
end

.set_nameObject



19
20
21
# File 'lib/logster/pattern.rb', line 19

def self.set_name
  raise "Please override the `set_name` method and specify and a name for this set"
end

.valid?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/logster/pattern.rb', line 51

def self.valid?(pattern)
  return false unless Regexp === pattern
  pattern_size = pattern.inspect.size
  pattern_size > 3 && pattern_size < 500
end

Instance Method Details

#destroyObject



83
84
85
# File 'lib/logster/pattern.rb', line 83

def destroy
  @store.remove_pattern(set_name, self.to_s)
end

#modify(new_pattern) ⇒ Object

Raises:



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

def modify(new_pattern)
  new_pattern = self.class.parse_pattern(new_pattern)
  raise PatternError.new unless self.class.valid?(new_pattern)
  destroy
  self.pattern = new_pattern
  save
end

#patternObject



87
88
89
# File 'lib/logster/pattern.rb', line 87

def pattern
  @pattern
end

#save(args = {}) ⇒ Object



70
71
72
73
# File 'lib/logster/pattern.rb', line 70

def save(args = {})
  ensure_valid!
  @store.insert_pattern(set_name, self.to_s)
end

#to_sObject



66
67
68
# File 'lib/logster/pattern.rb', line 66

def to_s
  pattern.inspect
end

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/logster/pattern.rb', line 62

def valid?
  self.class.valid?(pattern)
end