motion_data_wrapper 0.0.5 → 0.0.6

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.
@@ -43,8 +43,7 @@ module MotionDataWrapper
43
43
 
44
44
  def sqlite_url
45
45
  if Object.const_defined?("UIApplication")
46
- file_path = File.join App.documents_path, "#{sqlite_store_name}.sqlite"
47
- NSURL.fileURLWithPath(file_path)
46
+ NSURL.fileURLWithPath(sqlite_path)
48
47
  else
49
48
  error_ptr = Pointer.new(:object)
50
49
  unless support_dir = NSFileManager.defaultManager.URLForDirectory(NSApplicationSupportDirectory, inDomain: NSUserDomainMask, appropriateForURL: nil, create: true, error: error_ptr)
@@ -55,6 +54,14 @@ module MotionDataWrapper
55
54
  support_dir.URLByAppendingPathComponent("#{sqlite_store_name}.sqlite")
56
55
  end
57
56
  end
57
+
58
+ def sqlite_path
59
+ @sqlite_path || File.join(App.documents_path, "#{sqlite_store_name}.sqlite")
60
+ end
61
+
62
+ def sqlite_path= path
63
+ @sqlite_path = path
64
+ end
58
65
 
59
66
  def persistent_store_options
60
67
  { NSMigratePersistentStoresAutomaticallyOption => true, NSInferMappingModelAutomaticallyOption => true }
@@ -1,11 +1,4 @@
1
1
  module MotionDataWrapper
2
2
  class RecordNotFound < StandardError
3
-
4
- def initialize(klass, id)
5
- @klass = klass
6
- @id = id
7
- super("Could not find #{@klass.name} id: #{@id}")
8
- end
9
-
10
3
  end
11
4
  end
@@ -8,71 +8,76 @@ module MotionDataWrapper
8
8
 
9
9
  module ClassMethods
10
10
 
11
- def all
12
- relation.to_a
13
- end
14
-
15
- def count
16
- relation.count
17
- end
18
-
19
- def destroy_all
20
- all.map &:destroy
11
+ # Delegate the simplest methods to the relation without any args
12
+ %w(all count destroy_all empty? exists? uniq).each do |method|
13
+ define_method method do
14
+ relation.send(method)
15
+ end
21
16
  end
22
17
 
18
+ # @param [Symbol] query part to exclude, ie :where or :limit
19
+ # @returns [Relation]
23
20
  def except(query_part)
24
21
  relation.except(query_part)
25
22
  end
26
23
 
27
- def find(object_id)
28
- raise MotionDataWrapper::RecordNotFound.new(self, object_id) unless entity = find_by_id(object_id)
29
- entity
24
+ # @returns [Model] or nil
25
+ def first
26
+ take
30
27
  end
31
28
 
32
- def first
33
- relation.first
29
+ # @param [id] id to retrieve record by
30
+ # @raises [RecordNotFound]
31
+ # @returns [Model]
32
+ def find(object_id)
33
+ find_by_id!(object_id)
34
34
  end
35
35
 
36
+ # @raises [RecordNotFound]
37
+ # @returns [Model]
36
38
  def first!
37
- first or raise MotionDataWrapper::RecordNotFound
39
+ take!
38
40
  end
39
41
 
42
+ # @returns [Model] or nil
40
43
  def last
41
- relation.last
44
+ relation.last.take
42
45
  end
43
46
 
47
+ # @raises [RecordNotFound]
48
+ # @returns [Model]
44
49
  def last!
45
- last or raise MotionDataWrapper::RecordNotFound
50
+ relation.last.take!
46
51
  end
47
52
 
53
+ # @param [Fixnum]
54
+ # @returns [Relation]
48
55
  def limit(l)
49
56
  relation.limit(l)
50
57
  end
51
58
 
