Class: ObjectGraph::ObjectGraph

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/ASModel_object_graph.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ObjectGraph

Returns a new instance of ObjectGraph.



68
69
70
71
72
# File 'backend/app/model/ASModel_object_graph.rb', line 68

def initialize(hash = {})
  @graph = Hash[hash.map {|model, ids|
                  [model, Set.new(ids)]
                }]
end

Instance Method Details

#add_objects(model, *ids) ⇒ Object



74
75
76
77
78
79
80
81
# File 'backend/app/model/ASModel_object_graph.rb', line 74

def add_objects(model, *ids)
  flat_ids = ids.flatten
  return if flat_ids.empty?
  @graph[model] ||= Set.new
  @graph[model].merge(flat_ids)

  self
end

#changed_since?(version) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'backend/app/model/ASModel_object_graph.rb', line 87

def changed_since?(version)
  self.version != version
end

#each(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'backend/app/model/ASModel_object_graph.rb', line 95

def each(&block)
  if block_given?
    @graph.keys.each do |model|
      block.call(model, @graph[model].to_a)
    end
  else
    Enumerator.new do |y|
      @graph.map {|model, ids|
        y << [model, ids.to_a]
      }
    end
  end
end

#ids_for(model) ⇒ Object



91
92
93
# File 'backend/app/model/ASModel_object_graph.rb', line 91

def ids_for(model)
  @graph[model] ? @graph[model].to_a : []
end

#modelsObject



109
110
111
# File 'backend/app/model/ASModel_object_graph.rb', line 109

def models
  @graph.keys
end

#versionObject



83
84
85
# File 'backend/app/model/ASModel_object_graph.rb', line 83

def version
  @graph.hash
end