Sha256: 125f9ad974cf7fc7777b5dd8b69c63357472e0dbbde9362f7a73c2e7f8d0ea4d

Contents?: true

Size: 1.22 KB

Versions: 240

Compression:

Stored size: 1.22 KB

Contents

module SocialStream
  # Methods for d3.js library
  # http://mbostock.github.com/d3/
  module D3
    class Force
      def initialize(ties, view)
        @view = view

        @force =
          ties.inject({ :nodes => [], :links => [] }) { |result, t|
            add_node(result[:nodes], t.sender)
            add_node(result[:nodes], t.receiver)

            add_link(result[:links], t, result[:nodes])

            result
          }
      end

      def to_json
        @force.to_json
      end

      private

      def node(actor)
        { 
          :name     => actor.name,
          :logo     => @view.image_path(actor.logo.url(:representation)),
          :group    => SocialStream.subjects.index(actor.subject_type.underscore.to_sym) + 1
        }
      end

      def add_node(nodes, actor)
        return if nodes_actor_index(nodes, actor)

        nodes << node(actor)
      end

      def add_link(links, tie, nodes)
        links << {
          :source => nodes_actor_index(nodes, tie.sender),
          :target => nodes_actor_index(nodes, tie.receiver),
          :value  => 1
        }
      end

      def nodes_actor_index(nodes, actor)
        nodes.index(nodes.find{ |n| n[:name] == actor.name })
      end
    end
  end
end

Version data entries

240 entries across 240 versions & 4 rubygems

Version Path
social_stream-2.2.2 base/lib/social_stream/d3.rb
social_stream-base-2.2.2 lib/social_stream/d3.rb
social_stream-2.2.1 base/lib/social_stream/d3.rb
social_stream-base-2.2.1 lib/social_stream/d3.rb
social_stream-2.2.0 base/lib/social_stream/d3.rb
social_stream-base-2.2.0 lib/social_stream/d3.rb
social_stream-2.1.1 base/lib/social_stream/d3.rb
social_stream-1.1.12 base/lib/social_stream/d3.rb
social_stream-base-1.1.11 lib/social_stream/d3.rb
social_stream-2.1.0 base/lib/social_stream/d3.rb
social_stream-base-2.1.0 lib/social_stream/d3.rb
social_stream-2.0.4 base/lib/social_stream/d3.rb
social_stream-base-2.0.4 lib/social_stream/d3.rb
social_stream-2.0.3 base/lib/social_stream/d3.rb
social_stream-base-2.0.3 lib/social_stream/d3.rb
social_stream-1.1.11 base/lib/social_stream/d3.rb
social_stream-base-1.1.10 lib/social_stream/d3.rb
social_stream-1.1.10 base/lib/social_stream/d3.rb
social_stream-base-1.1.9 lib/social_stream/d3.rb
social_stream-1.1.9 base/lib/social_stream/d3.rb