class MacAppSync::Defaults::Filter

Public Class Methods

filter(data, opts) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 7
def filter(data, opts)
  user_filter(global_filter(data), opts)
end

Private Class Methods

build_filter(opts) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 27
def build_filter(opts)
  if opts["only"]
    include_filter(opts)
  else
    exclude_filter(opts)
  end
end
exclude_filter(opts) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 40
def exclude_filter(opts)
  excludes = wrap(opts["exclude"]).to_set
  exclude_patterns = wrap(opts["exclude_patterns"]).map { |pattern| Regexp.new(pattern) }

  lambda do |key|
    excludes.include?(key) || exclude_patterns.any? { |pattern| key =~ pattern }
  end
end
global_exclude_keys() click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 59
def global_exclude_keys
  @global_exclude_keys ||= global_exclusions.fetch("keys").to_set
end
global_exclude_patterns() click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 63
def global_exclude_patterns
  @global_exclude_patterns ||= global_exclusions.fetch("patterns").map do |pattern|
    Regexp.new(pattern)
  end
end
global_exclusions() click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 69
def global_exclusions
  @global_exclusions ||= YAML.load_file(MacAppSync.gem_root.join("config", "global_defaults_exclusions.yml"))
end
global_filter(data) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 13
def global_filter(data)
  data.reject do |key, _value|
    global_exclude_keys.include?(key) ||
    global_exclude_patterns.any? { |pattern| key =~ pattern }
  end
end
include_filter(opts) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 35
def include_filter(opts)
  only = wrap(opts["only"]).to_set
  ->(key) { !only.include?(key) }
end
user_filter(data, opts) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 20
def user_filter(data, opts)
  return data if opts.empty?

  should_exclude = build_filter(opts)
  data.reject { |key, _| should_exclude.call(key) }
end
wrap(object) click to toggle source
# File lib/mac_app_sync/defaults/filter.rb, line 49
def wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end