Class: CacheJSON::Worker::AllPermutations

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_json/worker.rb

Instance Method Summary collapse

Instance Method Details

#all_argument_permutations(klass) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/cache_json/worker.rb', line 62

def all_argument_permutations(klass)
  refresh_options = klass.cache_json_full_options[:refresh]
  if refresh_options
    all_combinations_with_fixed_points({}, refresh_options[:arguments])
  else
    []
  end
end

#all_cache_classesObject



55
56
57
58
59
60
# File 'lib/cache_json/worker.rb', line 55

def all_cache_classes
  # TODO: make this more efficient
  ObjectSpace.each_object(Class).select do |klass|
    klass.included_modules.include? CacheJSON::Base
  end
end

#all_combinations_with_fixed_points(fixed_points, full_hash) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cache_json/worker.rb', line 71

def all_combinations_with_fixed_points(fixed_points, full_hash)
  non_fixed_points = full_hash.dup.delete_if { |k, _| fixed_points.key?(k) }
  if non_fixed_points.empty?
    [fixed_points]
  else
    pivot_key = non_fixed_points.keys.first
    values_for_key(non_fixed_points, pivot_key).flat_map do |pivot_key_value|
      new_fixed_points = fixed_points.merge(Hash[pivot_key, pivot_key_value])
      all_combinations_with_fixed_points(new_fixed_points, full_hash)
    end
  end
end

#resultsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/cache_json/worker.rb', line 44

def results
  all_cache_classes.flat_map do |klass|
    all_argument_permutations(klass).map do |args|
      {
        klass: klass,
        args: args
      }
    end
  end
end

#values_for_key(hsh, key) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/cache_json/worker.rb', line 84

def values_for_key(hsh, key)
  pivot_key_values = hsh[key]
  if pivot_key_values.is_a?(Proc)
    pivot_key_values.call
  elsif pivot_key_values.is_a?(Range) || pivot_key_values.is_a?(Array)
    pivot_key_values
  else
    [pivot_key_values]
  end
end