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.



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

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

Class Method Details

.child_classesObject



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

def self.child_classes
  @child_classes
end

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



46
47
48
49
50
51
52
# File 'lib/logster/pattern.rb', line 46

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



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

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

.inherited(subclass) ⇒ Object



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

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

.parse_pattern(string) ⇒ Object



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

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

.set_nameObject



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

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)


54
55
56
57
58
# File 'lib/logster/pattern.rb', line 54

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



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

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

#modify(new_pattern) ⇒ Object

Raises:



78
79
80
81
82
83
84
# File 'lib/logster/pattern.rb', line 78

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



90
91
92
# File 'lib/logster/pattern.rb', line 90

def pattern
  @pattern
end

#save(args = {}) ⇒ Object



73
74
75
76
# File 'lib/logster/pattern.rb', line 73

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

#to_sObject



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

def to_s
  pattern.inspect
end

#valid?Boolean

Returns:

  • (Boolean)


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

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