Class: Render::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/render/graph.rb

Constant Summary collapse

PARAM =
%r{:(?<param>[\w_]+)}
PARAMS =
%r{#{PARAM}[\/\;\&]?}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, attributes = {}) ⇒ Graph

Returns a new instance of Graph.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/render/graph.rb', line 24

def initialize(schema, attributes = {})
  self.schema = if schema.is_a?(Symbol)
    Schema.new(schema)
  else
    schema
  end

  self.relationships = (attributes.delete(:relationships) || {})
  self.raw_endpoint = (attributes.delete(:endpoint) || "")
  self.graphs = (attributes.delete(:graphs) || [])
  self.config = attributes
  self.parental_params = {}

  initialize_params!
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



16
17
18
# File 'lib/render/graph.rb', line 16

def config
  @config
end

#graphsObject

Returns the value of attribute graphs.



16
17
18
# File 'lib/render/graph.rb', line 16

def graphs
  @graphs
end

#paramsObject

Returns the value of attribute params.



16
17
18
# File 'lib/render/graph.rb', line 16

def params
  @params
end

#parental_paramsObject

Returns the value of attribute parental_params.



16
17
18
# File 'lib/render/graph.rb', line 16

def parental_params
  @parental_params
end

#raw_endpointObject

Returns the value of attribute raw_endpoint.



16
17
18
# File 'lib/render/graph.rb', line 16

def raw_endpoint
  @raw_endpoint
end

#relationshipsObject

Returns the value of attribute relationships.



16
17
18
# File 'lib/render/graph.rb', line 16

def relationships
  @relationships
end

#schemaObject

Returns the value of attribute schema.



16
17
18
# File 'lib/render/graph.rb', line 16

def schema
  @schema
end

Instance Method Details

#endpointObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/render/graph.rb', line 40

def endpoint
  uri = URI(raw_endpoint)

  uri.path.gsub!(PARAMS) do |param|
    key = param_key(param)
    param.gsub(PARAM, param_value(key))
  end

  if uri.query
    uri.query.gsub!(PARAMS) do |param|
      key = param_key(param)
      "#{key}=#{param_value(key)}&"
    end.chop!
  end

  uri.to_s
end

#render(inherited_attributes = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/render/graph.rb', line 58

def render(inherited_attributes = {})
  calculate_parental_params!(inherited_attributes)
  graph_attributes = schema.render(inherited_attributes.merge(parental_params.merge({ endpoint: endpoint })))

  graph = graphs.inject(graph_attributes) do |attributes, nested_graph|
    threads = []
    # TODO threading should be configured so people may also think about Thread.abort_on_transaction!
    threads << Thread.new do
      title = schema.title.to_sym
      parent_data = attributes[title]
      nested_graph_data = if parent_data.is_a?(Array)
        data = parent_data.collect do |element|
          nested_graph.render(element)
        end
        key = data.first.keys.first
        attributes[title] = data.collect { |d| d[key] }
      else
        data = nested_graph.render(parent_data)
        parent_data.merge!(data)
      end
    end
    threads.collect(&:join)
    attributes
  end
  DottableHash.new(graph)
end