Class: Outliers::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Verifications::Shared
Defined in:
lib/outliers/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Verifications::Shared

#equals?, #none_exist?

Constructor Details

#initialize(provider) ⇒ Collection

Returns a new instance of Collection.



33
34
35
36
37
# File 'lib/outliers/collection.rb', line 33

def initialize(provider)
  @targets   = []
  @provider = provider
  @logger   = Outliers.logger
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



9
10
11
# File 'lib/outliers/collection.rb', line 9

def provider
  @provider
end

#targetsObject

Returns the value of attribute targets.



10
11
12
# File 'lib/outliers/collection.rb', line 10

def targets
  @targets
end

Class Method Details

.filtersObject



20
21
22
# File 'lib/outliers/collection.rb', line 20

def self.filters
  []
end

.resource_classObject



24
25
26
27
# File 'lib/outliers/collection.rb', line 24

def self.resource_class
  array = self.to_s.gsub(/Collection$/, '').split('::')
  array.inject(Object) {|o,c| o.const_get c}
end

.to_humanObject



12
13
14
# File 'lib/outliers/collection.rb', line 12

def self.to_human
  (self.to_s.split('::') - ['Outliers', 'Resources']).map { |p| p.underscore }.join('_').downcase.gsub(/_collection$/, '')
end

.verificationsObject



16
17
18
# File 'lib/outliers/collection.rb', line 16

def self.verifications
   Outliers::Verifications::Shared.verifications + self.resource_class.verifications 
end

Instance Method Details

#each(&block) ⇒ Object



39
40
41
42
43
# File 'lib/outliers/collection.rb', line 39

def each &block  
  list.each do |resource|
    block.call resource
  end  
end

#exclude_by_key(exclusions) ⇒ Object



45
46
47
48
49
# File 'lib/outliers/collection.rb', line 45

def exclude_by_key(exclusions)
  @logger.info "Excluding the following resources: '#{exclusions.join(',')}'."
  save = list.reject {|u| exclusions.include? u.public_send key}
  @list = save
end

#filter(args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/outliers/collection.rb', line 51

def filter(args)
  name  = args.keys.first
  value = args.fetch name

  logger.info "Applying filter '#{name}' with value '#{value}'."

  unless self.public_methods.include? "filter_#{name}".to_sym
    raise Exceptions::UnknownFilter.new "Unknown filter '#{name}'."
  end

  filtered_list = self.public_send "filter_#{name}", value

  logger.warn "No resources match filter." unless filtered_list.any?

  @list = filtered_list
end

#keyObject



93
94
95
# File 'lib/outliers/collection.rb', line 93

def key
  resource_class.key
end

#listObject



89
90
91
# File 'lib/outliers/collection.rb', line 89

def list
  @list ||= load_all
end

#resource_classObject



97
98
99
# File 'lib/outliers/collection.rb', line 97

def resource_class
  self.class.resource_class
end

#to_sObject



29
30
31
# File 'lib/outliers/collection.rb', line 29

def to_s
  self.class.to_human
end

#verify(name, arguments = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/outliers/collection.rb', line 68

def verify(name, arguments={})
  name << "?" unless name =~ /^.*\?$/

  unless list.any?
    return { failing_resources: [], passing_resources: [] }
  end

  logger.info "Verifying '#{name}'."
  logger.debug "Target resources '#{list_by_key.join(', ')}'."

  unless verification_exists? name
    raise Exceptions::UnknownVerification.new "Unkown verification '#{name}'."
  end

  if collection_verification? name
    send_collection_verification name, arguments
  else
    send_resources_verification name, arguments
  end
end