motion_record 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NDdiYmE3NTlmNjMzMmVjN2UwMmJhNjk1YTRlOGFlYmQxYjVmMmNkYQ==
5
- data.tar.gz: !binary |-
6
- YTgzNmU2OTFhMzVhNjRiYWZlNGY1MjZiZDQ0MWI5Njk0Y2VlMTQ3Zg==
2
+ SHA1:
3
+ metadata.gz: 806d2c421426bad6f8c077bcd5a6d12e74015367
4
+ data.tar.gz: 2a906afefcc90eecd49b3f03badeda1bee7f3171
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NWVkNjgwMjUwYzBkNjgzY2FhNmY5OTM1YWI0N2M1Nzk4ZjQ2N2M5YWE5MTc4
10
- NDViNWZkYmQ0NjhlZTFlMzM5NjAyOGIwNTMzYjQzNzEzMDhkNGE0OTkzZWM4
11
- MTRkNDg4ZTA1N2NiYWJmNjBiZDcwY2I0Mjk5NTI0NzdjZWNjZmI=
12
- data.tar.gz: !binary |-
13
- ZDkxM2Y1ODI4NTFmYmExZjMwZjM2MzEzOTgxMjRiNWYyYWM1ODlmNWE4YWJk
14
- MTNlNTY5ZTMyOTExZWE2ODRjYzRkYTZjODYyNDYzZGQ4ZGUxYjJjMmNhMmEy
15
- YjMyYTRlMmRiYzhmNDIwOTUyMzEzOWM4MmRmMWI1Njk2M2U2MDY=
6
+ metadata.gz: e46095019c1d27c71be32543d3b8896f909a0f64446e4fdd6a76d8e39e2d59fbcb00bdc806206a17fa5554e9b0f73264b7769deb79d5b25dbdc6281d554be082
7
+ data.tar.gz: f314d3c7defa369ab86623572789dcea5f9c0127e46a45325100a4ccd64b4846779c336a3c31525dd39199403e9525e078cb0eb58c7f454b7159409b38cddf4f
data/README.md CHANGED
@@ -6,9 +6,9 @@ MotionRecord
6
6
  Everything you need to start using SQLite as the datastore for your RubyMotion
7
7
  app.
8
8
 
