fauna 0.2.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,197 +0,0 @@
1
-
2
- module Fauna
3
- class SetRef
4
- def initialize(attrs)
5
- @attrs = attrs
6
- end
7
-
8
- def ts
9
- Resource.time_from_usecs(@attrs['ts'])
10
- end
11
-
12
- def resource_ref
13
- @attrs['resource']
14
- end
15
-
16
- def resource
17
- Fauna::Resource.find_by_ref(resource_ref)
18
- end
19
- end
20
-
21
- class Event < SetRef
22
- def set_ref
23
- @attrs['set']
24
- end
25
-
26
- def action
27
- @attrs['action']
28
- end
29
-
30
- def set
31
- EventSet.new(set_ref)
32
- end
33
- end
34
-
35
- class EventsPage < Fauna::Resource
36
- include Enumerable
37
-
38
- def self.find(ref, query = nil)
39
- if query
40
- query = query.merge(:before => usecs_from_time(query[:before])) if query[:before]
41
- query = query.merge(:after => usecs_from_time(query[:after])) if query[:after]
42
- end
43
-
44
- alloc(Fauna::Client.get(ref, query).to_hash)
45
- end
46
-
47
- def before
48
- struct['before'] ? Resource.time_from_usecs(struct['before']) : nil
49
- end
50
-
51
- def after
52
- struct['after'] ? Resource.time_from_usecs(struct['after']) : nil
53
- end
54
-
55
- def events
56
- @events ||= struct['events'].map { |e| Event.new(e) }
57
- end
58
-
59
- def each(&block)
60
- events.each(&block)
61
- end
62
-
63
- def empty?
64
- events.empty?
65
- end
66
- end
67
-
68
- class RefsPage < Fauna::Resource
69
- include Enumerable
70
-
71
- def self.find(ref, query = nil)
72
- alloc(Fauna::Client.get(ref, query).to_hash)
73
- end
74
-
75
- def refs
76
- @refs ||= struct['events'].map { |e| SetRef.new(e) }
77
- end
78
-
79
- def resources
80
- refs.map(&:resource)
81
- end
82
-
83
- def empty?
84
- events.empty?
85
- end
86
- end
87
-
88
- class EventSet
89
- attr_reader :ref
90
-
91
- def initialize(ref)
92
- @ref = ref
93
- end
94
-
95
- def page(query = nil)
96
- EventsPage.find(ref, query)
97
- end
98
-
99
- def creates(query = nil)
100
- RefsPage.find("#{ref}/creates", query)
101
- end
102
-
103
- def updates(query = nil)
104
- RefsPage.find("#{ref}/updates", query)
105
- end
106
-
107
- def events(query = nil)
108
- page(query).events
109
- end
110
-
111
- def resources(query = nil)
112
- creates(query).resources
113
- end
114
-
115
- def self.join(*args)
116
- EventSetQuery.new('join', *args)
117
- end
118
-
119
- def self.union(*args)
120
- EventSetQuery.new('union', *args)
121
- end
122
-
123
- def self.intersection(*args)
124
- EventSetQuery.new('intersection', *args)
125
- end
126
-
127
- def self.difference(*args)
128
- EventSetQuery.new('difference', *args)
129
- end
130
-
131
- def self.query(&block)
132
- module_eval(&block)
133
- end
134
- end
135
-
136
- class CustomEventSet < EventSet
137
- def add(resource)
138
- self.class.add(ref, resource)
139
- end
140
-
141
- def remove(resource)
142
- self.class.remove(ref, resource)
143
- end
144
-
145
- def self.add(ref, resource)
146
- resource = resource.ref if resource.respond_to?(:ref)
147
- Fauna::Client.post(ref, 'resource' => resource)
148
- end
149
-
150
- def self.remove(ref, resource)
151
- resource = resource.ref if resource.respond_to?(:ref)
152
- Fauna::Client.delete(ref, 'resource' => resource)
153
- end
154
- end
155
-
156
- class EventSetQuery < EventSet
157
- def initialize(function, *params)
158
- @function = function
159
- @params = params
160
- end
161
-
162
- def query
163
- @query ||=
164
- begin
165
- pstrs = @params.map do |p|
166
- p.respond_to?(:query) ? p.query : "'#{p.respond_to?(:ref) ? p.ref : p}'"
167
- end
168
-
169
- "#{@function}(#{pstrs.join(',')})"
170
- end
171
- end
172
-
173
- def ref
174
- "query?query=#{query}"
175
- end
176
-
177
- def page(query = nil)
178
- EventsPage.find("query", (query || {}).merge(:query => self.query))
179
- end
180
-
181
- def creates(query = nil)
182
- RefsPage.find("query/creates", (query || {}).merge(:query => self.query))
183
- end
184
-
185
- def updates(query = nil)
186
- RefsPage.find("query/updates", (query || {}).merge(:query => self.query))
187
- end
188
-
189
- end
190
-
191
- class EventSetConfig < Fauna::Resource
192
- def initialize(parent_class, name, attrs = {})
193
- super(attrs)
194
- struct['ref'] = "#{parent_class}/sets/#{name}/config"
195
- end
196
- end
197
- end
data/lib/fauna/model.rb DELETED
@@ -1,73 +0,0 @@
1
- module Fauna
2
- class Model < Resource
3
- def self.inherited(base)
4
- base.send :extend, ActiveModel::Naming
5
- base.send :include, ActiveModel::Validations
6
- base.send :include, ActiveModel::Conversion
7
-
8
- # Callbacks support
9
- base.send :extend, ActiveModel::Callbacks
10
- base.send :include, ActiveModel::Validations::Callbacks
11
- base.send :define_model_callbacks, :save, :create, :update, :destroy
12
-
13
- # Serialization
14
- base.send :include, ActiveModel::Serialization
15
- end
16
-
17
- def self.all
18
- Fauna::EventSet.new(fauna_class)
19
- end
20
-
21
- def self.find_by_ref(ref)
22
- Fauna::Resource.find_by_ref(URI.escape(ref))
23
- end
24
-
25
- def self.find_by_unique_id(unique_id)
26
- find_by_ref("#{fauna_class}/unique_id/#{unique_id}")
27
- end
28
-
29
- def self.find(id)
30
- find_by_ref("#{fauna_class}/#{id}")
31
- end
32
-
33
- class << self
34
- alias find_by_id find
35
- end
36
-
37
- def id
38
- ref.split("/").last
39
- end
40
-
41
- def save
42
- if valid?
43
- run_callbacks(:save) do
44
- if new_record?
45
- run_callbacks(:create) { super }
46
- else
47
- run_callbacks(:update) { super }
48
- end
49
- end
50
- else
51
- false
52
- end
53
- end
54
-
55
- def delete
56
- run_callbacks(:destroy) { super }
57
- end
58
-
59
- def valid?
60
- run_callbacks(:validate) { super }
61
- end
62
-
63
- def to_model
64
- self
65
- end
66
-
67
- private
68
-
69
- def post
70
- Fauna::Client.post(self.class.fauna_class, struct)
71
- end
72
- end
73
- end
@@ -1,32 +0,0 @@
1
- module Fauna
2
- class ClassConfig < Fauna::Resource; end
3
-
4
- class Class < Fauna::Model
5
- class << self
6
- def inherited(base)
7
- fc = name.split("::").last.underscore
8
- Fauna.add_class(fc, base) unless Fauna.exists_class_for_name?(fc)
9
- end
10
-
11
- def config_ref
12
- "#{fauna_class}/config"
13
- end
14
-
15
- def data
16
- Fauna::Resource.find_by_ref(config_ref).data
17
- end
18
-
19
- def update_data!(hash = {})
20
- meta = Fauna::Resource.find_by_ref(config_ref)
21
- block_given? ? yield(meta.data) : meta.data = hash
22
- meta.save!
23
- end
24
-
25
- def update_data(hash = {})
26
- meta = Fauna::Resource.find_by_ref(config_ref)
27
- block_given? ? yield(meta.data) : meta.data = hash
28
- meta.save
29
- end
30
- end
31
- end
32
- end
@@ -1,23 +0,0 @@
1
-
2
- module Fauna
3
- class User < Fauna::Model
4
-
5
- class Config < Fauna::Resource; end
6
-
7
- def self.self
8
- find_by_ref("users/self")
9
- end
10
-
11
- def self.find_by_email(email)
12
- find_by_ref("users/email/#{email}")
13
- end
14
-
15
- def config
16
- Fauna::User::Config.find_by_ref("#{ref}/config")
17
- end
18
-
19
- # set on user create
20
- def email; struct['email']; end
21
- def password; struct['password']; end
22
- end
23
- end
@@ -1,8 +0,0 @@
1
-
2
- module Fauna
3
- class Publisher < Fauna::Resource
4
- def self.find
5
- find_by_ref("publisher")
6
- end
7
- end
8
- end
@@ -1,23 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- class AssociationTest < MiniTest::Unit::TestCase
4
-
5
- def setup
6
- super
7
-
8
- @pig = Pig.create!(:name => "Henwen")
9
- @vision = Vision.create!(:pronouncement => "It was a dark and stormy night...", :pig => @pig)
10
- @pig.visions.add @vision
11
- end
12
-
13
- def test_event_set
14
- assert_equal @pig.visions.page.events.first.resource, @vision
15
- assert @pig.visions.page.events.first.resource.pronouncement
16
- end
17
-
18
- def test_reference
19
- assert_equal @vision.pig, @pig
20
- assert_equal @vision.pig_ref, @pig.ref
21
- assert @vision.pig.visions
22
- end
23
- end
@@ -1,92 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- # TODO use association_test classes
4
-
5
- class EventSetTest < ActiveModel::TestCase
6
- # include ActiveModel::Lint::Tests
7
-
8
- def setup
9
- super
10
- @model = MessageBoard.create!
11
- end
12
-
13
- def test_page
14
- page = @model.posts.page
15
- assert_equal "#{@model.ref}/sets/posts", page.ref
16
- assert_equal 0, page.events.size
17
- end
18
-
19
- def test_pagination
20
- @model.posts.add(Post.create(:body => "Flewdyr Flam, called also Flewdyr Wledig."))
21
- @model.posts.add(Post.create(:body => "Recorded in the Triads as one of the three sovereigns."))
22
- @model.posts.add(Post.create(:body => "They preferred remaining as knights in the court of Arthur."))
23
- @model.posts.add(Post.create(:body => "Even so, they had dominions of their own."))
24
- @model.posts.add(Post.create(:body => "He is mentioned in the Mabinogi of Cilhwch and Olwen."))
25
-
26
- page1 = @model.posts.page(:size => 2)
27
- assert_equal 2, page1.events.size
28
- page2 = @model.posts.page(:size => 2, :before => page1.before)
29
- assert_equal 2, page2.events.size
30
- page3 = @model.posts.page(:size => 2, :before => page2.before)
31
- assert_equal 1, page3.events.size
32
-
33
- page4 = @model.posts.page(:size => 2, :after => page3.events.last.ts)
34
- assert_equal 2, page4.events.size
35
- page5 = @model.posts.page(:size => 2, :after => page4.after)
36
- assert_equal 2, page5.events.size
37
- page6 = @model.posts.page(:size => 2, :after => page5.after)
38
- assert_equal 1, page6.events.size
39
- end
40
-
41
- def test_any
42
- @model.posts.add(Post.create(:body => "Hello"))
43
- assert @model.posts.page.any?
44
- assert @model.posts.page.events.any?
45
- end
46
-
47
- def test_event_set_add
48
- @model.posts.add(Post.create(:body => "Goodbye"))
49
- page = @model.posts.page
50
- assert_equal 1, page.events.size
51
- assert_equal "Goodbye", page.events[0].resource.body
52
- end
53
-
54
- def test_event_set_remove
55
- @model.posts.add(Post.create(:body => "Hello"))
56
- page = @model.posts.page
57
- assert_equal 1, page.events.size
58
- @model.posts.remove(page.events[0].resource)
59
- end
60
-
61
- def test_event_set_resources
62
- post = Post.create(:body => "Hello")
63
- @model.posts.add(post)
64
- assert_equal [post], @model.posts.resources
65
- assert_equal [post], @model.posts.creates.resources
66
- assert_equal [post], @model.posts.updates.resources
67
- end
68
-
69
- def test_event_set_query
70
- posts = (1..3).map do |i|
71
- Post.create(:body => "p#{i}").tap do |p|
72
- @model.posts.add(p)
73
- end
74
- end
75
-
76
- comments = posts.map do |p|
77
- (1..3).map do |i|
78
- Comment.create(:body => "#{p.body}_c#{i}").tap do |c|
79
- p.comments.add(c)
80
- end
81
- end
82
- end
83
-
84
- q = Fauna::EventSet.join(@model.posts, 'sets/comments')
85
-
86
- assert_equal 9, q.resources.size
87
-
88
- comments.flatten.each do |c|
89
- assert q.resources.include?(c)
90
- end
91
- end
92
- end