Class: Courtier::ConfigFilter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/courtier/config_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration, criteria = {}) ⇒ ConfigFilter

Initialize new ConfigFilter.

Parameters:

  • List (Array<Config>)

    if Config instances.



12
13
14
15
16
17
18
19
20
21
# File 'lib/courtier/config_filter.rb', line 12

def initialize(configuration, criteria={})
  @configuration = configuration
  @criteria      = criteria

  @list = []

  configuration.each do |c|
    @list << c if c.match?(criteria)
  end
end

Instance Method Details

#[](subset) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/courtier/config_filter.rb', line 47

def [](subset)
  return method_missing(:[]) if profile
  if tool
    criteria = @criteria.dup
    criteria[:profile] = subset
  else
    criteria = @criteria.dup
    criteria[:tool] = subset
  end
  self.class.new(@configuration, criteria)
end

#call(*args) ⇒ Object

Call each config.



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

def call(*args)
  @list.each do |c|
    if c.profile?(RC.current_profile)
      c.call(*args)
    end
  end
end

#each(&block) ⇒ Object



62
63
64
65
66
# File 'lib/courtier/config_filter.rb', line 62

def each(&block)
  @list.each do
    block
  end
end

#profileObject



33
34
35
# File 'lib/courtier/config_filter.rb', line 33

def profile
  @criteria[:profile]
end

#profilesObject

Returns list of profiles.



40
41
42
# File 'lib/courtier/config_filter.rb', line 40

def profiles
  @list.map{ |c| c.profile }
end

#sizeObject



71
72
73
# File 'lib/courtier/config_filter.rb', line 71

def size
  @list.size
end

#to_proc(exec = false) ⇒ Object

Convert to Proc.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/courtier/config_filter.rb', line 89

def to_proc(exec=false)
  list = @list
  if exec
    Proc.new do |*args|
      list.each{ |c| instance_exec(*args, &c) }
    end
  else
    Proc.new do |*args|
      list.each{ |c| c.call(*args) }
    end
  end
end

#toolObject



26
27
28
# File 'lib/courtier/config_filter.rb', line 26

def tool
  @criteria[:tool]
end