lazy_record 0.4.3 → 0.4.4

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: b7b7636ef9cc120164c70b0699be114487c9cf44
4
- data.tar.gz: 76b74ad616e38c5d308b56cd1a2bc2a43868a1cc
3
+ metadata.gz: 86fc7c8a4fc3d5cb68c30d8967dccc32e41a300a
4
+ data.tar.gz: 4a3d473eef29dda64e62689394103992a46a1750
5
5
  SHA512:
6
- metadata.gz: c337338e1551509e3685e5dba1008a7c66edd0f939baa88824f06f22dea1dfd4cdd05b3dbfe2b7f35c70e5e953cbe3401416f046c0510b833dc234a6872dce3c
7
- data.tar.gz: 959bd54448280106dd126ee3a3ba19d8e8523bd99061211cc4ce55bdefa4f52e1942fb644d69ce4b484fe77d815074f8682965cd9580e1ffae44e0064ace10d9
6
+ metadata.gz: 2192b9b391256c80b27c2bbc0e8dabe0f617793c8ad502d092c8e88f8fcd75dabbab0313c442386469c4378cead5509b4bb9b4c62b7e09a2b341792b975ebc87
7
+ data.tar.gz: 121c6dc8b06a5a11b7c6bc8895e380156d332c98385ddc0c8685041ed3d2c60e43f9de807ff2fadc6b6befd95e38408497665102fc2f893e46862fe97a4880ed
data/lib/lazy_record.rb CHANGED
@@ -8,8 +8,10 @@ require 'lazy_record/version'
8
8
  require 'lazy_record/associations'
9
9
  require 'lazy_record/attributes'
10
10
  require 'lazy_record/callbacks'
11
+ require 'lazy_record/collections'
11
12
  require 'lazy_record/dynamic_modules'
12
13
  require 'lazy_record/methods'
14
+ require 'lazy_record/nesting'
13
15
  require 'lazy_record/scopes'
14
16
  require 'lazy_record/validations'
15
17
  require 'lazy_record/relation'
@@ -1,81 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'lazy_record/nesting'
4
+
3
5
  module LazyRecord
4
- # Set up in-memory associations between POROs.
6
+ # Set up in-memory one-to-one associations between POROs.
5
7
  module Associations
6
- COLLECTION_MODULE_NAME = :Collections
7
- NESTED_ATTRS_MODULE_NAME = :NestedAttributes
8
-
9
- def define_collection_getter(collection)
10
- model = find_scoped_collection_class(collection)
11
- define_method(collection) do
12
- if instance_variable_get("@#{collection}").nil?
13
- instance_variable_set("@#{collection}", Relation.new(model: model))
14
- end
15
- instance_variable_get("@#{collection}")
16
- end
17
- end
18
-
19
- def define_collection_setter(collection)
20
- model = find_scoped_collection_class(collection)
21
- define_method("#{collection}=") do |coll|
22
- coll = Relation.new(model: model, array: coll) if coll.is_a?(Array)
23
- return instance_variable_set("@#{collection}", coll) if coll.is_a? Relation
24
- raise ArgumentError, "Argument must be a collection of #{collection}"
25
- end
26
- end
27
-
28
- def find_scoped_collection_class(collection)
29
- -> { apply_nesting(collection.to_s.classify).constantize }
30
- end
31
-
32
- def define_collection_counter(collection)
33
- define_method("#{collection}_count") do
34
- send(collection).count
35
- end
36
- end
37
-
38
- def define_collections(*collections)
39
- define_method(:collections) do
40
- collections
41
- end
42
- end
43
-
44
- def define_collection_counts_to_s
45
- define_method(:collection_counts_to_s) do
46
- collections.map do |collection|
47
- "#{collection}_count: #{stringify_value(send("#{collection}_count"))}"
48
- end
49
- end
50
- private :collection_counts_to_s
51
- end
8
+ include LazyRecord::Nesting
52
9
 
53
- def apply_nesting(class_name)
54
- "#{to_s.split('::')[0..-3].join('::')}::#{class_name}"
55
- end
56
-
57
- def lr_has_many(*collections)
58
- include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
59
- mod.extend(Associations)
60
- mod.module_eval { add_collection_methods(collections) }
61
- end
10
+ ASSOCIATION_MODULE_NAME = :Associations
62
11
 
63
12
  def lr_has_one(*args)
64
- include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
13
+ include mod = get_or_set_mod(ASSOCIATION_MODULE_NAME)
65
14
  mod.extend(Associations) unless mod.const_defined?('Associations')
66
15
  mod.module_eval { add_has_one_methods(args) }
67
16
  end
68
17
 
69
- def add_collection_methods(collections)
70
- define_collections(*collections)
71
- define_collection_counts_to_s
72
- collections.each do |collection|
73
- define_collection_getter(collection)
74
- define_collection_setter(collection)
75
- define_collection_counter(collection)
76
- end
77
- end
78
-
79
18
  def add_has_one_methods(args)
80
19
  define_has_one_associations(*args)
81
20
  define_has_one_associations_to_s
@@ -117,25 +56,5 @@ module LazyRecord
117
56
  def find_scoped_association_class(association)
118
57
  -> { apply_nesting(association.to_s.camelize).constantize }
