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(string, splitter = nil) ⇒ Filter

Returns a new instance of Filter.



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

def initialize(string, splitter = nil)
  if string
    string = string.dup
    @invert = !!string.sub!(/^-/, '')
    @values = string.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.



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

def invert
  @invert
end

#transparentObject (readonly)

Returns the value of attribute transparent.



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

def transparent
  @transparent
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#custom_pass?(&block) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pass?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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