medea 0.2.31 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
2
+ require 'rspec/core/rake_task'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ desc "Run all examples"
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ #task :default => :spec
@@ -79,7 +79,7 @@ module Medea
79
79
  @state = :prefetch
80
80
  end
81
81
 
82
- def remove! member
82
+ def remove! member, cascade=false
83
83
  raise RuntimeError, "You can only remove an item if you are accessing this list from an object." unless @parent.is_a? JasonObject
84
84
  raise ArgumentError, "You can only remove #{@type.name} items from this collection!" unless member.is_a? @type
85
85
  raise ArgumentError, "This item (#{member.jason_key}) doesn't exist in the list you're trying to remove it from!" unless self.include? member
@@ -87,11 +87,11 @@ module Medea
87
87
  if @list_type == :value
88
88
  member.jason_parent = nil
89
89
  member.jason_parent_list = nil
90
- member.save!
90
+ member.delete! if cascade
91
91
  elsif @list_type == :reference
92
92
 
93
93
  #send DELETE to JasonDB::db_auth_url/a_class.name/
94
- url = "#{JasonDB::db_auth_url}#{@type.name}/#{@parent.jason_key}/#{@list_name}/#{member.jason_key}"
94
+ url = "#{JasonDB::db_auth_url}#{@parent.class.name}/#{@parent.jason_key}/#{@list_name}/#{member.jason_key}"
95
95
 
96
96
  response = RestClient.delete url
97
97
 
@@ -80,6 +80,11 @@ module Medea
80
80
  end
81
81
  end
82
82
 
83
+ def ==(other)
84
+ return false if not other.is_a? JasonObject
85
+ jason_key == other.jason_key
86
+ end
87
+
83
88
  #"flexihash" access interface
84
89
  def []=(key, value)
85
90
  @__jason_data ||= {}
@@ -219,7 +224,19 @@ module Medea
219
224
  @__jason_state = :stale
220
225
  end
221
226
 
222
- def delete!
227
+ def delete! cascade=false
228
+ #TODO: Put this into some kind of async method or have JasonDB able to set flags on many records at once
229
+ #This will be REALLY REALLY slowww!
230
+ if cascade && (self.class.class_variable_defined? :@@lists)
231
+ @@lists.keys.each do |list_name|
232
+ #for each list that I have
233
+ list = send(list_name)
234
+ list.each do |item|
235
+ #remove each item from the list, deleting it if possible
236
+ list.remove! item, true
237
+ end
238
+ end
239
+ end
223
240
  url = "#{JasonDB::db_auth_url}#{self.class.name}/#{self.jason_key}"
224
241
  response = RestClient.delete url
225
242
  raise "DELETE failed!" unless response.code == 201
data/lib/medea/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Medea
2
- VERSION = "0.2.31"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe "JasonObject" do
4
+ before :each do
5
+ @user = User.new
6
+ end
7
+
8
+ after :each do
9
+ @user.delete!
10
+ end
11
+
12
+ it "should be comparable to other JasonObjects" do
13
+ @user.save!
14
+ (@user == User.new).should eq(false)
15
+ (@user == @user).should eq(true)
16
+ (@user == User.get_by_key(@user.jason_key, :lazy)).should eq(true)
17
+ (@user == User.get_by_key(@user.jason_key)).should eq(true)
18
+ (@user == "Hello world?").should eq(false)
19
+ (@user == @user.jason_key).should eq(false)
20
+ end
21
+
22
+ it "should be persistable" do
23
+ @user.name = "Freddy"
24
+ @user.save!
25
+
26
+ @user.jason_state.should eq(:stale)
27
+
28
+ retrieved_user = User.get_by_key @user.jason_key
29
+ retrieved_user.name.should eq(@user.name)
30
+ end
31
+
32
+ it "should track it's state" do
33
+ @user.jason_state.should eq(:new)
34
+ @user.save!
35
+ @user.name = "Freddy"
36
+ @user.jason_state.should eq(:dirty)
37
+ @user.save!
38
+ @user.jason_state.should eq(:stale)
39
+
40
+ retrieved_user = User.get_by_key @user.jason_key, :lazy
41
+ retrieved_user.jason_state.should eq(:ghost)
42
+ retrieved_user.name
43
+ retrieved_user.jason_state.should eq(:stale)
44
+ end
45
+ end
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ describe "list properties" do
4
+ before :all do
5
+ @user_list = []
6
+ @user = User.new
7
+ @user_list << @user
8
+ (1..4).collect do |i|
9
+ other_user = User.new
10
+ other_user.name = "Automaton #{i}"
11
+ @user_list << other_user
12
+ @user.followees.add! other_user
13
+ m = Message.new
14
+ m.message = "Hello world! #automaton"
15
+ other_user.messages.add! m
16
+ end
17
+
18
+ ["Hello? #user", "Is this thing on? #user", "I love Mondays! #user"].each do |m|
19
+ msg = Message.new
20
+ msg.message = m
21
+ @user.messages.add! msg
22
+ end
23
+
24
+ end
25
+
26
+ after :all do
27
+ @user_list.each do |user|
28
+ user.messages.each do |m|
29
+ user.remove! m, true
30
+ end
31
+ user.delete!
32
+ end
33
+ end
34
+
35
+ it "should add a method to the class" do
36
+ @user.respond_to?(:followees).should eq(true)
37
+ @user.respond_to?(:messages).should eq(true)
38
+ @user.respond_to?(:products).should eq(false)
39
+ end
40
+
41
+ it "should return a JasonListProperty" do
42
+ @user.followees.is_a?(Medea::JasonListProperty).should eq(true)
43
+ @user.messages.is_a?(Medea::JasonListProperty).should eq(true)
44
+ end
45
+
46
+ it "should resolve to objects" do
47
+ @user.followees.each do |f|
48
+ f.is_a?(Medea::JasonObject).should eq(true)
49
+ end
50
+
51
+ @user.messages.each do |m|
52
+ m.is_a?(Medea::JasonObject).should eq(true)
53
+ m.message.should include("#user")
54
+ end
55
+ end
56
+
57
+ it "should resolve sub-lists" do
58
+ @user.followees.messages.is_a?(Medea::JasonListProperty).should eq(true)
59
+ end
60
+
61
+ it "should resolve items in a sub-list as objects" do
62
+ @user.followees.messages.each do |msg|
63
+ msg.is_a?(Medea::JasonObject).should eq(true)
64
+ msg.message.should include("#automaton")
65
+ end
66
+ end
67
+
68
+ it "should count correctly" do
69
+ @user.messages.count.should eq(3)
70
+ @user.followees.messages.count.should eq(4)
71
+ @user.followees.count.should eq(4)
72
+ end
73
+ end
@@ -0,0 +1,19 @@
1
+ $: << File.join(File.dirname(__FILE__), '/../lib')
2
+ require 'rspec'
3
+ require 'rspec/mocks'
4
+ require 'medea'
5
+
6
+ class Message < Medea::JasonObject
7
+ end
8
+
9
+ class User < Medea::JasonObject
10
+ has_many :followees, User
11
+ owns_many :messages, Message
12
+ end
13
+
14
+ #mock the db url
15
+ module JasonDB
16
+ def JasonDB::db_auth_url mode=:secure
17
+ "https://michael:password@rest.jasondb.com/medea-test/"
18
+ end
19
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: medea
3
3
  version: !ruby/object:Gem::Version
4
- hash: 41
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 31
10
- version: 0.2.31
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Jensen
@@ -98,12 +98,10 @@ files:
98
98
  - lib/medea/jasonobject.rb
99
99
  - lib/medea/list_properties.rb
100
100
  - lib/medea/version.rb
101
- - lib/test.rb
102
- - lib/testjdq.rb
103
- - lib/testjlp.rb
104
- - lib/testmeta.rb
105
- - lib/testsublist.rb
106
101
  - medea.gemspec
102
+ - spec/jason_object_spec.rb
103
+ - spec/list_properties_spec.rb
104
+ - spec/spec_helper.rb
107
105
  has_rdoc: true
108
106
  homepage: ""
109
107
  licenses: []
