fauna 0.2.2 → 0.2.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.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.2.3 Event set API enhancements
2
+
1
3
  v0.2.2 Support for Fauna API version 1
2
4
 
3
5
  v0.1.2 Fauna::User.find_by_external_id returns a User or nil, not an Array.
data/Manifest CHANGED
@@ -1,7 +1,6 @@
1
1
  CHANGELOG
2
2
  Gemfile
3
3
  LICENSE
4
- Manifest
5
4
  README.md
6
5
  Rakefile
7
6
  fauna.gemspec
@@ -27,3 +26,4 @@ test/model/user_test.rb
27
26
  test/model/validation_test.rb
28
27
  test/readme_test.rb
29
28
  test/test_helper.rb
29
+ Manifest
data/fauna.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "fauna"
5
- s.version = "0.2.2"
5
+ s.version = "0.2.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Fauna, Inc."]
9
- s.date = "2013-03-02"
9
+ s.date = "2013-03-03"
10
10
  s.description = "Official Ruby client for the Fauna API."
11
11
  s.email = ""
12
12
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "lib/fauna.rb", "lib/fauna/client.rb", "lib/fauna/connection.rb", "lib/fauna/ddl.rb", "lib/fauna/model.rb", "lib/fauna/model/class.rb", "lib/fauna/model/event_set.rb", "lib/fauna/model/publisher.rb", "lib/fauna/model/user.rb", "lib/fauna/rails.rb", "lib/fauna/resource.rb"]
data/lib/fauna.rb CHANGED
@@ -33,7 +33,7 @@ module Fauna
33
33
  DEFAULT_BLOCK = proc do
34
34
  with User, class_name: "users"
35
35
  with User::Config, class_name: "users/config"
36
- with EventSetPage, class_name: "sets"
36
+ with EventsPage, class_name: "sets"
37
37
  with EventSetConfig, class_name: "sets/config"
38
38
  with ClassConfig, class_name: "classes/config"
39
39
  with Publisher, class_name: "publisher"
data/lib/fauna/model.rb CHANGED
@@ -14,18 +14,13 @@ module Fauna
14
14
  base.send :include, ActiveModel::Serialization
15
15
  end
16
16
 
17
- # TODO: use proper class here
18
- def self.find_by_id(id)
19
- ref =
20
- if self <= Fauna::User::Config
21
- "users/#{id}/config"
22
- else
23
- "#{fauna_class}/#{id}"
24
- end
25
-
26
- Fauna::Resource.find(ref)
17
+ def self.all
18
+ Fauna::EventSet.new(fauna_class)
27
19
  end
28
20
 
21
+ def self.find_by_id(id)
22
+ Fauna::Resource.find("#{fauna_class}/#{id}")
23
+ end
29
24
 
30
25
  def self.find_by_unique_id(unique_id)
31
26
  find("#{fauna_class}/unique_id/#{unique_id}")
@@ -60,5 +55,11 @@ module Fauna
60
55
  def to_model
61
56
  self
62
57
  end
58
+
59
+ private
60
+
61
+ def post
62
+ Fauna::Client.post(self.class.fauna_class, struct)
63
+ end
63
64
  end
64
65
  end
@@ -28,11 +28,5 @@ module Fauna
28
28
  meta.save
29
29
  end
30
30
  end
31
-
32
- private
33
-
34
- def post
35
- Fauna::Client.post(self.class.fauna_class, struct)
36
- end
37
31
  end
38
32
  end
@@ -1,46 +1,69 @@
1
1
 
2
2
  module Fauna
3
- class Event
4
-
5
- attr_reader :ts, :set_ref, :resource_ref, :action
3
+ class SetRef
4
+ attr_reader :ts, :resource_ref
6
5
 
7
6
  def initialize(attrs)
8
- # TODO v1
9
7
  @ts = attrs['ts']
10
- @set_ref = attrs['set']
11
8
  @resource_ref = attrs['resource']
12
- @action = attrs['action']
13
9
  end
14
10
 
15
11
  def resource
16
12
  Fauna::Resource.find(resource_ref)
17
13
  end
14
+ end
15
+
16
+ class Event < SetRef
17
+ attr_reader :set_ref, :action
18
+
19
+ def initialize(attrs)
20
+ super(attrs)
21
+ @set_ref = attrs['set']
22
+ @action = attrs['action']
23
+ end
18
24
 
19
25
  def set
20
26
  EventSet.new(set_ref)
21
27
  end
22
28
  end
23
29
 