119
58
  end
120
-
121
- def define_collection_attributes_setter(collection, class_name)
122
- define_method("#{collection}_attributes=") do |collection_attributes|
123
- collection_attributes.values.each do |attributes|
124
- send(collection) << class_name.new(attributes)
125
- end
126
- collection
127
- end
128
- end
129
-
130
- def lr_accepts_nested_attributes_for(*collections)
131
- include mod = get_or_set_mod(NESTED_ATTRS_MODULE_NAME)
132
- mod.extend(Associations)
133
- mod.module_eval do
134
- collections.each do |collection|
135
- class_name = collection.to_s.classify.constantize
136
- define_collection_attributes_setter(collection, class_name)
137
- end
138
- end
139
- end
140
59
  end
141
60
  end
@@ -10,6 +10,7 @@ module LazyRecord
10
10
  base.extend ClassMethods
11
11
  base.extend Scopes
12
12
  base.extend Attributes
13
+ base.extend Collections
13
14
  base.extend Associations
14
15
  base.extend Callbacks
15
16
  base.extend Validations
@@ -0,0 +1,91 @@
1
+ require 'lazy_record/nesting'
2
+
3
+ module LazyRecord
4
+ # Set up in-memory one-to-many relationships between objects
5
+ module Collections
6
+ include LazyRecord::Nesting
7
+
8
+ COLLECTION_MODULE_NAME = :Collections
9
+ NESTED_ATTRS_MODULE_NAME = :NestedAttributes
10
+
11
+ def define_collection_getter(collection)
12
+ model = find_scoped_collection_class(collection)
13
+ define_method(collection) do
14
+ if instance_variable_get("@#{collection}").nil?
15
+ instance_variable_set("@#{collection}", Relation.new(model: model))
16
+ end
17
+ instance_variable_get("@#{collection}")
18
+ end
19
+ end
20
+
21
+ def define_collection_setter(collection)
22
+ model = find_scoped_collection_class(collection)
23
+ define_method("#{collection}=") do |coll|
24
+ coll = Relation.new(model: model, array: coll) if coll.is_a?(Array)
25
+ return instance_variable_set("@#{collection}", coll) if coll.is_a? Relation
26
+ raise ArgumentError, "Argument must be a collection of #{collection}"
27
+ end
28
+ end
29
+
30
+ def find_scoped_collection_class(collection)
31
+ -> { apply_nesting(collection.to_s.classify).constantize }
32
+ end
33
+
34
+ def define_collection_counter(collection)
35
+ define_method("#{collection}_count") do
36
+ send(collection).count
37
+ end
38
+ end
39
+
40
+ def define_collections(*collections)
41
+ define_method(:collections) do
42
+ collections
43
+ end
44
+ end
45
+
46
+ def define_collection_counts_to_s
47
+ define_method(:collection_counts_to_s) do
48
+ collections.map do |collection|
49
+ "#{collection}_count: #{stringify_value(send("#{collection}_count"))}"
50
+ end
51
+ end
52
+ private :collection_counts_to_s
53
+ end
54
+
55
+ def lr_has_many(*collections)
56
+ include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
57
+ mod.extend(Collections)
58
+ mod.module_eval { add_collection_methods(collections) }
59
+ end
60
+
61
+ def add_collection_methods(collections)
62
+ define_collections(*collections)
63
+ define_collection_counts_to_s
64
+ collections.each do |collection|
65
+ define_collection_getter(collection)
66
+ define_collection_setter(collection)
67
+ define_collection_counter(collection)
68
+ end
69
+ end
70
+
71
+ def define_collection_attributes_setter(collection, class_name)
72
+ define_method("#{collection}_attributes=") do |collection_attributes|
73
+ collection_attributes.values.each do |attributes|
74
+ send(collection) << class_name.new(attributes)
75
+ end
76
+ collection
77
+ end
78
+ end
79
+
80
+ def lr_accepts_nested_attributes_for(*collections)
81
+ include mod = get_or_set_mod(NESTED_ATTRS_MODULE_NAME)
82
+ mod.extend(Collections)
83
+ mod.module_eval do
84
+ collections.each do |collection|
85
+ class_name = collection.to_s.classify.constantize
86
+ define_collection_attributes_setter(collection, class_name)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,8 @@
1
+ module LazyRecord
2
+ # Apply the same namespace nesting as self to another object.
3
+ module Nesting
4
+ def apply_nesting(class_name)
5
+ "#{to_s.split('::')[0..-3].join('::')}::#{class_name}"
6
+ end
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.4.3'
4
+ VERSION = '0.4.4'
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.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
@@ -136,8 +136,10 @@ files:
136
136
  - lib/lazy_record/base.rb
137
137
  - lib/lazy_record/base_module.rb
138
138
  - lib/lazy_record/callbacks.rb
139
+ - lib/lazy_record/collections.rb
139
140
  - lib/lazy_record/dynamic_modules.rb
140
141
  - lib/lazy_record/methods.rb
142
+ - lib/lazy_record/nesting.rb
141
143
  - lib/lazy_record/relation.rb
142
144
  - lib/lazy_record/scopes.rb
143
145
  - lib/lazy_record/validations.rb