Sha256: 49573db473111c47037d9d664f09b82f71f42676b2d8c21a080b920e4ff04294

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module Hari
  class Node < Entity
    module Repository
      extend ActiveSupport::Concern

      def reindex(options = {})
        self.class.indexed_properties.each do |property|
          if change = previous_changes[property.name]
            previous, current = change

            Index.new(property, previous).delete self
            Index.new(property, current).add self
          elsif options[:force_index]
            value = send(property.name)
            Index.new(property, value).add self
          end
        end
      end

      def remove_from_indexes
        self.class.indexed_properties.each do |property|
          value = send(property.name)
          Index.new(property, value).delete self
        end
      end

      module ClassMethods

        def find_one(id, options = {})
          id = "#{node_type}##{id}" unless id.to_s.include?('#')
          super id, options
        end

        def find_many(ids, options = {})
          ids = ids.map do |id|
            id.to_s.include?('#') ? id : "#{node_type}##{id}"
          end

          super ids, options
        end

        def find_by(name, value)
          if property = indexed_properties.find { |p| p.name.to_s == name.to_s }
            Index.new property, value
          else
            fail "missing index for key #{name}"
          end
        end

        def where(conditions = {})
          conditions.inject(nil) do |index, (key, value)|
            query = find_by(key, value)
            index ? index.append(query) : query
          end
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.5 lib/hari/node/repository.rb