n4j 0.0.1.4 → 0.0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/n4j/entity.rb CHANGED
@@ -12,11 +12,11 @@ module N4j
12
12
  include N4j::Field
13
13
 
14
14
  extend ActiveModel::Callbacks
15
- define_model_callbacks :initialize
15
+ define_model_callbacks :initialize, :save
16
16
 
17
17
  include Representation
18
18
 
19
- attr_accessor :from_neo4j
19
+ attr_writer :from_neo4j
20
20
  attr_accessor :destroyed
21
21
  attribute :data
22
22
  end
@@ -33,6 +33,10 @@ module N4j
33
33
  end
34
34
  end
35
35
 
36
+ def from_neo4j
37
+ @from_neo4j ||= HashWithIndifferentAccess.new
38
+ end
39
+
36
40
  def note_update!
37
41
  changed_attributes.clear
38
42
  end
@@ -72,12 +76,18 @@ module N4j
72
76
  end
73
77
 
74
78
  def save
75
- updateable_bundle = create_bundle.select(&:persist_hash)
76
- commands = updateable_bundle.collect {|entity| entity.persist_hash(:id => entity.place_in_batch) }
77
- results = N4j.batch(commands)
78
- updateable_bundle.zip(results).each do |entity,result|
79
- entity.load_neo4j_data(result['body'])
80
- entity.post_save
79
+ run_callbacks :save do
80
+ updateable_bundle = create_bundle.select(&:persist_hash)
81
+ commands = updateable_bundle.collect {|entity| entity.persist_hash(:id => entity.place_in_batch) }
82
+ index_commands = create_bundle.select do |entity|
83
+ entity.class.ancestors.
84
+ include?(N4j::Node)
85
+ end.collect(&:index_hashes).compact.flatten
86
+ results = N4j.batch(commands + index_commands)
87
+ updateable_bundle.zip(results).each do |entity,result|
88
+ entity.load_neo4j_data(result['body'])
89
+ entity.post_save
90
+ end
81
91
  end
82
92
  end
83
93
 
data/lib/n4j/field.rb CHANGED
@@ -7,20 +7,20 @@ module N4j::Field
7
7
  define_method name do
8
8
  data_access(name.to_s)
9
9
  end
10
-
10
+
11
11
  define_method "#{name}=" do |value|
12
12
  data_set(name, value)
13
13
  end
14
14
  end
15
15
  end
16
-
16
+
17
17
  def data_access(key)
18
- data[key]
18
+ data && data[key.to_s]
19
19
  end
20
20
 
21
21
  def data_set(key, new_value)
22
22
  new_data = data.merge({key.to_s => new_value})
23
23
  self.data = new_data
24
24
  end
25
-
25
+
26
26
  end
data/lib/n4j/index.rb ADDED
@@ -0,0 +1,62 @@
1
+ module N4j::Node::Index
2
+ extend ActiveSupport::Concern
3
+ included do
4
+ N4j.batch([{:to => "#{N4j.neo4j_url_prefix}/index/node/", :method => "POST", :body => {:name => index_name}}])
5
+ class_attribute :indexes
6
+ end
7
+ module ClassMethods
8
+ def find_by_index(key, value)
9
+ search_url = "#{N4j.neo4j_url_prefix}
10
+ /index/node
11
+ /#{index_name}
12
+ /#{CGI.escape(key.to_s)}
13
+ /#{CGI.escape(value)}".gsub(/\s+/,'')
14
+ result = N4j.batch([{:to => search_url,:method => 'GET'}]).first['body'].first
15
+ new(result) if result
16
+ end
17
+
18
+ def index_name
19
+ model_name.i18n_key
20
+ end
21
+
22
+ def index(key)
23
+ self.indexes ||= []
24
+ self.indexes << key
25
+
26
+ define_singleton_method "find_by_#{key}" do |value|
27
+ find_by_index(key, value)
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ def add_to_index(key)
34
+ index = {
35
+ "value" => send(key),
36
+ "uri" => url,
37
+ "key" => key
38
+ }
39
+
40
+ N4j.batch([{:to => "#{index_path}",:method => 'POST',:body => index }])
41
+ end
42
+
43
+ def index_path
44
+ "#{N4j.neo4j_url_prefix}/index/node/#{self.class.index_name}"
45
+ end
46
+
47
+ def index_hashes # (opts ={}) <- the normal argument pattern doesn't really work here.
48
+ if (indexes = self.class.indexes)
49
+ indexes.collect do |index|
50
+ if (value = send(index))
51
+ index = {
52
+ "value" => send(index),
53
+ "uri" => url,
54
+ "key" => index
55
+ }
56
+ {:to => "#{index_path}",:method => 'POST',:body => index }
57
+ end
58
+ end.compact
59
+ end
60
+ end
61
+
62
+ end
data/lib/n4j/node.rb CHANGED
@@ -1,27 +1,29 @@
1
1
  module N4j::Node
2
-
2
+
3
3
  autoload :AutoRelate, 'n4j/auto_relate'
4
-
4
+ autoload :Index, 'n4j/index'
5
+
5
6
  extend ActiveSupport::Concern
6
7
  included do
7
8
  include AutoRelate
9
+ include Index
8
10
  # include N4j::Entity