52
- def method_missing(method, *args, &block)
53
- if method.start_with?("find_by_")
54
- attribute = method.gsub("find_by_", "")
55
- relation.where("#{attribute} = ?", *args).first
56
- elsif method.start_with?("find_all_by_")
57
- attribute = method.gsub("find_all_by_", "")
58
- relation.where("#{attribute} = ?", *args).to_a
59
- else
60
- super
61
- end
62
- end
63
-
59
+ # @param [Fixnum]
60
+ # @returns [Relation]
64
61
  def offset(o)
65
62
  relation.offset(o)
66
63
  end
67
64
 
65
+ # @param [Symbol] column
66
+ # @param @optional [Hash] options, key :ascending
67
+ # @returns [Relation]
68
68
  def order(*args)
69
69
  relation.order(*args)
70
70
  end
71
71
 
72
+ # @param [Symbol] column
73
+ # @returns [Array]
72
74
  def pluck(column)
73
75
  relation.pluck(column)
74
76
  end
75
77
 
78
+ # @param [Symbol] column
79
+ # @param @optional [Hash] options, key :ascending
80
+ # @returns [Relation]
76
81
  def reorder(*args)
77
82
  relation.except(:order).order(*args)
78
83
  end
@@ -85,19 +90,42 @@ module MotionDataWrapper
85
90
  end
86
91
  end
87
92
 
88
- def uniq
89
- relation.uniq
90
- end
91
-
93
+ # @param [String] conditions
94
+ # @param [vargs] Replacements
95
+ # @returns [Relation]
96
+ # Usage:
97
+ # where("title contains[cd] ?", "title")
92
98
  def where(*args)
93
99
  relation.where(*args)
94
100
  end
95
101
 
102
+ # @param [NSManagedObjectContext]
103
+ # @param [Relation]
96
104
  def with_context(ctx)
97
105
  relation.with_context(ctx)
98
106
  end
99
107
 
100
- private
108
+ private
109
+
110
+ def method_missing(method, *args, &block)
111
+ if method.start_with?("find_by_")
112
+ attribute = method.gsub("find_by_", "").gsub("!", "")
113
+ chain = relation.where("#{attribute} = ?", *args)
114
+
115
+ if method.end_with?("!")
116
+ chain.first!
117
+ else
118
+ chain.first
119
+ end
120
+
121
+ elsif method.start_with?("find_all_by_")
122
+ attribute = method.gsub("find_all_by_", "")
123
+ relation.where("#{attribute} = ?", *args).to_a
124
+
125
+ else
126
+ super
127
+ end
128
+ end
101
129
 
102
130
  def relation
103
131
  Relation.alloc.initWithClass(self)
@@ -32,17 +32,30 @@ module MotionDataWrapper
32
32
  end
33
33
 
34
34
  end
35
-
35
+
36
+ def awakeFromFetch
37
+ super
38
+ after_fetch if respond_to? :after_fetch
39
+ end
40
+
41
+ def awakeFromInsert
42
+ super
43
+ after_fetch if respond_to? :after_fetch
44
+ end
45
+
36
46
  def destroy
37
47
 
38
48
  if context = managedObjectContext
49
+ before_destroy_callback
39
50
  context.deleteObject(self)
40
51
  error = Pointer.new(:object)
41
- context.save(error)
52
+ if context.save(error)
53
+ @destroyed = true
54
+ after_destroy_callback
55
+ freeze
56
+ end
42
57
  end
43
58
 
44
- @destroyed = true
45
- freeze
46
59
  end
47
60
 
48
61
  def destroyed?
@@ -72,14 +85,47 @@ module MotionDataWrapper
72
85
  context.insertObject(self)
73
86
  end
74
87
 
88
+ before_save_callback
75
89
  error = Pointer.new(:object)
76
90
  unless context.save(error)
77
91
  managedObjectContext.deleteObject(self)
78
92
  raise MotionDataWrapper::RecordNotSaved, self and return false
79
93
  end
94
+ instance_variable_set('@new_record', false)
95
+ after_save_callback
96
+
80
97
  true
81
98
  end
82
-
99
+
100
+ private
101
+
102
+ def before_save_callback
103
+ before_save if respond_to? :before_save
104
+ @is_new_record = new_record?
105
+ if @is_new_record
106
+ before_create if respond_to? :before_create
107
+ else
108
+ before_update if respond_to? :before_update
109
+ end
110
+ end
111
+
112
+ def after_save_callback
113
+ if @is_new_record
114
+ after_create if respond_to? :after_create
115
+ else
116
+ after_update if respond_to? :after_update
117
+ end
118
+ after_save if respond_to? :after_save
119
+ end
120
+
121
+ def before_destroy_callback
122
+ before_destroy if respond_to? :before_destroy
123
+ end
124
+
125
+ def after_destroy_callback
126
+ after_destroy if respond_to? :after_destroy
127
+ end
128
+
83
129
  end
84
130
  end
85
131
  end
@@ -19,6 +19,10 @@ module MotionDataWrapper
19
19
  all.map &:destroy
20
20
  end
21
21
 
22
+ def empty?
23
+ self.count == 0
24
+ end
25
+
22
26
  def except(query_part)
23
27
  case query_part.to_sym
24
28
  when :where
@@ -33,15 +37,13 @@ module MotionDataWrapper
33
37
  self
34
38
  end
35
39
 
36
- def first
37
- self.fetchLimit = 1
38
- to_a[0]
40
+ def exists?
41
+ !empty?
39
42
  end
40
43
 
41
44
  def last
42
45
  self.fetchOffset = self.count - 1 unless self.count < 1
43
- self.fetchLimit = 1
44
- to_a[0]
46
+ self
45
47
  end
46
48
 
47
49
  def limit(l)
@@ -75,6 +77,14 @@ module MotionDataWrapper
75
77
  to_a.collect { |r| r[column] }
76
78
  end
77
79
 
80
+ def take
81
+ limit(1).to_a[0]
82
+ end
83
+
84
+ def take!
85
+ take or raise RecordNotFound
86
+ end
87
+
78
88
  def uniq
79
89
  self.returnsDistinctResults = true
80
90
  self
@@ -1,3 +1,3 @@
1
1
  module MotionDataWrapper
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,27 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_data_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Matt Brewer
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-06-24 00:00:00.000000000 Z
12
+ date: 2013-09-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bubble-wrap
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  description: Forked from the mattgreen/nitron gem, this provides an intuitive way
@@ -49,25 +52,27 @@ files:
49
52
  - lib/motion_data_wrapper/version.rb
50
53
  homepage: https://github.com/macfanatic/motion_data_wrapper
51
54
  licenses: []
52
- metadata: {}
53
55
  post_install_message:
54
56
  rdoc_options: []
55
57
  require_paths:
56
58
  - lib
57
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
58
61
  requirements:
59
- - - '>='
62
+ - - ! '>='
60
63
  - !ruby/object:Gem::Version
61
64
  version: '0'
62
65
  required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
63
67
  requirements:
64
- - - '>='
68
+ - - ! '>='
65
69
  - !ruby/object:Gem::Version
66
70
  version: '0'
67
71
  requirements: []
68
72
  rubyforge_project:
69
- rubygems_version: 2.0.3
73
+ rubygems_version: 1.8.24
70
74
  signing_key:
71
- specification_version: 4
75
+ specification_version: 3
72
76
  summary: Provides an easy ActiveRecord-like interface to CoreData
73
77
  test_files: []
78
+ has_rdoc:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 3da72e1eb1f458f7befcce0e620f963d2bbad3f1
4
- data.tar.gz: 1ee074ffa517340dce15493f3afb9d740ab6b8e2
5
- SHA512:
6
- metadata.gz: a4afe689fd4c21370529084ffe24ba54b3ff94c470a15148ff39187ce5b5358885ccad3d0d67aa102d8f19f3e048d5e605b36356e3e9f84c82f385748bc235a3
7
- data.tar.gz: 0696baa492af321233565b0cd70e2bf751e7a81d738f72a47f330d54dce49ffe5fa481e7085ea77500fbd49f3d7134b6df0a7d72c37702628f30f3a89cb88b9c