24
- class EventSetPage < Fauna::Resource
30
+ class EventsPage < Fauna::Resource
31
+ include Enumerable
32
+
33
+ def self.find(ref, query = nil)
34
+ alloc(Fauna::Client.get(ref, query).to_hash)
35
+ end
36
+
25
37
  def events
26
38
  @events ||= struct['events'].map { |e| Event.new(e) }
27
39
  end
28
40
 
29
- def any?
30
- struct['events'].any?
41
+ def each(&block)
42
+ events.each(&block)
43
+ end
44
+
45
+ def empty?
46
+ events.empty?
47
+ end
48
+ end
49
+
50
+ class RefsPage < Fauna::Resource
51
+ include Enumerable
52
+
53
+ def self.find(ref, query = nil)
54
+ alloc(Fauna::Client.get(ref, query).to_hash)
55
+ end
56
+
57
+ def refs
58
+ @refs ||= struct['events'].map { |e| SetRef.new(e) }
31
59
  end
32
60
 
33
61
  def resources
34
- # TODO duplicates can exist in the local event_set. remove w/ v1
35
- seen = {}
36
- events.inject([]) do |a, ev|
37
- if (ev.action == 'create' && !seen[ev.resource_ref])
38
- seen[ev.resource_ref] = true
39
- a << ev.resource
40
- end
62
+ refs.map(&:resource)
63
+ end
41
64
 
42
- a
43
- end
65
+ def empty?
66
+ events.empty?
44
67
  end
45
68
  end
46
69
 
@@ -52,7 +75,15 @@ module Fauna
52
75
  end
53
76
 
54
77
  def page(query = nil)
55
- EventSetPage.find(ref, query)
78
+ EventsPage.find(ref, query)
79
+ end
80
+
81
+ def creates(query = nil)
82
+ RefsPage.find("#{ref}/creates", query)
83
+ end
84
+
85
+ def updates(query = nil)
86
+ RefsPage.find("#{ref}/updates", query)
56
87
  end
57
88
 
58
89
  def events(query = nil)
@@ -60,7 +91,7 @@ module Fauna
60
91
  end
61
92
 
62
93
  def resources(query = nil)
63
- page(query).resources
94
+ creates(query).resources
64
95
  end
65
96
 
66
97
  def add(resource)
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Fauna
3
- class Publisher < Fauna::Model
3
+ class Publisher < Fauna::Resource
4
4
  def self.find
5
5
  super("publisher")
6
6
  end
@@ -2,7 +2,7 @@
2
2
  module Fauna
3
3
  class User < Fauna::Model
4
4
 
5
- class Config < Fauna::Model; end
5
+ class Config < Fauna::Resource; end
6
6
 
7
7
  def self.self
8
8
  find("users/self")
@@ -12,18 +12,12 @@ module Fauna
12
12
  find("users/email/#{email}")
13
13
  end
14
14
 
15
- def email; struct['email']; end
16
-
17
- def password; struct['password']; end
18
-
19
15
  def config
20
16
  Fauna::User::Config.find("#{ref}/config")
21
17
  end
22
18
 
23
- private
24
-
25
- def post
26
- Fauna::Client.post("users", struct)
27
- end
19
+ # set on user create
20
+ def email; struct['email']; end
21
+ def password; struct['password']; end
28
22
  end
29
23
  end
@@ -27,6 +27,11 @@ class ClassTest < ActiveModel::TestCase
27
27
  assert pig.ref
28
28
  end
29
29
 
30
+ def test_all
31
+ pig = Pig.create(:visited => false)
32
+ assert Pig.all.resources.include?(pig)
33
+ end
34
+
30
35
  def test_save
31
36
  pig = Pig.new(:visited => false)
32
37
  pig.save
@@ -35,4 +35,12 @@ class EventSetTest < ActiveModel::TestCase
35
35
  assert_equal page.events.size, 1
36
36
  @model.posts.remove(page.events[0].resource)
37
37
  end
38
+
39
+ def test_event_set_resources
40
+ post = Post.create(:body => "Hello")
41
+ @model.posts.add(post)
42
+ assert_equal [post], @model.posts.resources
43
+ assert_equal [post], @model.posts.creates.resources
44
+ assert_equal [post], @model.posts.updates.resources
45
+ end
38
46
  end
@@ -16,6 +16,11 @@ class UserTest < ActiveModel::TestCase
16
16
  assert user.ref
17
17
  end
18
18
 
19
+ def test_all
20
+ user = Fauna::User.create(@attributes)
21
+ assert Fauna::User.all.resources.include?(user)
22
+ end
23
+
19
24
  def test_save
20
25
  user = Fauna::User.new(@attributes)
21
26
  user.save
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fauna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-02 00:00:00.000000000 Z
12
+ date: 2013-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel