embedded 0.2.0 → 0.3.0
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 +4 -4
- data/README.md +43 -1
- data/lib/embedded/model.rb +8 -2
- data/lib/embedded/scope.rb +10 -2
- data/lib/embedded/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0cb08595d317787cfd5ca6b9de7fa50e6800eeeb3607466192816e65eb99099
|
4
|
+
data.tar.gz: 0e0daa41abd96d62c02316fa7a432b2af8764faf92fbf1d0b188ce480b005a7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7da9b8d21c25d77f32a3055e3218b13a59b0ce677862cc10a922e0534e1ff6f22c06ce6c4f5a5df9f10a40551837d5d01e4b1c8ee55864eff0050d60f166196
|
7
|
+
data.tar.gz: f4f78dd54363b5a0fc9400e0708edc7381b74f9f6326ddaaa1b176c2b867e133eef3b36141c277b5d6e96266c2204993b2dc560cac48d6d1d5b744979658597e
|
data/README.md
CHANGED
@@ -164,8 +164,9 @@ Also you can persist the reservation, and when fetching it back from the db its
|
|
164
164
|
|
165
165
|
### Database Mapping
|
166
166
|
|
167
|
-
|
167
|
+
The default convention column mapping is the value object name as prefix and the value object attribute as suffix.
|
168
168
|
|
169
|
+
Example:
|
169
170
|
If Reservation attribute name is scheduled_time and its TimeInterval has start_time and end_time attributes, your column names should be defined as followed:
|
170
171
|
|
171
172
|
```ruby
|
@@ -196,6 +197,47 @@ class CreateShops < ActiveRecord::Migration
|
|
196
197
|
end
|
197
198
|
```
|
198
199
|
|
200
|
+
We can override this convetion if you pass attrs argument as hash where you define the mapping.
|
201
|
+
|
202
|
+
Example:
|
203
|
+
```ruby
|
204
|
+
class PersonalDocument
|
205
|
+
attr_reader :number, :type
|
206
|
+
|
207
|
+
def initialize(values = {})
|
208
|
+
@number = values.fetch(:number)
|
209
|
+
@type = values.fetch(:type)
|
210
|
+
end
|
211
|
+
|
212
|
+
def ==(other)
|
213
|
+
return false if !other.is_a?(PersonalDocument)
|
214
|
+
|
215
|
+
@number == other.number && @type == other.type
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
class Person < ApplicationRecord
|
220
|
+
embeds :identification, attrs: {
|
221
|
+
number: id_number,
|
222
|
+
type: id_type
|
223
|
+
}, class_name: 'PersonalDocument'
|
224
|
+
end
|
225
|
+
|
226
|
+
class CreatePeople < ActiveRecord::Migration
|
227
|
+
def change
|
228
|
+
create_table :people do |t|
|
229
|
+
t.string :id_number
|
230
|
+
t.string :id_type
|
231
|
+
|
232
|
+
t.timestamps
|
233
|
+
end
|
234
|
+
|
235
|
+
add_index :people, :id_number
|
236
|
+
add_index :people, :id_type
|
237
|
+
end
|
238
|
+
end
|
239
|
+
```
|
240
|
+
|
199
241
|
### Querying
|
200
242
|
|
201
243
|
For example you have now a model that has prices in different currencies.
|
data/lib/embedded/model.rb
CHANGED
@@ -3,8 +3,14 @@ module Embedded
|
|
3
3
|
ScopeMethod = ActiveRecord::VERSION::MAJOR >= 4 ? :all.freeze : :scoped.freeze
|
4
4
|
|
5
5
|
def embedded_column_names(embeddable_attr, attributes)
|
6
|
-
attributes.
|
7
|
-
|
6
|
+
if attributes.is_a?(Array)
|
7
|
+
attributes.inject({}) do |hash, a|
|
8
|
+
hash.merge(:"#{embeddable_attr}_#{a}" => a)
|
9
|
+
end
|
10
|
+
elsif attributes.is_a?(Hash)
|
11
|
+
attributes.invert
|
12
|
+
else
|
13
|
+
raise ArgumentError.new('invalid attributes')
|
8
14
|
end
|
9
15
|
end
|
10
16
|
|
data/lib/embedded/scope.rb
CHANGED
@@ -6,8 +6,16 @@ module Embedded
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def embedded_attributes_for(embeddable_attr, value = nil)
|
9
|
-
@attributes[embeddable_attr][:attrs]
|
10
|
-
|
9
|
+
attrs = @attributes[embeddable_attr][:attrs]
|
10
|
+
|
11
|
+
if attrs.is_a?(Array)
|
12
|
+
attrs.inject({}) do |a,attr|
|
13
|
+
a.merge(:"#{embeddable_attr}_#{attr}" => value ? value.send(attr) : nil)
|
14
|
+
end
|
15
|
+
elsif attrs.is_a?(Hash)
|
16
|
+
attrs.inject({}) do |a,(attr,column)|
|
17
|
+
a.merge(column => value ? value.send(attr) : nil)
|
18
|
+
end
|
11
19
|
end
|
12
20
|
end
|
13
21
|
|
data/lib/embedded/version.rb
CHANGED