lazy_record 0.1.4 → 0.1.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.
- checksums.yaml +4 -4
- data/lib/lazy_record/associations.rb +9 -3
- data/lib/lazy_record/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bc0937314b609cac19dbd32b7a69d5833d23b1a
|
4
|
+
data.tar.gz: 11941dc0a6c8b0f3c15b1875606637752641ff2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48345672368d7f8e32c8d216e55b78ba1a4bf2f1f29a32ac958484bea3b074bc0f66ec1d905799afbda616c185daa72611666d337fd33fc49fb09ec4e3232765
|
7
|
+
data.tar.gz: 12e8cce3cd65a4ae3341dbe5e7bcd1e259f38f6e878f9c525605433e7abb50ed53865b62772ed83b7beac3eeef7b1b2cf6f40833b2f7a5aa2a29662532077146
|
@@ -7,28 +7,34 @@ module LazyRecord
|
|
7
7
|
NESTED_ATTRS_MODULE_NAME = :NestedAttributes
|
8
8
|
|
9
9
|
def define_collection_getter(collection, class_name)
|
10
|
+
model = apply_nesting(class_name).constantize
|
10
11
|
define_method(collection) do
|
11
12
|
if instance_variable_get("@#{collection}").nil?
|
12
|
-
instance_variable_set("@#{collection}", Relation.new(model:
|
13
|
+
instance_variable_set("@#{collection}", Relation.new(model: model))
|
13
14
|
end
|
14
15
|
instance_variable_get("@#{collection}")
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
19
|
def define_collection_setter(collection, class_name)
|
20
|
+
model = apply_nesting(class_name).constantize
|
19
21
|
define_method("#{collection}=") do |coll|
|
20
|
-
coll = Relation.new(model:
|
22
|
+
coll = Relation.new(model: model, array: coll) if coll.is_a?(Array)
|
21
23
|
return instance_variable_set("@#{collection}", coll) if coll.is_a? Relation
|
22
24
|
raise ArgumentError, "Argument must be a collection of #{collection}"
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
28
|
+
def apply_nesting(class_name)
|
29
|
+
"#{self.to_s.split('::')[0..-3].join('::')}::#{class_name}"
|
30
|
+
end
|
31
|
+
|
26
32
|
def lr_has_many(*collections)
|
27
33
|
include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
|
28
34
|
mod.extend(Associations)
|
29
35
|
mod.module_eval do
|
30
36
|
collections.each do |collection|
|
31
|
-
class_name = collection.to_s.classify
|
37
|
+
class_name = collection.to_s.classify
|
32
38
|
define_collection_getter(collection, class_name)
|
33
39
|
define_collection_setter(collection, class_name)
|
34
40
|
end
|
data/lib/lazy_record/version.rb
CHANGED