Sha256: f9c7e98dacfcd68f05df093d13b17b2e9e2c85af15c0ee8294f16ef7bfb30560

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

module ApacheAge
  module Entities
    class Entity
      class << self
        def find_by(attributes)
          where_clause = attributes.map { |k, v| "find.#{k} = '#{v}'" }.join(' AND ')
          handle_find(where_clause)
        end

        def find(id)
          where_clause = "id(find) = #{id}"
          handle_find(where_clause)
        end

        private

        def age_graph = 'age_schema'

        def handle_find(where_clause)
          # try to find a vertex
          match_node = '(find)'
          cypher_sql = find_sql(match_node, where_clause)
          age_response = execute_find(cypher_sql)

          if age_response.nil?
            # if not a vertex try to find an edge
            match_edge = '()-[find]->()'
            cypher_sql = find_sql(match_edge, where_clause)
            age_response = execute_find(cypher_sql)
            return nil if age_response.nil?
          end

          instantiate_result(age_response)
        end

        def execute_find(cypher_sql)
          age_result = ActiveRecord::Base.connection.execute(cypher_sql)
          return nil if age_result.values.first.nil?

          age_result
        end

        def instantiate_result(age_response)
          age_type = age_response.values.first.first.split('::').last
          json_string = age_response.values.first.first.split('::').first
          json_data = JSON.parse(json_string)

          age_label = json_data['label']
          attribs = json_data.except('label', 'properties')
                            .merge(json_data['properties'])
                            .symbolize_keys

          "#{json_data['label'].gsub('__', '::')}".constantize.new(**attribs)
        end

        def find_sql(match_clause, where_clause)
          <<-SQL
            SELECT *
            FROM cypher('#{age_graph}', $$
                MATCH #{match_clause}
                WHERE #{where_clause}
                RETURN find
            $$) as (found agtype);
          SQL
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rails_age-0.6.1 lib/apache_age/entities/entity.rb
rails_age-0.6.0 lib/apache_age/entities/entity.rb
rails_age-0.5.3 lib/apache_age/entities/entity.rb
rails_age-0.5.2 lib/apache_age/entities/entity.rb
rails_age-0.5.1 lib/apache_age/entities/entity.rb
rails_age-0.5.0 lib/apache_age/entities/entity.rb
rails_age-0.4.1 lib/apache_age/entities/entity.rb
rails_age-0.4.0 lib/apache_age/entities/entity.rb
rails_age-0.3.2 lib/apache_age/entities/entity.rb
rails_age-0.3.1 lib/apache_age/entities/entity.rb