medea 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,7 +15,7 @@ module Medea
15
15
  inheritable_attributes :owned
16
16
  @owned = false
17
17
 
18
- include JasonObjectListProperties
18
+ include JasonObjectMetaProperties
19
19
 
20
20
  #end meta
21
21
 
@@ -113,13 +113,31 @@ module Medea
113
113
  end
114
114
  #end "flexihash" access
115
115
 
116
- def initialize key = nil, mode = :eager
117
- if key
118
- @__id = key
119
- if mode == :eager
120
- load
116
+ def sanitize hash
117
+ #remove the keys in hash that aren't allowed
118
+ forbidden_keys = ["jason_key",
119
+ "jason_state",
120
+ "jason_parent",
121
+ "jason_parent_key",
122
+ "jason_parent_list"]
123
+ hash.delete_if { |k,v| forbidden_keys.include? k }
124
+ result = {}
125
+ hash.each { |k, v| result[k.to_s] = v }
126
+ result
127
+ end
128
+
129
+ def initialize initialiser = nil, mode = :eager
130
+ if initialiser
131
+ if initialiser.is_a? Hash
132
+ @__jason_state = :new
133
+ @__jason_data = sanitize initialiser
121
134
  else
122
- @__jason_state = :ghost
135
+ @__id = initialiser
136
+ if mode == :eager
137
+ load
138
+ else
139
+ @__jason_state = :ghost
140
+ end
123
141
  end
124
142
  else
125
143
  @__jason_state = :new
@@ -179,8 +197,22 @@ module Medea
179
197
 
180
198
  #object persistence methods
181
199
 
200
+ def update_attributes attributes
201
+ @__jason_data = sanitize attributes
202
+ save
203
+ end
204
+
182
205
  #POSTs the current values of this object back to JasonDB
183
206
  #on successful post, sets state to STALE and updates eTag
207
+ def save
208
+ return false if @__jason_state == :stale or @__jason_state == :ghost
209
+ begin
210
+ save!
211
+ return true
212
+ rescue
213
+ return false
214
+ end
215
+ end
184
216
  def save!
185
217
  #no changes? no save!
186
218
  return if @__jason_state == :stale or @__jason_state == :ghost
@@ -1,9 +1,9 @@
1
- module JasonObjectListProperties
1
+ module JasonObjectMetaProperties
2
2
  def self.included(base)
3
- base.extend(MetaListProperties)
3
+ base.extend(MetaProperties)
4
4
  end
5
5
 
6
- module MetaListProperties
6
+ module MetaProperties
7
7
  def create_member_list list_name, list_class, list_type
8
8
  list = {}
9
9
  list = self.send(:class_variable_get, :@@lists) if self.class_variable_defined? :@@lists
@@ -27,5 +27,10 @@ module JasonObjectListProperties
27
27
  #list_type.class_variable_set :@@owner, self
28
28
  list_class.owned = true
29
29
  end
30
+
31
+ def key_field field_name
32
+ #this field must be present to save, and it must be unique
33
+ self.send(:class_variable_set, :@@key_field, field_name)
34
+ end
30
35
  end
31
36
  end
data/lib/medea/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Medea
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
 
4
4
  #When the templates are changed, this version should be incremented
5
5
  #This version is used when uploading/updating the templates
data/lib/medea.rb CHANGED
@@ -4,7 +4,7 @@ module Medea
4
4
  require 'medea/version'
5
5
  require 'medea/inheritable_attributes'
6
6
  require 'medea/active_model_methods'
7
- require 'medea/list_properties'
7
+ require 'medea/meta_properties'
8
8
  require 'medea/jasonobject'
9
9
  require 'medea/jasondeferredquery'
10
10
  require 'medea/jasonlistproperty'
@@ -52,4 +52,33 @@ describe "JasonObject" do
52
52
  usr.should_not eq(u)
53
53
  end
54
54
  end
55
+
56
+ it "should be initialisable with a hash" do
57
+ @user = User.new({:name => "Jimmy", :age => 31})
58
+ @user.name.should eq("Jimmy")
59
+ @user.age.should eq(31)
60
+ end
61
+
62
+ it "should be updateable with update_attributes" do
63
+
64
+ field_hash = {:name => "Robert",
65
+ :age => 25}
66
+ @user.name = "Fred"
67
+ @user.age = 20
68
+
69
+ @user.stub(:save).and_return true
70
+ @user.should_receive :save
71
+ (@user.update_attributes field_hash).should eq(true)
72
+
73
+ @user.name.should eq(field_hash[:name])
74
+ @user.age.should eq(field_hash[:age])
75
+ end
76
+
77
+ it "should pass the result of save back through update_attributes" do
78
+ @user.stub(:save).and_return true
79
+ (@user.update_attributes({})).should eq(true)
80
+
81
+ @user.stub(:save).and_return false
82
+ (@user.update_attributes({})).should eq(false)
83
+ end
55
84
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
7
+ - 5
8
8
  - 0
9
- version: 0.4.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Jensen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-13 00:00:00 +11:00
17
+ date: 2011-01-18 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -92,7 +92,7 @@ files:
92
92
  - lib/medea/jasondeferredquery.rb
93
93
  - lib/medea/jasonlistproperty.rb
94
94
  - lib/medea/jasonobject.rb
95
- - lib/medea/list_properties.rb
95
+ - lib/medea/meta_properties.rb
96
96
  - lib/medea/version.rb
97
97
  - medea.gemspec
98
98
  - spec/jason_object_spec.rb