class ActiveGraph::Shared::FilteredHash

Constants

VALID_HASH_INSTRUCTIONS
VALID_INSTRUCTIONS_TYPES
VALID_SYMBOL_INSTRUCTIONS

Attributes

base[R]
instructions[R]
instructions_type[R]

Public Class Methods

new(base, instructions) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
10 def initialize(base, instructions)
11   @base = base
12   @instructions = instructions
13   @instructions_type = instructions.class
14   validate_instructions!(instructions)
15 end

Public Instance Methods

filtered_base() click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
17 def filtered_base
18   case instructions
19   when Symbol
20     filtered_base_by_symbol
21   when Hash
22     filtered_base_by_hash
23   end
24 end

Private Instance Methods

filter(filter_keys, key) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
47 def filter(filter_keys, key)
48   filtering = key == :with
49   base.select { |k, _v| key?(filter_keys, k) == filtering }
50 end
filtered_base_by_hash() click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
37 def filtered_base_by_hash
38   behavior_key = instructions.keys.first
39   filter_keys = keys_array(behavior_key)
40   [filter(filter_keys, :with), filter(filter_keys, :without)]
41 end
filtered_base_by_symbol() click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
28 def filtered_base_by_symbol
29   case instructions
30   when :all
31     [base, {}]
32   when :none
33     [{}, base]
34   end
35 end
key?(filter_keys, key) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
43 def key?(filter_keys, key)
44   filter_keys.include?(key)
45 end
keys_array(key) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
52 def keys_array(key)
53   instructions[key].is_a?(Array) ? instructions[key] : [instructions[key]]
54 end
valid_hash_instructions() click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
75 def valid_hash_instructions
76   VALID_HASH_INSTRUCTIONS
77 end
valid_hash_instructions?(instructions) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
67 def valid_hash_instructions?(instructions)
68   valid_hash_instructions.include?(instructions.keys.first)
69 end
valid_symbol_instructions() click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
71 def valid_symbol_instructions
72   VALID_SYMBOL_INSTRUCTIONS
73 end
valid_symbol_instructions?(instructions) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
63 def valid_symbol_instructions?(instructions)
64   valid_symbol_instructions.include?(instructions)
65 end
validate_instructions!(instructions) click to toggle source
   # File lib/active_graph/shared/filtered_hash.rb
56 def validate_instructions!(instructions)
57   fail InvalidHashFilterType, "Filtering instructions #{instructions} are invalid" unless VALID_INSTRUCTIONS_TYPES.include?(instructions.class)
58   clazz = instructions_type.name.downcase
59   return if send(:"valid_#{clazz}_instructions?", instructions)
60   fail InvalidHashFilterType, "Invalid instructions #{instructions}, valid options for #{clazz}: #{send(:"valid_#{clazz}_instructions")}"
61 end