lib/render/graph.rb in render-0.0.4 vs lib/render/graph.rb in render-0.0.5

- old
+ new

@@ -1,14 +1,8 @@ -# The Graph is the top-level model that defines a Schema and correlating Graphs. -# It also includes particular metadata: -# - Endpoint to query for its schema's data -# - Config for this endpoint, e.g. an access token -# - Relationships between it and a Graph that includes it - require "render/schema" require "render/errors" -require "render/dottable_hash" +require "render/extensions/dottable_hash" module Render class Graph PARAM = %r{:(?<param>[\w_]+)} PARAMS = %r{#{PARAM}[\/\;\&]?} @@ -23,61 +17,68 @@ 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.raw_endpoint = (options.delete(:endpoint) || schema.definition[:endpoint]).to_s self.config = options self.inherited_data = {} end - 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 + def title + schema.universal_title || schema.title end - def render(inherited_properties = nil) + def render!(inherited_properties = nil) self.inherited_data = inherited_properties if (schema.type == Array) explicit_data = inherited_data else explicit_data = inherited_data.is_a?(Hash) ? inherited_data : {} explicit_data = explicit_data.merge!(relationship_data_from_parent) end - graph_data = DottableHash.new + graph_data = Extensions::DottableHash.new rendered_data = schema.render!(explicit_data, endpoint) 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] + nested_data << graph.render!(element)[graph.title] end else - nested_data = graph.render(parent_data) + nested_data = graph.render!(parent_data) graph_data.merge!(nested_data) end end end self.rendered_data = graph_data.merge!(rendered_data) end + private + + def endpoint + raw_endpoint.gsub!(":host", config.fetch(:host)) if raw_endpoint.match(":host") + uri = URI(raw_endpoint) + + uri.path.gsub!(PARAMS) do |param| + key = param_key(param) + param.gsub(PARAM, param_value(key).to_s) + 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 + def loop_with_configured_threading(elements) if Render.threading? threads = [] elements.each do |element| threads << Thread.new do @@ -89,15 +90,9 @@ elements.each do |element| yield element end end end - - def title - schema.universal_title || schema.title - end - - private def determine_schema(schema_or_definition) if schema_or_definition.is_a?(Schema) schema_or_definition else