embedded 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dad0317dedc0a4f5a2bc155d667f6b4131ba67d3ed03c225f387deb68c3187c9
4
- data.tar.gz: ea2b00691c5431639e3991afb3b0dc86bc87702f749f7fe695e55c6932ea6a1e
3
+ metadata.gz: f0cb08595d317787cfd5ca6b9de7fa50e6800eeeb3607466192816e65eb99099
4
+ data.tar.gz: 0e0daa41abd96d62c02316fa7a432b2af8764faf92fbf1d0b188ce480b005a7f
5
5
  SHA512:
6
- metadata.gz: d1c1e2ff032c40c27938ae4af252ceb1d2b261f2b4d7ab01e6dd4d4421094e6fa09886a7b011a4dbe5f75898390eec0874646c0c3ba38604f54376f34944f3c7
7
- data.tar.gz: 534cd2314130b599c428a6ef898b09624d8ca2728c10bf4d5d858b0938809eb00a9f0b9a2c11140c8cb3fd274d95ac3318b9e4af3d98b48cb011ac1d8a18cfd7
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
- Your table columns have to be named in a specific way so they are mapped correctly, for example:
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.
@@ -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.inject({}) do |hash, a|
7
- hash.merge(:"#{embeddable_attr}_#{a}" => a)
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
 
@@ -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].inject({}) do |a,attr|
10
- a.merge(:"#{embeddable_attr}_#{attr}" => value ? value.send(attr) : nil)
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
 
@@ -1,3 +1,3 @@
1
1
  module Embedded
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embedded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jvillarejo