lib/hari/node.rb in hari-0.0.4 vs lib/hari/node.rb in hari-0.0.5
- old
+ new
@@ -1,8 +1,9 @@
require 'hari/node/repository'
require 'hari/node/queries'
require 'hari/node/serialization'
+require 'hari/node/index'
module Hari
class Node < Entity
include Hari::Node::Queries
include Hari::Node::Repository
@@ -13,21 +14,37 @@
def initialize(attrs = {})
attrs = { model_id: attrs } if attrs.kind_of?(::Fixnum)
super
end
+ after_update { reindex }
+ after_create { reindex force_index: true }
+ after_destroy { remove_from_indexes }
+
def generate_id
- return super unless model_id
+ unless model_id.present?
+ begin
+ self.model_id = SecureRandom.hex(8)
+ end until !Hari.redis.exists(node_key)
+ end
- model_id.to_s.include?('#') ? model_id.to_s : "#{node_type}##{model_id}"
+ node_key
end
+ def node_key
+ "#{node_type}##{model_id}"
+ end
+
def node_type
@node_type || self.class.node_type
end
def self.node_type
self.to_s.underscore
+ end
+
+ def self.indexed_properties
+ properties.select { |p| p.options[:index] }
end
end
end