schema-model 0.4.7 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/schema/associations/has_many.rb +4 -0
- data/lib/schema/associations/has_one.rb +4 -0
- data/lib/schema/model.rb +3 -1
- data/lib/schema/utils.rb +32 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe6d443393897ff1afd8170d5be1d363c4a81d0c173fb661f3e7aee01fb9f39c
|
4
|
+
data.tar.gz: d4b204f257a9bbfbf60d55ada9d3cd02efdaf56c13e3ef7edb141bf48c7d31a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6127881ad0492a5d1ae67c15dd8a06cc03f7f128094ccf0ed41b9805e8b3b05a4da2e93aa9934dfe9c92575ab773a76be5edbd695de245854c47fc36bfa2d25
|
7
|
+
data.tar.gz: 8f4edb0b2a479eba63a4ffd81e8d3019486bf64f2e616a1cb6e73c6551b3abd0d5fed2e757487aa828d7d42d95a770e1b7f121eee722a5d3a88f9149a9e80d2e
|
@@ -32,6 +32,10 @@ STR
|
|
32
32
|
|
33
33
|
kls = const_get(options[:class_name])
|
34
34
|
kls.class_eval(&block) if block
|
35
|
+
if options[:default]
|
36
|
+
options[:default_code] = '[]'
|
37
|
+
::Schema::Utils.add_association_default_methods(self, options)
|
38
|
+
end
|
35
39
|
add_aliases(name, options)
|
36
40
|
kls
|
37
41
|
end
|
@@ -32,6 +32,10 @@ STR
|
|
32
32
|
|
33
33
|
kls = const_get(options[:class_name])
|
34
34
|
kls.class_eval(&block) if block
|
35
|
+
if options[:default]
|
36
|
+
options[:default_code] = options[:class_name] + '.new'
|
37
|
+
::Schema::Utils.add_association_default_methods(self, options)
|
38
|
+
end
|
35
39
|
add_aliases(name, options)
|
36
40
|
kls
|
37
41
|
end
|
data/lib/schema/model.rb
CHANGED
@@ -18,7 +18,8 @@ module Schema
|
|
18
18
|
type: type,
|
19
19
|
getter: name.to_s.freeze,
|
20
20
|
setter: "#{name}=",
|
21
|
-
instance_variable: "@#{name}"
|
21
|
+
instance_variable: "@#{name}",
|
22
|
+
default_method: "#{name}_default"
|
22
23
|
}
|
23
24
|
end
|
24
25
|
|
@@ -53,6 +54,7 @@ module Schema
|
|
53
54
|
|
54
55
|
add_value_to_class_method(:schema, name => options)
|
55
56
|
add_attribute_methods(name, options)
|
57
|
+
::Schema::Utils.add_attribute_default_methods(self, options) if options.has_key?(:default)
|
56
58
|
add_aliases(name, options)
|
57
59
|
end
|
58
60
|
|
data/lib/schema/utils.rb
CHANGED
@@ -54,5 +54,37 @@ module Schema
|
|
54
54
|
|
55
55
|
kls.send(:include, ::Schema::Associations::DynamicTypes)
|
56
56
|
end
|
57
|
+
|
58
|
+
def add_attribute_default_methods(kls, options)
|
59
|
+
kls.class_eval(
|
60
|
+
<<-STR, __FILE__, __LINE__ + 1
|
61
|
+
def #{options[:default_method]}
|
62
|
+
#{options[:default].inspect}
|
63
|
+
end
|
64
|
+
|
65
|
+
def #{options[:getter]}
|
66
|
+
if #{options[:instance_variable]}.nil?
|
67
|
+
#{options[:default_method]}
|
68
|
+
else
|
69
|
+
#{options[:instance_variable]}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
STR
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
def add_association_default_methods(kls, options)
|
77
|
+
kls.class_eval(
|
78
|
+
<<-STR, __FILE__, __LINE__ + 1
|
79
|
+
def #{options[:default_method]}
|
80
|
+
#{options[:default_code]}
|
81
|
+
end
|
82
|
+
|
83
|
+
def #{options[:getter]}
|
84
|
+
#{options[:instance_variable]} ||= #{options[:default_method]}
|
85
|
+
end
|
86
|
+
STR
|
87
|
+
)
|
88
|
+
end
|
57
89
|
end
|
58
90
|
end
|