Sha256: 0d1eba4377274cd444c875ce4337d8a7f0ec7d32d24896493937e5d222936fce

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Architect4r
  module Model
    module Queries
      extend ActiveSupport::Concern
      
      module InstanceMethods
        
      end
      
      module ClassMethods
        
        def count(opts = {}, &block)
          data = connection.execute_cypher("start s=node(#{self.model_root.id}) match (s)<-[:model_type]-(d) return count(d)")
          data['data'].flatten.first
        end
        
        # Fetch a record of the specified model based on its id
        #
        def find(id)
          data = connection.execute_cypher("start s=node(#{self.model_root.id}), d=node(#{id.to_i}) match s<-[r:model_type]-d return d")
          data &&= data['data'] && data['data'].flatten.first
          self.build_from_database(data)
        end
        alias :find_by_id :find
        
        def find!(id)
          self.find_by_id(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!"))
        end
        alias :find_by_id! :find!
        
        # Use this method only to fetch items of the same class.
        def find_by_cypher(query, identifier)
          if result_data = connection.execute_cypher(query)
            result_data = connection.transform_cypher_result_to_hash(result_data)
            result_data.map { |item| connection.convert_if_possible(item[identifier]) }
          else
            nil
          end
        end
        
        def all
          find_by_cypher("start ModelRoot=node(#{self.model_root.id}) match ModelRoot<-[:model_type]-Item return Item", 'Item')
        end
        
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
architect4r-0.4.2 lib/architect4r/model/queries.rb