module Fathom::NetworkBuilder

This is the role necessary for importing nodes, shaping graphs, adding edges, that sort of thing.

Public Instance Methods

from_hash(hash) click to toggle source
# File lib/fathom/roles/network_builder.rb, line 5
def from_hash(hash)
  hash = serialize_hash(hash)
  attributes.merge!(hash)
end

Protected Instance Methods

array_conversion_attributes() click to toggle source
# File lib/fathom/roles/network_builder.rb, line 44
def array_conversion_attributes
  @array_conversion_map ||= [:properties, :variables, :edges, :definitions]
end
attribute_should_be_an_array(attribute) click to toggle source
# File lib/fathom/roles/network_builder.rb, line 40
def attribute_should_be_an_array(attribute)
  array_conversion_attributes.include?(attribute)
end
infer_serialized_array(array, klass) click to toggle source

Uses the Class.infer, if available. Otherwise initializes the value passed in.

# File lib/fathom/roles/network_builder.rb, line 49
def infer_serialized_array(array, klass)
  array.map do |e|
    if  klass == e.class
      e
    elsif klass.respond_to?(:infer)
      klass.infer(e)
    else
      klass.new(e)
    end
  end
end
serialization_class_for(attribute) click to toggle source
# File lib/fathom/roles/network_builder.rb, line 27
def serialization_class_for(attribute)
  serialization_map[attribute]
end
serialization_map() click to toggle source
# File lib/fathom/roles/network_builder.rb, line 31
def serialization_map
  @serialization_map ||= {
    definitions: Definition,
    edges: Edge,
    properties: Property,
    variables: Variable
  }
end
serialize_hash(hash) click to toggle source
# File lib/fathom/roles/network_builder.rb, line 12
def serialize_hash(hash)
  hash.inject({}) do |h, (key, value)|

    value = [value] if attribute_should_be_an_array(key) and not value.is_a?(Array)

    if klass = serialization_class_for(key)
      value = infer_serialized_array(value, klass)
    end

    h[key] = value

    h
  end
end