fauna 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGELOG +2 -0
- data/Manifest +0 -1
- data/README.md +0 -2
- data/Rakefile +19 -12
- data/fauna.gemspec +7 -6
- data/lib/fauna.rb +1 -3
- data/lib/fauna/cache.rb +10 -12
- data/lib/fauna/client.rb +3 -3
- data/lib/fauna/connection.rb +28 -31
- data/lib/fauna/named_resource.rb +2 -2
- data/lib/fauna/rails.rb +20 -18
- data/lib/fauna/resource.rb +58 -30
- data/lib/fauna/set.rb +27 -16
- data/lib/fauna/util.rb +2 -2
- data/lib/tasks/fauna.rake +14 -14
- data/test/class_test.rb +6 -1
- data/test/client_test.rb +9 -8
- data/test/connection_test.rb +12 -12
- data/test/database_test.rb +5 -6
- data/test/query_test.rb +13 -9
- data/test/readme_test.rb +5 -5
- data/test/set_test.rb +2 -3
- data/test/test_helper.rb +47 -44
- metadata +17 -33
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -2
- data/fauna-ruby.pem +0 -21
- metadata.gz.sig +0 -0
data/lib/fauna/resource.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Fauna
|
2
|
-
class Resource
|
2
|
+
class Resource # rubocop:disable Metrics/ClassLength
|
3
3
|
def self.resource_subclass(fauna_class)
|
4
4
|
case fauna_class
|
5
5
|
when 'databases' then Fauna::NamedResource
|
@@ -18,7 +18,7 @@ module Fauna
|
|
18
18
|
|
19
19
|
def self.new(fauna_class, attrs = {})
|
20
20
|
obj = resource_subclass(fauna_class).allocate
|
21
|
-
obj.instance_variable_set('@struct',
|
21
|
+
obj.instance_variable_set('@struct', 'ref' => nil, 'ts' => nil, 'deleted' => false, 'class' => fauna_class)
|
22
22
|
obj.struct = attrs
|
23
23
|
obj
|
24
24
|
end
|
@@ -32,7 +32,7 @@ module Fauna
|
|
32
32
|
end
|
33
33
|
|
34
34
|
attr_reader :struct
|
35
|
-
|
35
|
+
alias_method :to_hash, :struct
|
36
36
|
|
37
37
|
def ts
|
38
38
|
struct['ts'] ? Fauna.time_from_usecs(struct['ts']) : nil
|
@@ -42,27 +42,47 @@ module Fauna
|
|
42
42
|
struct['ts'] = Fauna.usecs_from_time(time)
|
43
43
|
end
|
44
44
|
|
45
|
-
def ref
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def
|
50
|
-
|
51
|
-
|
45
|
+
def ref
|
46
|
+
struct['ref']
|
47
|
+
end
|
48
|
+
|
49
|
+
def fauna_class
|
50
|
+
struct['class']
|
51
|
+
end
|
52
|
+
|
53
|
+
def deleted
|
54
|
+
struct['deleted']
|
55
|
+
end
|
56
|
+
|
57
|
+
def constraints
|
58
|
+
struct['constraints'] ||= {}
|
59
|
+
end
|
60
|
+
|
61
|
+
def data
|
62
|
+
struct['data'] ||= {}
|
63
|
+
end
|
64
|
+
|
65
|
+
def references
|
66
|
+
struct['references'] ||= {}
|
67
|
+
end
|
68
|
+
|
69
|
+
def permissions
|
70
|
+
struct['permissions'] ||= {}
|
71
|
+
end
|
52
72
|
|
53
73
|
def events(pagination = {})
|
54
74
|
EventsPage.find("#{ref}/events", {}, pagination)
|
55
75
|
end
|
56
76
|
|
57
77
|
def new_event(action, time)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
)
|
65
|
-
|
78
|
+
return unless persisted?
|
79
|
+
|
80
|
+
Fauna::Event.new(
|
81
|
+
'resource' => ref,
|
82
|
+
'set' => ref,
|
83
|
+
'action' => action,
|
84
|
+
'ts' => Fauna.usecs_from_time(time),
|
85
|
+
)
|
66
86
|
end
|
67
87
|
|
68
88
|
def set(name)
|
@@ -70,9 +90,9 @@ module Fauna
|
|
70
90
|
end
|
71
91
|
|
72
92
|
def eql?(other)
|
73
|
-
|
93
|
+
fauna_class == other.fauna_class && ref == other.ref && !ref.nil?
|
74
94
|
end
|
75
|
-
|
95
|
+
alias_method :==, :eql?
|
76
96
|
|
77
97
|
# dynamic field access
|
78
98
|
|
@@ -81,9 +101,9 @@ module Fauna
|
|
81
101
|
end
|
82
102
|
|
83
103
|
def method_missing(method, *args)
|
84
|
-
if field = getter_method(method)
|
104
|
+
if (field = getter_method(method))
|
85
105
|
struct[field]
|
86
|
-
elsif field = setter_method(method)
|
106
|
+
elsif (field = setter_method(method))
|
87
107
|
struct[field] = args.first
|
88
108
|
else
|
89
109
|
super
|
@@ -92,11 +112,17 @@ module Fauna
|
|
92
112
|
|
93
113
|
# object lifecycle
|
94
114
|
|
95
|
-
def new_record
|
115
|
+
def new_record?
|
116
|
+
ref.nil?
|
117
|
+
end
|
96
118
|
|
97
|
-
def deleted
|
119
|
+
def deleted?
|
120
|
+
!!deleted
|
121
|
+
end
|
98
122
|
|
99
|
-
def persisted
|
123
|
+
def persisted?
|
124
|
+
!(new_record? || deleted?)
|
125
|
+
end
|
100
126
|
|
101
127
|
def save
|
102
128
|
new_record? ? post : put
|
@@ -124,15 +150,17 @@ module Fauna
|
|
124
150
|
# TODO: make this configurable, and possible to invert to a white list
|
125
151
|
UNASSIGNABLE_ATTRIBUTES = %w(ts deleted fauna_class).inject({}) { |h, attr| h.update attr => true }
|
126
152
|
|
127
|
-
DEFAULT_ATTRIBUTES = {"data" => {}, "references" => {}, "constraints" => {} }
|
128
|
-
|
129
153
|
def struct=(attributes)
|
130
|
-
|
154
|
+
default_attributes.merge(attributes).each do |name, val|
|
131
155
|
send "#{name}=", val unless UNASSIGNABLE_ATTRIBUTES[name.to_s]
|
132
156
|
end
|
133
157
|
end
|
134
158
|
|
135
|
-
|
159
|
+
private
|
160
|
+
|
161
|
+
def default_attributes
|
162
|
+
{ 'data' => {}, 'references' => {}, 'constraints' => {} }
|
163
|
+
end
|
136
164
|
|
137
165
|
def getter_method(method)
|
138
166
|
field = method.to_s
|
@@ -140,7 +168,7 @@ module Fauna
|
|
140
168
|
end
|
141
169
|
|
142
170
|
def setter_method(method)
|
143
|
-
(/(.*)=$/ =~ method.to_s) ?
|
171
|
+
(/(.*)=$/ =~ method.to_s) ? Regexp.last_match[1] : nil
|
144
172
|
end
|
145
173
|
end
|
146
174
|
end
|
data/lib/fauna/set.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Fauna
|
2
2
|
class Set
|
3
|
-
|
4
3
|
attr_reader :ref
|
5
4
|
|
6
5
|
def initialize(ref)
|
@@ -62,16 +61,12 @@ module Fauna
|
|
62
61
|
def param_strings
|
63
62
|
if @function == 'match'
|
64
63
|
# Escape strings for match values
|
65
|
-
@params = @params[0..1] + @params[2..-1].
|
66
|
-
|
67
|
-
p.inspect
|
68
|
-
else
|
69
|
-
p
|
70
|
-
end
|
64
|
+
@params = @params[0..1] + @params[2..-1].collect do |p|
|
65
|
+
p.is_a?(String) ? p.inspect : p
|
71
66
|
end
|
72
67
|
end
|
73
68
|
|
74
|
-
@param_strings ||= @params.
|
69
|
+
@param_strings ||= @params.collect do |p|
|
75
70
|
if p.respond_to? :expr
|
76
71
|
p.expr
|
77
72
|
elsif p.respond_to? :ref
|
@@ -156,16 +151,24 @@ module Fauna
|
|
156
151
|
refs.empty?
|
157
152
|
end
|
158
153
|
|
159
|
-
def fauna_count
|
160
|
-
|
161
|
-
|
154
|
+
def fauna_count
|
155
|
+
struct['count']
|
156
|
+
end
|
157
|
+
|
158
|
+
def length
|
159
|
+
refs.length
|
160
|
+
end
|
161
|
+
|
162
|
+
def size
|
163
|
+
refs.size
|
164
|
+
end
|
162
165
|
end
|
163
166
|
|
164
167
|
class EventsPage < Fauna::Resource
|
165
168
|
include Enumerable
|
166
169
|
|
167
170
|
def events
|
168
|
-
@events ||= struct['events'].
|
171
|
+
@events ||= struct['events'].collect { |e| Event.new(e) }
|
169
172
|
end
|
170
173
|
|
171
174
|
def each(&block)
|
@@ -176,9 +179,17 @@ module Fauna
|
|
176
179
|
events.empty?
|
177
180
|
end
|
178
181
|
|
179
|
-
def fauna_count
|
180
|
-
|
181
|
-
|
182
|
+
def fauna_count
|
183
|
+
struct['count']
|
184
|
+
end
|
185
|
+
|
186
|
+
def length
|
187
|
+
events.length
|
188
|
+
end
|
189
|
+
|
190
|
+
def size
|
191
|
+
events.size
|
192
|
+
end
|
182
193
|
end
|
183
194
|
|
184
195
|
class Event
|
@@ -223,7 +234,7 @@ module Fauna
|
|
223
234
|
end
|
224
235
|
|
225
236
|
def editable?
|
226
|
-
|
237
|
+
%w(create delete).include? action
|
227
238
|
end
|
228
239
|
end
|
229
240
|
end
|
data/lib/fauna/util.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Fauna
|
2
2
|
def self.time_from_usecs(microseconds)
|
3
|
-
Time.at(microseconds/1_000_000, microseconds % 1_000_000)
|
3
|
+
Time.at(microseconds / 1_000_000, microseconds % 1_000_000)
|
4
4
|
end
|
5
5
|
|
6
6
|
def self.usecs_from_time(time)
|
7
|
-
time.to_i *
|
7
|
+
time.to_i * 1_000_000 + time.usec
|
8
8
|
end
|
9
9
|
end
|
data/lib/tasks/fauna.rake
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
task :environment
|
3
3
|
|
4
4
|
namespace :fauna do
|
5
|
-
desc
|
5
|
+
desc 'Migrate your Fauna database to the latest version of your schema'
|
6
6
|
task :migrate => :environment do
|
7
7
|
puts "Migrating #{Rails.env} Fauna database schema"
|
8
8
|
Fauna::Client.context(Fauna.connection) do
|
@@ -10,14 +10,14 @@ namespace :fauna do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
desc
|
13
|
+
desc 'Completely reset your Fauna database'
|
14
14
|
task :reset => :environment do
|
15
15
|
if Rails.env.production?
|
16
16
|
puts "Won't reset #{Rails.env} Fauna database"
|
17
17
|
else
|
18
18
|
Fauna::Client.context(Fauna.root_connection) do
|
19
19
|
puts "Resetting #{Rails.env} Fauna database"
|
20
|
-
Fauna::Client.delete(
|
20
|
+
Fauna::Client.delete('everything')
|
21
21
|
end
|
22
22
|
Fauna.auth!
|
23
23
|
end
|
@@ -29,8 +29,8 @@ namespace :fauna do
|
|
29
29
|
Fauna::Client.context(Fauna.connection) do
|
30
30
|
FileUtils.mkdir_p(Fauna::FIXTURES_DIR)
|
31
31
|
|
32
|
-
class_configs = Fauna.connection.get(
|
33
|
-
class_configs[
|
32
|
+
class_configs = Fauna.connection.get('/classes')['references'] || {}
|
33
|
+
class_configs['users'] = nil
|
34
34
|
|
35
35
|
Dir.chdir(Fauna::FIXTURES_DIR) do
|
36
36
|
class_configs.each do |ref, value|
|
@@ -38,10 +38,10 @@ namespace :fauna do
|
|
38
38
|
FileUtils.mkdir_p(class_name)
|
39
39
|
|
40
40
|
Dir.chdir(class_name) do
|
41
|
-
# FIXME shouldn't round trip JSON
|
42
|
-
File.open(
|
43
|
-
(Fauna.connection.get(class_name)[
|
44
|
-
File.open("#{
|
41
|
+
# FIXME: shouldn't round trip JSON
|
42
|
+
File.open('config.json', 'w') { |f| f.write(value.to_json) } if value
|
43
|
+
(Fauna.connection.get(class_name)['references'] || {}).each do |inner_ref, inner_value|
|
44
|
+
File.open("#{inner_ref.split('/').last}.json", 'w') { |f| f.write(inner_value.to_json) }
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
@@ -49,17 +49,17 @@ namespace :fauna do
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
desc
|
52
|
+
desc 'Load the contents of your Fauna database'
|
53
53
|
task :load => :environment do
|
54
54
|
puts "Loading #{Rails.env} Fauna database contents from #{Fauna::FIXTURES_DIR}"
|
55
55
|
Fauna::Client.context(Fauna.connection) do
|
56
56
|
Dir.chdir(Fauna::FIXTURES_DIR) do
|
57
|
-
Dir[
|
57
|
+
Dir['**/*.json'].collect do |filename|
|
58
58
|
value = JSON.parse(File.open(filename) { |f| f.read })
|
59
59
|
begin
|
60
|
-
Fauna.connection.put(value[
|
60
|
+
Fauna.connection.put(value['ref'], value)
|
61
61
|
rescue Fauna::Connection::NotFound
|
62
|
-
Fauna.connection.post(value[
|
62
|
+
Fauna.connection.post(value['ref'].split('/')[0..-2].join('/'), value)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
@@ -68,4 +68,4 @@ namespace :fauna do
|
|
68
68
|
|
69
69
|
end
|
70
70
|
|
71
|
-
task :test => [
|
71
|
+
task :test => ['fauna:reset', 'fauna:migrate', 'fauna:load']
|
data/test/class_test.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
require File.expand_path('../test_helper', __FILE__)
|
2
2
|
|
3
3
|
class ClassTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
4
|
def setup
|
6
5
|
super
|
7
6
|
@model = Fauna::Resource.new('classes/pigs')
|
8
7
|
end
|
9
8
|
|
9
|
+
def test_new
|
10
|
+
settings = Fauna::Resource.new('settings', :ref => 'settings/12345678', :email => 'test@test.com')
|
11
|
+
user = Fauna::Resource.new('users', :ref => 'users/87654321')
|
12
|
+
assert user.data.object_id != settings.data.object_id
|
13
|
+
end
|
14
|
+
|
10
15
|
def test_create
|
11
16
|
pig = Fauna::Resource.create 'classes/pigs', :data => { :visited => true }
|
12
17
|
assert_equal true, pig.data['visited']
|
data/test/client_test.rb
CHANGED
@@ -3,12 +3,12 @@ require File.expand_path('../test_helper', __FILE__)
|
|
3
3
|
class ClientTest < MiniTest::Unit::TestCase
|
4
4
|
def setup
|
5
5
|
super
|
6
|
-
@attributes = {
|
6
|
+
@attributes = { 'name' => 'Princess Eilonwy', 'email' => email, 'password' => password }
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_database_context
|
10
10
|
Fauna::Client.context(@server_connection) do
|
11
|
-
user = Fauna::Client.post(
|
11
|
+
user = Fauna::Client.post('users', @attributes)
|
12
12
|
user = Fauna::Client.get(user['ref'])
|
13
13
|
Fauna::Client.delete(user['ref'])
|
14
14
|
end
|
@@ -16,10 +16,11 @@ class ClientTest < MiniTest::Unit::TestCase
|
|
16
16
|
|
17
17
|
def test_client_context
|
18
18
|
Fauna::Client.context(@client_connection) do
|
19
|
-
|
19
|
+
Fauna::Resource.create('users', @attributes)
|
20
|
+
|
20
21
|
Fauna::Client.context(@client_connection) do
|
21
22
|
assert_raises(Fauna::Connection::PermissionDenied) do
|
22
|
-
|
23
|
+
Fauna::Resource.create('classes/posts', @attributes)
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
@@ -27,11 +28,11 @@ class ClientTest < MiniTest::Unit::TestCase
|
|
27
28
|
|
28
29
|
def test_token_context
|
29
30
|
Fauna::Client.context(@server_connection) do
|
30
|
-
Fauna::Client.post(
|
31
|
+
Fauna::Client.post('users', @attributes)
|
31
32
|
end
|
32
33
|
|
33
34
|
Fauna::Client.context(@client_connection) do
|
34
|
-
@token = Fauna::Client.post(
|
35
|
+
@token = Fauna::Client.post('tokens', @attributes)
|
35
36
|
end
|
36
37
|
|
37
38
|
Fauna::Client.context(Fauna::Connection.new(:secret => @token['secret'], :domain => @server_connection.domain, :scheme => @server_connection.scheme, :port => @server_connection.port)) do
|
@@ -42,7 +43,7 @@ class ClientTest < MiniTest::Unit::TestCase
|
|
42
43
|
|
43
44
|
def test_caching_1
|
44
45
|
Fauna::Client.context(@server_connection) do
|
45
|
-
user = Fauna::Client.post(
|
46
|
+
user = Fauna::Client.post('users', @attributes)
|
46
47
|
@server_connection.expects(:get).never
|
47
48
|
Fauna::Client.get(user['ref'])
|
48
49
|
end
|
@@ -50,7 +51,7 @@ class ClientTest < MiniTest::Unit::TestCase
|
|
50
51
|
|
51
52
|
def test_caching_2
|
52
53
|
Fauna::Client.context(@client_connection) do
|
53
|
-
user = Fauna::Client.post(
|
54
|
+
user = Fauna::Client.post('users', @attributes)
|
54
55
|
|
55
56
|
Fauna::Client.context(@server_connection) do
|
56
57
|
Fauna::Client.get(user['ref'])
|
data/test/connection_test.rb
CHANGED
@@ -3,47 +3,47 @@ require File.expand_path('../test_helper', __FILE__)
|
|
3
3
|
class ConnectionTest < MiniTest::Unit::TestCase
|
4
4
|
def setup
|
5
5
|
super
|
6
|
-
@attributes = {
|
6
|
+
@attributes = { 'name' => 'Arawn', 'email' => email, 'password' => password }
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_get
|
10
|
-
@server_connection.get(
|
10
|
+
@server_connection.get('users/instances')
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_get_with_invalid_key
|
14
14
|
connection = Fauna::Connection.new(:secret => 'bad_key', :domain => @server_connection.domain, :scheme => @server_connection.scheme, :port => @server_connection.port)
|
15
15
|
assert_raises(Fauna::Connection::Unauthorized) do
|
16
|
-
connection.get(
|
16
|
+
connection.get('users/instances')
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_post
|
21
|
-
@client_connection.post(
|
21
|
+
@client_connection.post('users', @attributes)['resource']
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_put
|
25
|
-
user = @server_connection.post(
|
26
|
-
user = @server_connection.put(user['ref'],
|
25
|
+
user = @server_connection.post('users', @attributes)['resource']
|
26
|
+
user = @server_connection.put(user['ref'], :data => { :pockets => 2 })['resource']
|
27
27
|
|
28
28
|
assert_equal 2, user['data']['pockets']
|
29
29
|
|
30
|
-
user = @server_connection.put(user['ref'],
|
30
|
+
user = @server_connection.put(user['ref'], :data => { :apples => 3 })['resource']
|
31
31
|
|
32
32
|
assert_nil user['data']['pockets']
|
33
33
|
assert_equal 3, user['data']['apples']
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_patch
|
37
|
-
user = @server_connection.post(
|
38
|
-
user = @server_connection.patch(user['ref'],
|
39
|
-
user = @server_connection.patch(user['ref'],
|
37
|
+
user = @server_connection.post('users', @attributes)['resource']
|
38
|
+
user = @server_connection.patch(user['ref'], :data => { :pockets => 2 })['resource']
|
39
|
+
user = @server_connection.patch(user['ref'], :data => { :apples => 3 })['resource']
|
40
40
|
|
41
41
|
assert_equal 2, user['data']['pockets']
|
42
42
|
assert_equal 3, user['data']['apples']
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_delete
|
46
|
-
user = @server_connection.post(
|
46
|
+
user = @server_connection.post('users', @attributes)['resource']
|
47
47
|
@server_connection.delete(user['ref'])
|
48
48
|
assert_raises(Fauna::Connection::NotFound) do
|
49
49
|
@server_connection.get(user['ref'])
|
@@ -61,6 +61,6 @@ class ConnectionTest < MiniTest::Unit::TestCase
|
|
61
61
|
# raise RestClient::ServerBrokeConnection
|
62
62
|
# end
|
63
63
|
# end
|
64
|
-
# @server_connection.get(
|
64
|
+
# @server_connection.get('users/instances')
|
65
65
|
# end
|
66
66
|
end
|