9
- :turtle: Android support should be coming soon
9
+ :turtle: Android support should be [coming soon](https://github.com/magoosh/motion_record/issues/3)
10
10
 
11
- [![Code Climate](https://codeclimate.com/github/magoosh/motion_record/badges/gpa.svg)](https://codeclimate.com/github/magoosh/motion_record) [![Test Coverage](https://codeclimate.com/github/magoosh/motion_record/badges/coverage.svg)](https://codeclimate.com/github/magoosh/motion_record)
11
+ [![Gem Version](https://badge.fury.io/rb/motion_record.svg)](http://badge.fury.io/rb/motion_record) [![Code Climate](https://codeclimate.com/github/magoosh/motion_record/badges/gpa.svg)](https://codeclimate.com/github/magoosh/motion_record) [![Test Coverage](https://codeclimate.com/github/magoosh/motion_record/badges/coverage.svg)](https://codeclimate.com/github/magoosh/motion_record)
12
12
 
13
13
  Installation
14
14
  ------------
@@ -35,7 +35,38 @@ And then execute:
35
35
  $ bundle
36
36
  ```
37
37
 
38
- * TODO: Android???
38
+ MotionRecord::Base
39
+ ------------------
40
+
41
+ MotionRecord::Base provides a superclass for defining objects which are stored
42
+ in the database.
43
+
44
+ ```ruby
45
+ class Message < MotionRecord::Base
46
+ # That's all!
47
+ end
48
+ ```
49
+
50
+ Attribute methods are inferred from the associated SQLite table definition.
51
+
52
+ ```ruby
53
+ message = Message.new(subject: "Welcome!", body: "If you have any questions...")
54
+ # => #<Message: @id=nil @subject="Welcome!" @body="If you have any..." ...>
55
+ message.satisfaction
56
+ # => 0.0
57
+ ```
58
+
59
+ Manage persistence with `save!`, `delete!`, and `persisted?`
60
+
61
+ ```ruby
62
+ message = Message.new(subject: "Welcome!", body: "If you have any questions...")
63
+ message.save!
64
+ message.id
65
+ # => 1
66
+ message.delete!
67
+ message.persisted?
68
+ # => false
69
+ ```
39
70
 
40
71
  MotionRecord::Schema
41
72
  --------------------
@@ -82,41 +113,6 @@ which will be cleared every time the app process is killed.
82
113
  MotionRecord::Schema.up!(file: :memory) # ...
83
114
  ```
84
115
 
85
- MotionRecord::Base
86
- ------------------
87
-
88
- MotionRecord::Base provides a superclass for defining objects which are stored
89
- in the database.
90
-
91
- ```ruby
92
- class Message < MotionRecord::Base
93
- # That's all!
94
- end
95
- ```
96
-
97
- Attribute methods are inferred from the associated SQLite table definition.
98
-
99
- ```ruby
100
- message = Message.new(subject: "Welcome!", body: "If you have any questions...")
101
- # => #<Message: @id=nil @subject="Welcome!" @body="If you have any..." ...>
102
- message.satisfaction
103
- # => 0.0
104
- ```
105
-
106
- Manage persistence with `save!`, `delete!`, and `persisted?`
107
-
108
- ```ruby
109
- message = Message.new(subject: "Welcome!", body: "If you have any questions...")
110
- message.save!
111
- message.id
112
- # => 1
113
- message.delete!
114
- message.persisted?
115
- # => false
116
- ```
117
-
118
- * TODO: Better default inflection of class names to table names
119
-
120
116
  MotionRecord::Scope
121
117
  -------------------
122
118
 
@@ -141,8 +137,6 @@ Message.where(read_at: nil).find_all
141
137
  Message.where(read_at: nil).update_all(read_at: Time.now.to_i)
142
138
  ```
143
139
 
144
- * TODO: Return "rows modified" count for update_all and delete_all
145
-
146
140
  Run calculations on scopes with `count`, `sum`, `maximum`, `minimum`, and
147
141
  `average`.
148
142
 
@@ -153,9 +147,6 @@ Message.where(subject: "How do you like the app?").maximum(:satisfaction)
153
147
  # => 10.0
154
148
  ```
155
149
 
156
- * TODO: Predicates for comparisons other than `=`
157
- * TODO: Handle datatype conversion in `where` and `update_all`
158
-
159
150
  MotionRecord::Serialization
160
151
  ----------------------------------
161
152
 
@@ -203,8 +194,6 @@ Survey.first
203
194
  # => #<Survey: @id=1 @response={"nps"=>10, "what_can_we_improve"=>"Nothing :)"}>
204
195
  ```
205
196
 
206
- * TODO: Make JSON serializer cross-platform
207
-
208
197
  #### Custom Serializers
209
198
 
210
199
  To write a custom serializer, extend MotionRecord::Serialization::BaseSerializer
@@ -231,7 +220,7 @@ end
231
220
  MotionRecord::Association
232
221
  -------------------------
233
222
 
234
- * TODO: has_many and belongs_to
223
+ [TODO](https://github.com/magoosh/motion_record/issues/7)
235
224
 
236
225
 
237
226
  Contributing
@@ -1,6 +1,8 @@
1
1
  module MotionRecord
2
2
  class Base
3
+ include Serialization
3
4
  include Persistence
5
+ include ScopeHelpers
4
6
 
5
7
  def initialize(attributes={})
6
8
  initialize_from_attribute_hash(attributes)
@@ -13,10 +13,6 @@ module MotionRecord
13
13
  end
14
14
  end
15
15
 
16
- def primary_key_condition
17
- {self.class.primary_key => self.instance_variable_get("@#{self.class.primary_key}")}
18
- end
19
-
20
16
  def persisted?
21
17
  !!@persisted
22
18
  end
@@ -36,7 +32,7 @@ module MotionRecord
36
32
  self.class.table_columns
37
33
 
38
34
  params = self.to_attribute_hash.reject { |k, _v| k == self.class.primary_key }
39
- table_params = self.class.to_table_params(params)
35
+ table_params = self.class.serialize_table_params(params)
40
36
 
41
37
  if persisted?
42
38
  self.class.where(primary_key_condition).update_all(table_params)
@@ -47,10 +43,11 @@ module MotionRecord
47
43
  self.mark_persisted!
48
44
  end
49
45
 
50
- module ClassMethods
51
- include MotionRecord::ScopeHelpers::ClassMethods
52
- include MotionRecord::Serialization::ClassMethods
46
+ def primary_key_condition
47
+ {self.class.primary_key => self.instance_variable_get("@#{self.class.primary_key}")}
48
+ end
53
49
 
50
+ module ClassMethods
54
51
  def create!(attributes={})
55
52
  self.new(attributes).save!
56
53
  end
@@ -43,7 +43,11 @@ module MotionRecord
43
43
  end
44
44
 
45
45
  def find_all
46
- connection.select(self).map { |row| @klass.from_table_params(row) }
46
+ connection.select(self).map do |row|
47
+ record = @klass.new(@klass.deserialize_table_params(row))
48
+ record.mark_persisted!
49
+ record
50
+ end
47
51
  end
48
52
 
49
53
  def pluck(attribute)
@@ -85,5 +85,9 @@ module MotionRecord
85
85
  Scope.new(self)
86
86
  end
87
87
  end
88
+
89
+ def self.included(base)
90
+ base.extend(ClassMethods)
91
+ end
88
92
  end
89
93
  end
@@ -23,19 +23,24 @@ module MotionRecord
23
23
  end
24
24
  end
25
25
 
26
- # Build a new object from the Hash result of a SQL query
27
- def from_table_params(params)
28
- attributes = {}
29
- params.each do |name, value|
26
+ # Deserialize a Hash of attributes from their database representation
27
+ #
28
+ # params - a Hash of Symbol column names to SQLite values
29
+ #
30
+ # Returns a Hash with all values replaced by their deserialized versions
31
+ def deserialize_table_params(params)
32
+ params.each_with_object({}) do |name_and_value, attributes|
33
+ name, value = name_and_value
30
34
  attributes[name.to_sym] = serializer(name.to_sym).deserialize(value)
31
35
  end
32
- record = self.new(attributes)
33
- record.mark_persisted!
34
- record
35
36
  end
36
37
 
37
38
  # Serialize a Hash of attributes to their database representation
38
- def to_table_params(hash)
39
+ #
40
+ # params - a Hash of Symbol column names to their attribute values
41
+ #
42
+ # Returns a Hash with all values replaced by their serialized versions
43
+ def serialize_table_params(hash)
39
44
  hash.each_with_object({}) do |attribute_and_value, params|
40
45
  attribute, value = attribute_and_value
41
46
  params[attribute] = serializer(attribute).serialize(value)
@@ -64,5 +69,9 @@ module MotionRecord
64
69
  serializer_classes[attribute].new(table_columns[attribute])
65
70
  end
66
71
  end
72
+
73
+ def self.included(base)
74
+ base.extend(ClassMethods)
75
+ end
67
76
  end
68
77
  end
@@ -1,3 +1,3 @@
1
1
  module MotionRecord
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/motion_record.rb CHANGED
@@ -26,7 +26,9 @@ Motion::Project::App.setup do |app|
26
26
 
27
27
  # Some files don't have the same dependency order and alphabetic order
28
28
  {
29
- "motion_record/persistence.rb" => "motion_record/scope_helpers.rb"
29
+ "motion_record/base.rb" => "motion_record/persistence.rb",
30
+ "motion_record/base.rb" => "motion_record/serialization.rb",
31
+ "motion_record/base.rb" => "motion_record/scope_helpers.rb"
30
32
  }.each do |file, dependency|
31
33
  app.files_dependencies File.join(dirname, file) => File.join(dirname, dependency)
32
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Millman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-13 00:00:00.000000000 Z
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Mini ActiveRecord for RubyMotion
@@ -82,12 +82,12 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ! '>='
85
+ - - '>='
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ! '>='
90
+ - - '>='
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []