lib/render/graph.rb in render-0.0.2 vs lib/render/graph.rb in render-0.0.3

- old
+ new

@@ -15,28 +15,22 @@ attr_accessor :schema, :raw_endpoint, :relationships, :graphs, - :params, - :parental_params, # TODO rename this to inherited_params - :config + :inherited_data, + :config, + :rendered_data - def initialize(schema, attributes = {}) - self.schema = if schema.is_a?(Symbol) - Schema.new(schema) - else - schema - end + 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.relationships = (attributes.delete(:relationships) || {}) - self.raw_endpoint = (attributes.delete(:endpoint) || "") - self.graphs = (attributes.delete(:graphs) || []) - self.config = attributes - self.parental_params = {} - - initialize_params! + self.inherited_data = {} end def endpoint uri = URI(raw_endpoint) @@ -53,56 +47,83 @@ end uri.to_s end - def render(inherited_attributes = {}) - calculate_parental_params!(inherited_attributes) - graph_attributes = schema.render(inherited_attributes.merge(parental_params.merge({ endpoint: endpoint }))) + def render(inherited_properties = {}) + self.inherited_data = inherited_properties - 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) + 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 - key = data.first.keys.first - attributes[title] = data.collect { |d| d[key] } else - data = nested_graph.render(parent_data) - parent_data.merge!(data) + nested_data = graph.render(parent_data) + graph_data.merge!(nested_data) end end + end + + self.rendered_data = graph_data.merge!(rendered_data) + end + + 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) - attributes + else + elements.each do |element| + yield element + end end - DottableHash.new(graph) end + def title + schema.universal_title || schema.title + end + private - def initialize_params! - self.params = raw_endpoint.scan(PARAMS).flatten.inject({}) do |params, param| - params.merge({ param.to_sym => nil }) + def determine_schema(schema_or_definition) + if schema_or_definition.is_a?(Schema) + schema_or_definition + else + Schema.new(schema_or_definition) end end - def calculate_parental_params!(inherited) - self.parental_params = relationships.inject(inherited) do |attributes, (parent_key, child_key)| - attributes.merge({ child_key => inherited[parent_key] }) + def relationship_data_from_parent + relationships.inject({}) do |data, (parent_key, child_key)| + data.merge({ child_key => value_from_inherited_data(child_key) }) end end def param_key(string) string.match(PARAM)[:param].to_sym end def param_value(key) - parental_params[key] || config[key] || raise(Errors::Graph::EndpointKeyNotFound.new(key)) + value_from_inherited_data(key) || config[key] || raise(Errors::Graph::EndpointKeyNotFound.new(key)) + end + + def value_from_inherited_data(key) + relationships.each do |parent_key, child_key| + if !inherited_data.is_a?(Hash) + return inherited_data + elsif (child_key == key) + return inherited_data.fetch(parent_key) + end + end + nil end end end