data/lib/test.rb DELETED
@@ -1,31 +0,0 @@
1
- $: << "~/Projects/Medea/lib"
2
- require 'rubygems'
3
- require 'medea'
4
-
5
- class Person < Medea::JasonObject
6
- end
7
-
8
- mikey = Person.new
9
- puts "state: #{mikey.jason_state}"
10
- mikey.name = "Michael"
11
- mikey.age = 21
12
- mikey.location = {:longitude => -30.123213, :latitude => 130.1231458}
13
- puts mikey.jason_key
14
- puts mikey.to_json
15
- mikey.save!
16
-
17
- puts "state: #{mikey.jason_state}"
18
-
19
- puts "Changing name => Bob"
20
- mikey.name = "Bob"
21
-
22
- puts "state: #{mikey.jason_state}"
23
- puts mikey.to_json
24
- mikey.save!
25
- puts "state: #{mikey.jason_state}"
26
-
27
- puts "Enter a Person key to retrieve: "
28
- id = gets.strip
29
-
30
- person = Person.new id
31
- puts person.to_json
data/lib/testjdq.rb DELETED
@@ -1,41 +0,0 @@
1
- $: << "~/Projects/Medea/lib"
2
- require 'rubygems'
3
- require 'medea'
4
-
5
- class Person < Medea::JasonObject
6
- end
7
-
8
- class Company < Medea::JasonObject
9
- has_many :employees, Person
10
- end
11
-
12
- puts "Lets make a person!"
13
- p = Person.new
14
- puts "Name?"
15
- p.name = gets.strip
16
- puts "Age?"
17
- p.age = gets.strip.to_i
18
- puts "OK - Saving"
19
- p.save!
20
-
21
- puts "", "Lets make a company!"
22
- c = Company.new
23
- puts "Name?"
24
- c.name = gets.strip
25
- puts "Address?"
26
- c.address = gets.strip
27
- puts "OK - Saving"
28
- c.save!
29
-
30
- puts "", "Making #{p.name} a member of #{c.name}"
31
- c.employees.add! p
32
- puts "OK - Saving"
33
- p.save!
34
-
35
- puts "", "Now querying for Persons that are members of #{c.name}"
36
- r = c.employees
37
- puts "Query: #{r.to_url}"
38
- puts "Got #{r.count} items:"
39
- r.each do |p|
40
- puts p.name
41
- end
data/lib/testjlp.rb DELETED
@@ -1,31 +0,0 @@
1
- $: << "~/Projects/Medea/lib"
2
- require 'rubygems'
3
- require 'medea'
4
-
5
- class Person < Medea::JasonObject
6
- has_many :followees, Person
7
- end
8
-
9
- p = Person.get_by_key "pa76b65a2-ba64-4cf0-8b37-7ad6a30ee8db"
10
-
11
- puts p.followees.to_url
12
- puts p.followees.count
13
-
14
- puts "Let's make a new person!"
15
-
16
- p1 = Person.new
17
- puts "Name?"
18
- p1.name = gets.strip
19
- if p1.name != ""
20
- puts "Saving..."
21
- p1.save!
22
-
23
- puts "Making #{p.name} follow #{p1.name}..."
24
- p.followees.add! p1
25
- end
26
- puts "Done!"
27
- list = p.followees
28
- puts "#{p.name} now following #{list.count} users:"
29
- list.each do |f|
30
- puts " - #{f.jason_key}: #{f.name}\n"
31
- end
data/lib/testmeta.rb DELETED
@@ -1,39 +0,0 @@
1
- $: << "~/Projects/Medea/lib"
2
- require 'rubygems'
3
- require 'medea'
4
-
5
- class Message < Medea::JasonObject
6
- end
7
-
8
- class User < Medea::JasonObject
9
- owns_many :messages, Message
10
- end
11
-
12
- puts "Enter an id, or blank to make a new user:"
13
- id = gets.strip
14
- if id == ""
15
- u = User.new
16
- puts "User's name?"
17
- u.name = gets.strip
18
-
19
- puts "Saving"
20
- u.save!
21
- else
22
- u = User.get_by_key id
23
- puts "#{u.name} has posted #{u.messages.count} messages"
24
- end
25
-
26
- while true
27
- puts "Enter a message (blank to stop):"
28
- message = gets.strip
29
- break if message == ""
30
- m = Message.new
31
- m.message = message
32
- m.from = u.name
33
- u.messages.add! m
34
- end
35
-
36
- puts "Fetching messages..."
37
- u.messages.each do |e|
38
- puts " - #{e.message}\n"
39
- end
data/lib/testsublist.rb DELETED
@@ -1,46 +0,0 @@
1
- $: << "~/Projects/Medea/lib"
2
- require 'rubygems'
3
- require 'medea'
4
-
5
- class Message < Medea::JasonObject; end
6
-
7
- class User < Medea::JasonObject
8
- owns_many :messages, Message
9
- has_many :followees, User
10
- end
11
-
12
- u1 = User.new
13
- u1.name = "Fred"
14
- u1.save!
15
-
16
- u2 = User.new
17
- u2.name = "George"
18
- u2.save!
19
-
20
- u1.followees.add! u2
21
- u1.followees.add! u1
22
-
23
- m1 = Message.new
24
- m1.from = u2.name
25
- m1.message = "Hello! This is George"
26
- u2.messages.add! m1
27
-
28
- m3 = Message.new
29
- m3.from = u1.name
30
- m3.message = "George sent me here, hope it's fun!"
31
- u1.messages.add! m3
32
-
33
- m2 = Message.new
34
- m2.from = u2.name
35
- m2.message = "Man, this is a long day!"
36
- u2.messages.add! m2
37
-
38
- puts "#{u2.name} has posted #{u2.messages.count} messages"
39
-
40
- puts "#{u1.name} is following #{u1.followees.count} users"
41
- puts "#{u1.name}'s timeline has #{u1.followees.messages.count} messages in it"
42
-
43
- u1.followees.messages.each do |m|
44
- puts "#{m.from}:"
45
- puts " #{m.message}"
46
- end