Class: Console::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/console/filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, enabled: nil, **options) ⇒ Filter

Returns a new instance of Filter.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/console/filter.rb', line 58

def initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, enabled: nil, **options)
	@output = output
	@verbose = verbose
	@level = level
	
	@subjects = {}
	
	@options = options
	
	if enabled
		enabled.each{|name| enable(name)}
	end
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



82
83
84
# File 'lib/console/filter.rb', line 82

def level
  @level
end

#optionsObject

Returns the value of attribute options.



86
87
88
# File 'lib/console/filter.rb', line 86

def options
  @options
end

#outputObject

Returns the value of attribute output.



80
81
82
# File 'lib/console/filter.rb', line 80

def output
  @output
end

#subjectsObject (readonly)

Returns the value of attribute subjects.



84
85
86
# File 'lib/console/filter.rb', line 84

def subjects
  @subjects
end

#verboseObject (readonly)

Returns the value of attribute verbose.



81
82
83
# File 'lib/console/filter.rb', line 81

def verbose
  @verbose
end

Class Method Details

.[](**levels) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/console/filter.rb', line 27

def self.[] **levels
	klass = Class.new(self)
	min_level, max_level = levels.values.minmax
	
	klass.instance_exec do
		const_set(:LEVELS, levels)
		const_set(:MINIMUM_LEVEL, min_level)
		const_set(:MAXIMUM_LEVEL, max_level)
		
		levels.each do |name, level|
			const_set(name.to_s.upcase, level)
			
			define_method(name) do |subject = nil, *arguments, **options, &block|
				if self.enabled?(subject, level)
					self.call(subject, *arguments, severity: name, **options, **@options, &block)
				end
			end
			
			define_method("#{name}!") do
				@level = level
			end
			
			define_method("#{name}?") do
				@level <= level
			end
		end
	end
	
	return klass
end

Instance Method Details

#all!Object



105
106
107
# File 'lib/console/filter.rb', line 105

def all!
	@level = self.class::MINIMUM_LEVEL - 1
end

#call(*arguments, **options, &block) ⇒ Object



141
142
143
# File 'lib/console/filter.rb', line 141

def call(*arguments, **options, &block)
	@output.call(*arguments, **options, &block)
end

#disable(subject) ⇒ Object

Disable specific logging for the specific class.

Parameters:

  • name (String, Class)

    The class to disable.



133
134
135
136
137
138
139
# File 'lib/console/filter.rb', line 133

def disable(subject)
	unless subject.is_a? Class
		subject = subject.class
	end
	
	@subjects.delete(subject)
end

#enable(subject, level = self.class::MINIMUM_LEVEL) ⇒ Object

Enable specific log level for the given class.

Parameters:

  • name (String, Class)

    The class to enable.



123
124
125
126
127
128
129
# File 'lib/console/filter.rb', line 123

def enable(subject, level = self.class::MINIMUM_LEVEL)
	unless subject.is_a?(Class)
		subject = subject.class
	end
	
	@subjects[subject] = level
end

#enabled?(subject, level = self.class::MINIMUM_LEVEL) ⇒ Boolean

You can enable and disable logging for classes. This function checks if logging for a given subject is enabled.

Parameters:

  • subject (Object)

    the subject to check.

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
# File 'lib/console/filter.rb', line 111

def enabled?(subject, level = self.class::MINIMUM_LEVEL)
	if specific_level = @subjects[subject.class]
		return level >= specific_level
	end
	
	if level >= @level
		return true
	end
end

#off!Object



101
102
103
# File 'lib/console/filter.rb', line 101

def off!
	@level = self.class::MAXIMUM_LEVEL + 1
end

#verbose!(value = true) ⇒ Object



96
97
98
99
# File 'lib/console/filter.rb', line 96

def verbose!(value = true)
	@verbose = value
	@output.verbose!(value)
end

#with(level: @level, verbose: @verbose, **options) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/console/filter.rb', line 72

def with(level: @level, verbose: @verbose, **options)
	dup.tap do |logger|
		logger.level = level
		logger.verbose! if verbose
		logger.options = @options.merge(options)
	end
end