lazy_record 0.1.4 → 0.1.6

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
  SHA1:
3
- metadata.gz: f711b308e8306e2e5425614a25e6d8fe12377f38
4
- data.tar.gz: 55ec6ec6d27afffae565d3a81723a514d07289cf
3
+ metadata.gz: 3bc0937314b609cac19dbd32b7a69d5833d23b1a
4
+ data.tar.gz: 11941dc0a6c8b0f3c15b1875606637752641ff2a
5
5
  SHA512:
6
- metadata.gz: 3a45dc9392a008a3e51425616b0742627f25f9a9d8980bd0bcc44f56da7a2181709b7bbfb302c2ce7f0c7702896aa50cc889f74e4b359c129e1586d5d8251b9f
7
- data.tar.gz: 77945dd63bae3e6344fd66eb72552c12579a9aced18c75fbb3e5ea994c1b8356f48c3d7a77adf94f11c6d270d7aa49bd41355cc15806ee21ee0dae2bd0b55e66
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: class_name))
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: class_name, array: coll) if coll.is_a?(Array)
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.constantize
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg