lazy_record 0.7.0 → 0.7.1

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fee6bdb6da8c27b811ad1cb292ddaab4d7828f33
4
- data.tar.gz: a5b556eea6d24c24baac786eb06b18801cfe1991
3
+ metadata.gz: 96f4035ed9553066def5485883f2134475f18812
4
+ data.tar.gz: c7ef20837f4f9e8bce74d38f6b71a22c8e03dcb0
5
5
  SHA512:
6
- metadata.gz: 9fa0cfb63cbe0d972b59442df3f0cd9c5173792ac551109ea0e4be7ba4609f286e1784991c96b96624af442e6dc1972f13e6349f7b5316db5fbeb1347b8ea070
7
- data.tar.gz: f0e9a7b71f97167e4fc41ae6874e5bc23b0bdec00f4f119f801ba9e7d0017cae02f9dff719ab4dbd5d487b59752ee6f189a0ce16a7e420d6e26a67f2024323d4
6
+ metadata.gz: cd311bb1ad1d8e87eadcadbc65c458682826a0b689e65d85a009c4b8f96d3cf63f1324382b4e7aebf70d956248f81e04bf145caa966245e1660747ec9207876f
7
+ data.tar.gz: c890f9790a3e25b079663f1c6cbb9945a2c82f2616a9ce1fa0cf161932b360518d0e99624a4ab02edccc5b549127e48b8d6e01d760e82d3a68cef1538829a5c4
data/lib/lazy_record.rb CHANGED
@@ -32,6 +32,7 @@ require 'lazy_record/class_methods'
32
32
  require 'lazy_record/collections'
33
33
  require 'lazy_record/dynamic_modules'
34
34
  require 'lazy_record/methods'
35
+ require 'lazy_record/nesting'
35
36
  require 'lazy_record/scopes'
36
37
  require 'lazy_record/validations'
37
38
  require 'lazy_record/relation'
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'lazy_record/nesting'
4
+
3
5
  module LazyRecord
4
6
  # Set up in-memory one-to-one associations between POROs.
5
7
  module Associations
8
+ include LazyRecord::Nesting
9
+
6
10
  ASSOCIATION_MODULE_NAME = :Associations
7
11
 
8
12
  def lr_has_one(*args)
@@ -36,7 +40,7 @@ module LazyRecord
36
40
  end
37
41
 
38
42
  def define_association_setter(assoc)
39
- klass = const_get(assoc.to_s.camelize)
43
+ klass = lazily_get_class(assoc.to_s.camelize).call
40
44
  define_method("#{assoc}=") do |val|
41
45
  return instance_variable_set("@#{assoc}", val) if val.is_a? klass
42
46
  raise ArgumentError, "Argument must be a #{klass}"
@@ -1,13 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'lazy_record/nesting'
4
+
3
5
  module LazyRecord
4
6
  # Set up in-memory one-to-many relationships between objects
5
7
  module Collections
8
+ include LazyRecord::Nesting
9
+
6
10
  COLLECTION_MODULE_NAME = :Collections
7
11
  NESTED_ATTRS_MODULE_NAME = :NestedAttributes
8
12
 
9
13
  def _define_collection_getter(collection, options)
10
- klass = const_get(options[:class_name])
14
+ klass = lazily_get_class(options[:class_name]).call
11
15
  module_eval <<-RUBY, __FILE__, __LINE__ + 1
12
16
  def #{collection}
13
17
  @#{collection} ||= Relation.new(klass: #{klass})
@@ -16,7 +20,7 @@ module LazyRecord
16
20
  end
17
21
 
18
22
  def _define_collection_setter(collection, options)
19
- klass = const_get(options[:class_name])
23
+ klass = lazily_get_class(options[:class_name]).call
20
24
  module_eval <<-RUBY, __FILE__, __LINE__ + 1
21
25
  def #{collection}=(coll)
22
26
  @#{collection} = Relation.new(klass: #{klass}, collection: coll)
@@ -76,7 +80,7 @@ module LazyRecord
76
80
  end
77
81
 
78
82
  def define_collection_attributes_setter(collection, options)
79
- class_name = const_get(options.fetch(:class_name))
83
+ class_name = lazily_get_class(options.fetch(:class_name)).call
80
84
  module_eval <<-RUBY, __FILE__, __LINE__ + 1
81
85
  def #{collection}_attributes=(collection_attributes)
82
86
  collection_attributes.values.each do |attributes|
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LazyRecord
4
+ # Apply the same namespace nesting as self to another object.
5
+ module Nesting
6
+ def apply_nesting(class_name)
7
+ "#{to_s.split('::')[0..-2].join('::')}::#{class_name}"
8
+ end
9
+
10
+ def lazily_get_class(class_name)
11
+ lambda do
12
+ begin
13
+ const_get(class_name)
14
+ rescue NameError
15
+ const_get apply_nesting(class_name)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.7.0'
4
+ VERSION = '0.7.1'
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.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
@@ -81,6 +81,7 @@ files:
81
81
  - lib/lazy_record/collections.rb
82
82
  - lib/lazy_record/dynamic_modules.rb
83
83
  - lib/lazy_record/methods.rb
84
+ - lib/lazy_record/nesting.rb
84
85
  - lib/lazy_record/relation.rb
85
86
  - lib/lazy_record/scopes.rb
86
87
  - lib/lazy_record/validations.rb