9
11
  end
10
12
  module ClassMethods
11
13
  def find(n)
12
14
  find_by_node_id(n)
13
15
  end
14
-
16
+
15
17
  def find_by_node_id(n)
16
18
  find_by_path("/node/#{n}")
17
19
  end
18
-
20
+
19
21
  def find_by_path(path)
20
22
  path.sub!(N4j.neo4j_url_prefix,'')
21
23
  result = N4j.batch([{:to => path, :method => 'GET'}])
22
- new(result.first['body'])
24
+ new(result.first['body'])
23
25
  end
24
-
26
+
25
27
  def service_root
26
28
  find_by_node_id(0)
27
29
  end
@@ -30,7 +32,7 @@ module N4j::Node
30
32
  def initialize(hsh)
31
33
  super(hsh)
32
34
  unless N4j.neo4j_hash?(hsh)
33
- self.data = hsh.dup
35
+ self.data = HashWithIndifferentAccess.new.merge(hsh)
34
36
  end
35
37
  end
36
38
 
@@ -19,11 +19,14 @@ module N4j::Entity::Representation
19
19
  end
20
20
 
21
21
  def url
22
- from_neo4j['self']
22
+ from_neo4j['self'] || place_in_batch_path
23
23
  end
24
24
 
25
25
  def path
26
- from_neo4j_relative['self'] ||
26
+ from_neo4j_relative['self'] || place_in_batch_path
27
+ end
28
+
29
+ def place_in_batch_path
27
30
  (place_in_batch ? "{#{place_in_batch}}" : nil)
28
31
  end
29
32
 
data/lib/n4j/request.rb CHANGED
@@ -10,7 +10,7 @@ module N4j::Request
10
10
  end
11
11
 
12
12
  def port
13
- config = YAML.load_file("#{Rails.root}/config/n4j.yml")
13
+ config = YAML.load_file("config/n4j.yml")
14
14
  config[Rails.env]['port']
15
15
  end
16
16
 
@@ -20,8 +20,12 @@ module N4j::Request
20
20
 
21
21
  def batch(commands) # [{'to'=> '', 'method' => '', 'body' => '', 'id' => 0},...]
22
22
  commands.flatten!
23
- commands.each_with_index {|command, index| command[:id] ||= index }
24
- # puts "Batch job: #{commands.inspect}"
23
+ # commands.each_with_index {|command, index| command[:id] ||= index }
24
+ commands.inject(0) do |index, command|
25
+ command[:id] = command[:id] || (index + 1)
26
+ end
27
+ puts "Batch job: "
28
+ commands.each {|c| puts " #{c}"}
25
29
  begin
26
30
  result = RestClient.post("#{neo4j_url_prefix}/batch",
27
31
  commands.to_json,
data/lib/n4j/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module N4j
2
- VERSION = "0.0.1.4"
2
+ VERSION = "0.0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: n4j
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.4
4
+ version: 0.0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-23 00:00:00.000000000 -05:00
13
- default_executable:
12
+ date: 2012-02-26 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rest-client
17
- requirement: &2153204100 !ruby/object:Gem::Requirement
16
+ requirement: &70305424386120 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *2153204100
24
+ version_requirements: *70305424386120
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: activesupport
28
- requirement: &2153203680 !ruby/object:Gem::Requirement
27
+ requirement: &70305424385700 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: '0'
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *2153203680
35
+ version_requirements: *70305424385700
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: activemodel
39
- requirement: &2153203260 !ruby/object:Gem::Requirement
38
+ requirement: &70305424385280 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,7 +43,7 @@ dependencies:
44
43
  version: '0'
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *2153203260
46
+ version_requirements: *70305424385280
48
47
  description: A little spiked out thing.
49
48
  email:
50
49
  - samsm@samsm.com
@@ -64,6 +63,7 @@ files:
64
63
  - lib/n4j/cypher.rb
65
64
  - lib/n4j/entity.rb
66
65
  - lib/n4j/field.rb
66
+ - lib/n4j/index.rb
67
67
  - lib/n4j/node.rb
68
68
  - lib/n4j/populate.rb
69
69
  - lib/n4j/railtie.rb
@@ -87,7 +87,6 @@ files:
87
87
  - test/field_test.rb
88
88
  - test/test_helper.rb
89
89
  - tmp/pids/.gitkeep
90
- has_rdoc: true
91
90
  homepage: ''
92
91
  licenses: []
93
92
  post_install_message:
@@ -100,15 +99,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
99
  - - ! '>='
101
100
  - !ruby/object:Gem::Version
102
101
  version: '0'
102
+ segments:
103
+ - 0
104
+ hash: -2230038907754236757
103
105
  required_rubygems_version: !ruby/object:Gem::Requirement
104
106
  none: false
105
107
  requirements:
106
108
  - - ! '>='
107
109
  - !ruby/object:Gem::Version
108
110
  version: '0'
111
+ segments:
112
+ - 0
113
+ hash: -2230038907754236757
109
114
  requirements: []
110
115
  rubyforge_project: n4j
111
- rubygems_version: 1.6.2
116
+ rubygems_version: 1.8.10
112
117
  signing_key:
113
118
  specification_version: 3
114
119
  summary: Help using Neo4j Rest server.