Class: Dump::Env::Filter

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

Overview

Filter strings by simple pattern:

'a,b,c' will pass only 'a', 'b' and 'c'
'-a,b,c' will pass everything except 'a', 'b' and 'c'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, splitter = nil) ⇒ Filter

Returns a new instance of Filter.



10
11
12
13
14
15
16
17
18
# File 'lib/dump/env/filter.rb', line 10

def initialize(s, splitter = nil)
  if s
    s = s.dup
    @invert = !!s.sub!(/^-/, '')
    @values = s.split(splitter || ',').map(&:strip).map(&:downcase).uniq.select(&:present?)
  else
    @transparent = true
  end
end

Instance Attribute Details

#invertObject (readonly)

Returns the value of attribute invert.



9
10
11
# File 'lib/dump/env/filter.rb', line 9

def invert
  @invert
end

#transparentObject (readonly)

Returns the value of attribute transparent.



9
10
11
# File 'lib/dump/env/filter.rb', line 9

def transparent
  @transparent
end

#valuesObject (readonly)

Returns the value of attribute values.



9
10
11
# File 'lib/dump/env/filter.rb', line 9

def values
  @values
end

Instance Method Details

#custom_pass?(&block) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/dump/env/filter.rb', line 24

def custom_pass?(&block)
  transparent || (invert ^ values.any?(&block))
end

#pass?(value) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/dump/env/filter.rb', line 20

def pass?(value)
  transparent || (invert ^ values.include?(value.to_s.downcase))
end