Module: N4j::Node::Index::ClassMethods

Defined in:
lib/n4j/index.rb

Instance Method Summary collapse

Instance Method Details

#find_by_index(key, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/n4j/index.rb', line 7

def find_by_index(key, value)
  unless instance_variable_get("@#{index_name}_created")
    N4j.batch([{:to => "/index/node/", :method => "POST", :body => {:name => index_name}}])
    instance_variable_set("@#{index_name}_created",true)
  end
  search_url = "/index/node
                /#{index_name}
                /#{CGI.escape(key.to_s)}
                /#{CGI.escape(value)}".gsub(/\s+/,'')
  result = N4j.batch([{:to => search_url,:method => 'GET'}]).first['body'].first
  new(result) if result
end

#index(key) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/n4j/index.rb', line 24

def index(key)
  self.indexes ||= []
  self.indexes << key

  define_singleton_method "find_by_#{key}" do |value|
    find_by_index(key, value)
  end

end

#index_nameObject



20
21
22
# File 'lib/n4j/index.rb', line 20

def index_name
  model_name.i18n_key
end