motion_model 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -50,7 +50,9 @@ class Inflector
50
50
 
51
51
  @irregulars = [
52
52
  ['person', 'people'],
53
- ['people', 'person']
53
+ ['people', 'person'],
54
+ ['children', 'child'],
55
+ ['child', 'children']
54
56
  ]
55
57
 
56
58
  @uncountables = [
@@ -194,7 +196,11 @@ class Debug
194
196
  def self.put_message(type, message, color = Ansi.reset_color)
195
197
  open_color = @@colorize ? color : ''
196
198
  close_color = @@colorize ? Ansi.reset_color : ''
197
- NSLog("#{open_color}#{type} #{caller[1]}: #{message}#{close_color}") unless @@silent
199
+
200
+ ### It appears that RubyMotion does not support caller backtrace yet.
201
+
202
+ # NSLog("#{open_color}#{type} #{caller[1]}: #{message}#{close_color}") unless @@silent
203
+ NSLog("#{open_color}#{type} #{message}#{close_color}") unless @@silent
198
204
  end
199
205
 
200
206
  def self.info(msg)
@@ -210,3 +216,14 @@ class Debug
210
216
  end
211
217
 
212
218
  end
219
+
220
+ # These are C macros in iOS SDK. Not workable for Ruby.
221
+ def UIInterfaceOrientationIsLandscape(orientation)
222
+ orientation == UIInterfaceOrientationLandscapeLeft ||
223
+ orientation == UIInterfaceOrientationLandscapeRight
224
+ end
225
+
226
+ def UIInterfaceOrientationIsPortrait(orientation)
227
+ orientation == UIInterfaceOrientationPortrait ||
228
+ orientation == UIInterfaceOrientationPortraitUpsideDown
229
+ end
@@ -208,6 +208,7 @@ module MotionModel
208
208
  end
209
209
 
210
210
  def cell_has_first_responder?(cell)
211
+ return false unless cell.respond_to?(:subviews)
211
212
  cell.subviews.each do |subview|
212
213
  return true if subview.isFirstResponder
213
214
  end
@@ -195,7 +195,7 @@ module MotionModel
195
195
 
196
196
  # task.assignees.new(:name => 'BoB')
197
197
  # creates a new unsaved Assignee object on the Task object task
198
- def new(options)
198
+ def new(options = {})
199
199
  raise ArgumentError.new("Creating on a relation requires the parent be saved first.") if @related_object.nil?
200
200
 
201
201
  id_field = (@related_object.class.to_s.downcase + '_id').to_sym
@@ -424,14 +424,14 @@ module MotionModel
424
424
  base_method = method.to_s.gsub('=', '').to_sym
425
425
 
426
426
  col = column_named(base_method)
427
- raise RuntimeError.new("nil column #{method} accessed.") if col.nil?
427
+ raise NoMethodError.new("nil column #{method} accessed.") if col.nil?
428
428
 
429
429
  unless col.type == :belongs_to_id
430
430
  has_relation = relation_for(col) if self.class.has_relation?(col)
431
431
  return has_relation if has_relation
432
432
  end
433
433
 
434
- if col
434
+ unless col.nil?
435
435
  if method.to_s.include?('=')
436
436
  @dirty = true
437
437
  return @data[base_method] = col.type == :belongs_to_id ? args[0] : self.cast_to_type(base_method, args[0])
@@ -439,11 +439,13 @@ module MotionModel
439
439
  return @data[base_method]
440
440
  end
441
441
  else
442
- raise NoMethodError, <<ERRORINFO
442
+ exception = NoMethodError.new(<<ERRORINFO
443
443
  method: #{method}
444
444
  args: #{args.inspect}
445
445
  in: #{self.class.name}
446
446
  ERRORINFO
447
+ )
448
+ raise exception
447
449
  end
448
450
  end
449
451
 
@@ -10,12 +10,12 @@ module MotionModel
10
10
  #
11
11
  # Raises a +MotionModel::PersistFileFailureError+ on failure.
12
12
  def deserialize_from_file(file_name = nil)
13
- @file_name ||= file_name
13
+ @file_name = file_name if file_name
14
14
 
15
15
  if File.exist? documents_file(@file_name)
16
16
  error_ptr = Pointer.new(:object)
17
17
 
18
- data = NSData.dataWithContentsOfFile(documents_file(file_name), options:NSDataReadingMappedIfSafe, error:error_ptr)
18
+ data = NSData.dataWithContentsOfFile(documents_file(@file_name), options:NSDataReadingMappedIfSafe, error:error_ptr)
19
19
 
20
20
  if data.nil?
21
21
  error = error_ptr[0]
@@ -38,11 +38,11 @@ module MotionModel
38
38
  #
39
39
  # Raises a +MotionModel::PersistFileFailureError+ on failure.
40
40
  def serialize_to_file(file_name = nil)
41
- @file_name ||= file_name
41
+ @file_name = file_name if file_name
42
42
  error_ptr = Pointer.new(:object)
43
-
43
+
44
44
  data = NSKeyedArchiver.archivedDataWithRootObject @collection
45
- unless data.writeToFile(documents_file(file_name), options: NSDataWritingAtomic, error: error_ptr)
45
+ unless data.writeToFile(documents_file(@file_name), options: NSDataWritingAtomic, error: error_ptr)
46
46
  # De-reference the pointer.
47
47
  error = error_ptr[0]
48
48
 
@@ -85,12 +85,15 @@ module MotionModel
85
85
  # values.
86
86
  def encodeWithCoder(coder)
87
87
  columns.each do |attr|
88
- value = self.send(attr)
89
- unless value.nil?
90
- coder.encodeObject(value, forKey: attr.to_s)
88
+ # Serialize attributes except the proxy has_many and belongs_to ones.
89
+ unless [:belongs_to, :has_many].include? column_named(attr).type
90
+ value = self.send(attr)
91
+ unless value.nil?
92
+ coder.encodeObject(value, forKey: attr.to_s)
93
+ end
91
94
  end
92
95
  end
93
96
  end
94
97
 
95
98
  end
96
- end
99
+ end
@@ -1,3 +1,3 @@
1
1
  module MotionModel
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
data/spec/model_spec.rb CHANGED
@@ -134,12 +134,13 @@ describe "Creating a model" do
134
134
  describe 'finders' do
135
135
  before do
136
136
  Task.delete_all
137
- 10.times {|i| Task.create(:name => "task #{i}")}
137
+ @tasks = []
138
+ 10.times {|i| @tasks.push Task.create(:name => "task #{i}")}
138
139
  end
139
140
 
140
141
  describe 'find' do
141
142
  it 'finds elements within the collection' do
142
- task = Task.find(3).name.should.equal('task 3')
143
+ task = Task.find(3).name.should.equal(@tasks[2].name) # zero based
143
144
  end
144
145
 
145
146
  it 'returns nil if find by id is not found' do
@@ -81,4 +81,97 @@ describe 'persistence' do
81
81
  Foo.length.should == 1
82
82
  end
83
83
  end
84
+
85
+ describe "remembering filename" do
86
+ class Foo
87
+ include MotionModel::Model
88
+ columns :name => :string
89
+ end
90
+
91
+ before do
92
+ Foo.delete_all
93
+ @foo = Foo.create(:name => 'Bob')
94
+ end
95
+
96
+ it "deserializes from last file if no filename given (previous method serialize)" do
97
+ Foo.serialize_to_file('test.dat')
98
+ Foo.delete_all
99
+ Foo.count.should == 0
100
+ Foo.deserialize_from_file
101
+ Foo.count.should == 1
102
+ end
103
+
104
+ it "deserializes from last file if no filename given (previous method deserialize)" do
105
+ Foo.serialize_to_file('test.dat')
106
+ Foo.serialize_to_file('bogus.dat') # serialize sets default filename to something bogus
107
+ File.delete Foo.documents_file('bogus.dat') # and we get rid of that file
108
+ Foo.deserialize_from_file('test.dat') # so we'll be sure the default filename last was set by deserialize
109
+ Foo.delete_all
110
+ Foo.count.should == 0
111
+ Foo.deserialize_from_file
112
+ Foo.count.should == 1
113
+ end
114
+
115
+ it "serializes to last file if no filename given (previous method serialize)" do
116
+ Foo.serialize_to_file('test.dat')
117
+ Foo.create(:name => 'Ted')
118
+ Foo.serialize_to_file
119
+ Foo.delete_all
120
+ Foo.count.should == 0
121
+ Foo.deserialize_from_file('test.dat')
122
+ Foo.count.should == 2
123
+ end
124
+
125
+ it "serializes to last file if no filename given (previous method deserialize)" do
126
+ Foo.serialize_to_file('test.dat')
127
+ Foo.delete_all
128
+ Foo.serialize_to_file('bogus.dat') # serialize sets default filename to something bogus
129
+ File.delete Foo.documents_file('bogus.dat') # and we get rid of that file
130
+ Foo.deserialize_from_file('test.dat') # so we'll be sure the default filename was last set by deserialize
131
+ Foo.create(:name => 'Ted')
132
+ Foo.serialize_to_file
133
+ Foo.delete_all
134
+ Foo.count.should == 0
135
+ Foo.deserialize_from_file('test.dat')
136
+ Foo.count.should == 2
137
+ end
138
+
139
+ end
84
140
  end
141
+
142
+ describe "serialization of relations" do
143
+ class Parent
144
+ include MotionModel::Model
145
+ columns :name
146
+ has_many :children
147
+ end
148
+
149
+ class Child
150
+ include MotionModel::Model
151
+ columns :name
152
+ belongs_to :parent
153
+ end
154
+
155
+ before do
156
+ parent = Parent.create(:name => 'BoB')
157
+ parent.children.create :name => 'Fergie'
158
+ parent.children.create :name => 'Will I Am'
159
+ end
160
+
161
+ it "is wired up right" do
162
+ Parent.first.name.should == 'BoB'
163
+ Parent.first.children.count.should == 2
164
+ end
165
+
166
+ it "serializes and deserializes properly" do
167
+ Parent.serialize_to_file('parents.dat')
168
+ Child.serialize_to_file('children.dat')
169
+ Parent.delete_all
170
+ Child.delete_all
171
+ Parent.deserialize_from_file('parents.dat')
172
+ Child.deserialize_from_file('children.dat')
173
+ Parent.first.name.should == 'BoB'
174
+ Parent.first.children.count.should == 2
175
+ Parent.first.children.first.name.should == 'Fergie'
176
+ end
177
+ end
@@ -75,6 +75,15 @@ describe 'related objects' do
75
75
  Task.find(3).assignees.push(assignee)
76
76
  Task.find(3).assignees.count.should == assignee_count + 1
77
77
  end
78
+
79
+ it "supports creating blank (empty) scratchpad associated objects" do
80
+ task = Task.create :name => 'watch a movie'
81
+ assignee = task.assignees.new
82
+ assignee.assignee_name = 'Chloe'
83
+ assignee.save
84
+ task.assignees.count.should == 1
85
+ task.assignees.first.assignee_name.should == 'Chloe'
86
+ end
78
87
  end
79
88
 
80
89
  describe "supporting belongs_to" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
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: 2012-09-22 00:00:00.000000000 Z
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Simple model and validation mixins for RubyMotion
15
15
  email: