Sha256: 41ec550027e83b02e66748de349b22c19531069cc0d387120f4935425d0553da

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

module Neo4j::ActiveRel
  module Query
    extend ActiveSupport::Concern

    module ClassMethods
      include Enumerable

      # Returns the object with the specified neo4j id.
      # @param [String,Fixnum] id of node to find
      # @param [Neo4j::Session] session optional
      def find(id, session = self.neo4j_session)
        raise "Unknown argument #{id.class} in find method (expected String or Fixnum)" if not [String, Fixnum].include?(id.class)
        find_by_id(id, session)
      end

      def find_by_id(key, session = Neo4j::Session.current!)
        Neo4j::Relationship.load(key.to_i, session)
      end

      # TODO make this not awful
      def where(args={})
        @query = if self._from_class == :any
          Neo4j::Session.query("MATCH n1-[r1:`#{self._type}`]->(#{cypher_node_string(:inbound)}) WHERE #{where_string(args)} RETURN r1")
        else
          self._from_class.query_as(:n1).match("(#{cypher_node_string(:outbound)})-[r1:`#{self._type}`]->(#{cypher_node_string(:inbound)})").where(Hash["r1" => args])
        end
        return self
      end

      def each
        if self._from_class == :any
          @query.map(&:r1)
        else
          @query.pluck(:r1)
        end.each {|r| yield r }
      end

      def first
        if self._from_class == :any
          @query.map(&:r1)
        else
          @query.pluck(:r1)
        end.first
      end

      def cypher_node_string(dir)
        case dir
        when :outbound
          node_identifier, dir_class = 'n1', self._from_class
        when :inbound
          node_identifier, dir_class = 'n2', self._to_class
        end
        dir_class == :any ? node_identifier : "#{node_identifier}:`#{dir_class.name}`"
      end

      private

      def where_string(args)
        args.map do |k, v|
          v.is_a?(Integer) ? "r1.#{k} = #{v}" : "r1.#{k} = '#{v}'"
        end.join(', ')
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
neo4j-3.0.0.rc.3 lib/neo4j/active_rel/query.rb
neo4j-3.0.0.rc.2 lib/neo4j/active_rel/query.rb
neo4j-3.0.0.alpha.11 lib/neo4j/active_rel/query.rb
neo4j-3.0.0.alpha.10 lib/neo4j/active_rel/query.rb