Class: Jason::IncludesHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/jason/includes_helper.rb

Overview

Helper to provide other modules with information about the includes of a subscription

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(main_tree) ⇒ IncludesHelper

Returns a new instance of IncludesHelper.



6
7
8
9
10
# File 'lib/jason/includes_helper.rb', line 6

def initialize(main_tree)
  raise "Root must be hash" if !main_tree.is_a?(Hash)
  raise "Only one root key allowed" if main_tree.keys.size != 1
  @main_tree = main_tree
end

Instance Attribute Details

#main_treeObject

Returns the value of attribute main_tree.



4
5
6
# File 'lib/jason/includes_helper.rb', line 4

def main_tree
  @main_tree
end

Instance Method Details

#all_models(model_name = nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/jason/includes_helper.rb', line 26

def all_models(model_name = nil)
  model_name = model_name.presence || root_model
  assoc_name = get_assoc_name(model_name)
  tree = get_tree_for(assoc_name)
  [model_name, all_models_recursive(tree)].flatten.uniq.map(&:to_s).map(&:singularize)
end

#all_models_recursive(tree) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jason/includes_helper.rb', line 12

def all_models_recursive(tree)
  sub_models = if tree.is_a?(Hash)
    tree.map do |k,v|
      [k, all_models_recursive(v)]
    end
  elsif tree.is_a?(Array)
    tree.map do |v|
      all_models_recursive(v)
    end
  else
    tree
  end
end

#get_assoc_name(model_name, haystack = main_tree) ⇒ Object

assoc could be plural or not, so need to scan both.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jason/includes_helper.rb', line 38

def get_assoc_name(model_name, haystack = main_tree)
  if haystack.is_a?(Hash)
    haystack.each do |assoc_name, includes_tree|
      if model_name.pluralize == assoc_name.to_s.pluralize
        return assoc_name
      else
        found_assoc = get_assoc_name(model_name, includes_tree)
        return found_assoc if found_assoc
      end
    end
  elsif haystack.is_a?(Array)
    haystack.each do |element|
      if element.is_a?(String)
        if model_name.pluralize == element.pluralize
          return element
        end
      else
        found_assoc = get_assoc_name(model_name, element)
        return found_assoc if found_assoc
      end
    end
  else
    if model_name.pluralize == haystack.to_s.pluralize
      return haystack
    end
  end

  return nil
end

#get_tree_for(needle, assoc_name = nil, haystack = main_tree) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jason/includes_helper.rb', line 68

def get_tree_for(needle, assoc_name = nil, haystack = main_tree)
  return haystack if needle.to_s.pluralize == assoc_name.to_s.pluralize

  if haystack.is_a?(Hash)
    haystack.each do |assoc_name, includes_tree|
      found_haystack = get_tree_for(needle, assoc_name, includes_tree)
      return found_haystack if found_haystack.present?
    end
  elsif haystack.is_a?(Array)
    haystack.each do |includes_tree|
      found_haystack = get_tree_for(needle, nil, includes_tree)
      return found_haystack if found_haystack.present?
    end
  elsif haystack.is_a?(String)
    found_haystack = get_tree_for(needle, haystack, nil)
    return found_haystack if found_haystack.present?
  end

  return []
end

#in_sub(parent_model, child_model) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jason/includes_helper.rb', line 89

def in_sub(parent_model, child_model)
  tree = get_tree_for(parent_model)

  if tree.is_a?(Hash)
    return tree.keys.map(&:singularize).include?(child_model)
  elsif tree.is_a?(Array)
    tree.each do |element|
      if element.is_a?(String)
        return true if element.singularize == child_model
      elsif element.is_a?(Hash)
        return true if element.keys.map(&:singularize).include?(child_model)
      end
    end
  elsif tree.is_a?(String)
    return tree.singularize == child_model
  end

  return false
end

#root_modelObject



33
34
35
# File 'lib/jason/includes_helper.rb', line 33

def root_model
  main_tree.keys[0]
end