lazy_record 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/example/person.rb +1 -1
- data/lib/lazy_record/associations.rb +25 -0
- data/lib/lazy_record/attributes.rb +3 -22
- data/lib/lazy_record/base_module.rb +21 -2
- 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: 9fcd565fc270b0c3ecb5278894e3f6eb1c3287d4
|
4
|
+
data.tar.gz: b647814ace73eda6f34300868e852cdbe5eb6724
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ccfaad3e66cd2c7c516f1a791d12dd0d5fab7c5ff0dd5b9c5f1ed01bd09ecde2c584aa78ed1a47e76421fd4a682a55d4c19ca70cab25dc1efe7c5612e80db09
|
7
|
+
data.tar.gz: cc8e35116bb835526f000f498e71f79e3d7a3d6d3101b0d9921cf5a789c1020f4b0bdf38f39b8e7395ef39f30afcd541360d814b6a0f6f059929d2b783c29d39
|
data/.gitignore
CHANGED
data/example/person.rb
CHANGED
@@ -25,6 +25,28 @@ module LazyRecord
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
def define_collection_counter(collection)
|
29
|
+
define_method("#{collection}_count") do
|
30
|
+
send(collection).count
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_collections(*collections)
|
35
|
+
define_method(:collections) do
|
36
|
+
instance_variable_get(:@collections) || instance_variable_set(:@collections,
|
37
|
+
collections)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def define_collection_counts_to_s
|
42
|
+
define_method(:collection_counts_to_s) do
|
43
|
+
collections.map do |collection|
|
44
|
+
"#{collection}_count: #{stringify_value(send("#{collection}_count"))}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
private :collection_counts_to_s
|
48
|
+
end
|
49
|
+
|
28
50
|
def apply_nesting(class_name)
|
29
51
|
"#{to_s.split('::')[0..-3].join('::')}::#{class_name}"
|
30
52
|
end
|
@@ -33,10 +55,13 @@ module LazyRecord
|
|
33
55
|
include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
|
34
56
|
mod.extend(Associations)
|
35
57
|
mod.module_eval do
|
58
|
+
define_collections(*collections)
|
59
|
+
define_collection_counts_to_s
|
36
60
|
collections.each do |collection|
|
37
61
|
class_name = collection.to_s.classify
|
38
62
|
define_collection_getter(collection, class_name)
|
39
63
|
define_collection_setter(collection, class_name)
|
64
|
+
define_collection_counter(collection)
|
40
65
|
end
|
41
66
|
end
|
42
67
|
end
|
@@ -21,13 +21,8 @@ module LazyRecord
|
|
21
21
|
|
22
22
|
def define_initialize
|
23
23
|
define_method(:initialize) do |opts = {}, &block|
|
24
|
-
opts
|
25
|
-
|
26
|
-
memo
|
27
|
-
end
|
28
|
-
|
29
|
-
instance_attr_accessors.each do |attr|
|
30
|
-
send("#{attr}=", opts[attr.to_sym])
|
24
|
+
opts.each do |k, v|
|
25
|
+
send("#{k}=", v) if respond_to?("#{k}=")
|
31
26
|
end
|
32
27
|
|
33
28
|
block&.call self
|
@@ -39,25 +34,12 @@ module LazyRecord
|
|
39
34
|
define_method(:instance_attrs_to_s) do
|
40
35
|
instance_attr_accessors.map do |attr|
|
41
36
|
value = send(attr)
|
42
|
-
|
43
|
-
"#{attr.to_s.delete(':')}: #{attr_to_s}"
|
37
|
+
"#{attr}: #{stringify_value(value)}"
|
44
38
|
end
|
45
39
|
end
|
46
40
|
private :instance_attrs_to_s
|
47
41
|
end
|
48
42
|
|
49
|
-
def define_stringify_value
|
50
|
-
define_method(:stringify_value) do |value|
|
51
|
-
if value.is_a?(String)
|
52
|
-
"\"#{value}\""
|
53
|
-
elsif value.nil?
|
54
|
-
'nil'
|
55
|
-
else
|
56
|
-
value
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
43
|
def define_instance_attr_accessors(*names)
|
62
44
|
define_method(:instance_attr_accessors) do
|
63
45
|
names.map(&:to_sym)
|
@@ -73,7 +55,6 @@ module LazyRecord
|
|
73
55
|
define_instance_attr_accessors(*names)
|
74
56
|
define_initialize
|
75
57
|
define_instance_attrs_to_s
|
76
|
-
define_stringify_value
|
77
58
|
end
|
78
59
|
end
|
79
60
|
end
|
@@ -31,16 +31,35 @@ module LazyRecord
|
|
31
31
|
[]
|
32
32
|
end
|
33
33
|
|
34
|
+
def collection_counts_to_s
|
35
|
+
[]
|
36
|
+
end
|
37
|
+
|
34
38
|
def inspect
|
35
39
|
"#<#{self.class} id: #{id ? id : 'nil'}"\
|
36
|
-
"#{instance_attrs_to_s.unshift('').join(', ')}
|
40
|
+
"#{instance_attrs_to_s.unshift('').join(', ')}"\
|
41
|
+
"#{collection_counts_to_s.unshift('').join(', ')}>"
|
37
42
|
end
|
38
43
|
|
39
44
|
def id
|
40
45
|
@id.freeze
|
41
46
|
end
|
42
47
|
|
43
|
-
|
48
|
+
def stringify_value(value)
|
49
|
+
if value.is_a?(String)
|
50
|
+
"\"#{value}\""
|
51
|
+
elsif value.nil?
|
52
|
+
'nil'
|
53
|
+
else
|
54
|
+
value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private :id=,
|
59
|
+
:stringify_value,
|
60
|
+
:instance_attrs_to_s,
|
61
|
+
:instance_attr_accessors,
|
62
|
+
:collection_counts_to_s
|
44
63
|
|
45
64
|
# Class methods provided to all LazyRecord classes
|
46
65
|
module ClassMethods
|
data/lib/lazy_record/version.rb
CHANGED