Sha256: 9b24dd024d4749532c2f99ec569850f8e410f6e2ea6529232f1ef33bb1fe1bff

Contents?: true

Size: 1.9 KB

Versions: 6

Compression:

Stored size: 1.9 KB

Contents

require 'orm_adapter'

module Neo4j
  module ActiveNode
    module ClassMethods
      include OrmAdapter::ToAdapter
    end

    class OrmAdapter < ::OrmAdapter::Base
      module ClassMethods
        include ActiveModel::Callbacks
      end

      def column_names
        klass._decl_props.keys
      end

      def i18n_scope
        :neo4j
      end

      # Get an instance by id of the model
      def get!(id)
        klass.find(wrap_key(id)).tap do |node|
          raise "No record found" if node.nil?
        end
      end

      # Get an instance by id of the model
      def get(id)
        klass.find(wrap_key(id))
      end

      # Find the first instance matching conditions
      def find_first(options = {})
        conditions, order = extract_conditions!(options)
        extract_id!(conditions)
        order = hasherize_order(order)

        result = klass.where(conditions)
        result = result.order(order) unless order.empty?
        result.first
      end

      # Find all models matching conditions
      def find_all(options = {})
        conditions, order, limit, offset = extract_conditions!(options)
        extract_id!(conditions)
        order = hasherize_order(order)

        result = klass.where(conditions)
        result = result.order(order) unless order.empty?
        result = result.skip(offset) if offset
        result = result.limit(limit) if limit
        result.to_a
      end

      # Create a model using attributes
      def create!(attributes = {})
        klass.create!(attributes)
      end

      # @see OrmAdapter::Base#destroy
      def destroy(object)
        object.destroy && true if valid_object?(object)
      end

      private

      def hasherize_order(order)
        (order || []).map {|clause| Hash[*clause] }
      end

      def extract_id!(conditions)
        if id = conditions.delete(:id)
          conditions[:neo_id] = id.to_i
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
neo4j-3.0.0.rc.3 lib/neo4j/active_node/orm_adapter.rb
neo4j-3.0.0.rc.2 lib/neo4j/active_node/orm_adapter.rb
neo4j-3.0.0.alpha.11 lib/neo4j/active_node/orm_adapter.rb
neo4j-3.0.0.alpha.10 lib/neo4j/active_node/orm_adapter.rb
neo4j-3.0.0.alpha.9 lib/neo4j/active_node/orm_adapter.rb
neo4j-3.0.0.alpha.8 lib/neo4j/active_node/orm_adapter.rb