Module: Fire::Model::Querying::ClassMethods

Defined in:
lib/model/querying/querying.rb

Instance Method Summary collapse

Instance Method Details

#direct_path_keys(params) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/model/querying/querying.rb', line 41

def direct_path_keys(params)
  res = []
  own_path_keys.each do |key|
    params[key] ? (res << key) : break
  end
  res
end

#direct_path_values(params, direct_keys) ⇒ Object



49
50
51
52
53
# File 'lib/model/querying/querying.rb', line 49

def direct_path_values(params, direct_keys)
  direct_keys.map do |dpk|
    path_value_param(params[dpk])
  end
end

#down_levels(root, levels_count) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/model/querying/querying.rb', line 23

def down_levels(root, levels_count)
  result = root.values

  levels_count.times do
    result = result.map(&:values).flatten.compact
  end

  result
end

#filter_opts(params, direct_keys) ⇒ Object



55
56
57
58
59
# File 'lib/model/querying/querying.rb', line 55

def filter_opts(params, direct_keys)
  direct_keys.each_with_object(params.clone) do |sk, res|
    res.delete(sk)
  end
end

#filter_result(rows, filter_opts, filter_condition) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/model/querying/querying.rb', line 33

def filter_result(rows, filter_opts, filter_condition)
  rows.map{|data| new(data) }.select do |model_object|
    not_filtered_by_attributes = model_object.has_data?(filter_opts)
    not_filtered_by_block = filter_condition ? filter_condition.(model_object) : true
    not_filtered_by_attributes && not_filtered_by_block
  end
end

#query(params = {}, &filter_condition) ⇒ Object Also known as: all



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/model/querying/querying.rb', line 8

def query(params={}, &filter_condition)
  direct_keys = direct_path_keys(params)
  full_path = ([ collection_name ] + direct_path_values(params, direct_keys)) * LEVEL_SEPARATOR

  response = connection.get(full_path).body
  return [] if response.nil?

  needed_levels = (all_path_keys - direct_keys - default_path_keys).count
  rows = down_levels(response, needed_levels)

  filter_result(rows, filter_opts(params, direct_keys), filter_condition)
end