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_or_definition, options = {}) ⇒ Graph

Returns a new instance of Graph.



24
25
26
27
28
29
30
31
32
# File 'lib/render/graph.rb', line 24

def initialize(schema_or_definition, options = {})
  self.schema = determine_schema(schema_or_definition)
  self.relationships = (options.delete(:relationships) || {})
  self.graphs = (options.delete(:graphs) || [])
  self.raw_endpoint = options.delete(:endpoint).to_s
  self.config = options

  self.inherited_data = {}
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

#inherited_dataObject

Returns the value of attribute inherited_data.



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

def inherited_data
  @inherited_data
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

#rendered_dataObject

Returns the value of attribute rendered_data.



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

def rendered_data
  @rendered_data
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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/render/graph.rb', line 34

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

#loop_with_configured_threading(elements) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/render/graph.rb', line 73

def loop_with_configured_threading(elements)
  if Render.threading?
    threads = []
    elements.each do |element|
      threads << Thread.new do
        yield element
      end
    end
    threads.collect(&:join)
  else
    elements.each do |element|
      yield element
    end
  end
end

#render(inherited_properties = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/render/graph.rb', line 52

def render(inherited_properties = {})
  self.inherited_data = inherited_properties

  graph_data = DottableHash.new
  inherited_data = relationship_data_from_parent.merge({ endpoint: endpoint })
  rendered_data = schema.render!(inherited_data) do |parent_data|
    loop_with_configured_threading(graphs) do |graph|
      if parent_data.is_a?(Array)
        graph_data[graph.title] = parent_data.inject([]) do |nested_data, element|
          nested_data << graph.render(element)[graph.title]
        end
      else
        nested_data = graph.render(parent_data)
        graph_data.merge!(nested_data)
      end
    end
  end

  self.rendered_data = graph_data.merge!(rendered_data)
end

#titleObject



89
90
91
# File 'lib/render/graph.rb', line 89

def title
  schema.universal_title || schema.title
end