class Nucop::Helpers::CopCounter
Public Class Methods
count(all_cops, cops_or_departments)
click to toggle source
# File lib/nucop/helpers/cop_counter.rb, line 10 def self.count(all_cops, cops_or_departments) new(all_cops).count(cops_or_departments) end
new(cops)
click to toggle source
# File lib/nucop/helpers/cop_counter.rb, line 14 def initialize(cops) @cops_by_department = group_by_department(cops) end
Public Instance Methods
count(cops_or_departments)
click to toggle source
# File lib/nucop/helpers/cop_counter.rb, line 18 def count(cops_or_departments) cops_or_departments .map do |cop_or_department| if department?(cop_or_department) @cops_by_department.fetch(cop_or_department, []).length else 1 end end .reduce(0, &:+) end
Private Instance Methods
department(cop_name)
click to toggle source
# File lib/nucop/helpers/cop_counter.rb, line 46 def department(cop_name) cop_name.split("/").first end
department?(cop_name)
click to toggle source
# File lib/nucop/helpers/cop_counter.rb, line 42 def department?(cop_name) !cop_name.include?("/") end
group_by_department(cop_names)
click to toggle source
# File lib/nucop/helpers/cop_counter.rb, line 32 def group_by_department(cop_names) cop_names.group_by do |cop_name| if department?(cop_name) raise "Expected fully-qualified cops by name (i.e. Department/Cop). Got: #{cop_name}" end department(cop_name) end end