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-0.13.2 base/lib/social_stream/d3.rb
social_stream-base-0.10.2 lib/social_stream/d3.rb
social_stream-0.13.1 base/lib/social_stream/d3.rb
social_stream-base-0.10.1 lib/social_stream/d3.rb
social_stream-0.13.0 base/lib/social_stream/d3.rb
social_stream-base-0.10.0 lib/social_stream/d3.rb
social_stream-0.12.14 base/lib/social_stream/d3.rb
social_stream-base-0.9.36 lib/social_stream/d3.rb
social_stream-0.12.13 base/lib/social_stream/d3.rb
social_stream-base-0.9.35 lib/social_stream/d3.rb
social_stream-0.12.12 base/lib/social_stream/d3.rb
social_stream-base-0.9.34 lib/social_stream/d3.rb
social_stream-0.12.11 base/lib/social_stream/d3.rb
social_stream-base-0.9.33 lib/social_stream/d3.rb
social_stream-0.12.10 base/lib/social_stream/d3.rb
social_stream-base-0.9.32 lib/social_stream/d3.rb
social_stream-0.12.9 base/lib/social_stream/d3.rb
social_stream-base-0.9.31 lib/social_stream/d3.rb
social_stream-0.12.8 base/lib/social_stream/d3.rb
social_stream-base-0.9.30 lib/social_stream/d3.rb