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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 735e5daf6fa7d40bdf226e1c8c8ca09c0853e0acc0f18a157d9ed2445380b19c
4
- data.tar.gz: '0808f77eeffc6139aca691c8121e068ad42b3187f7a261647aaf71bec83379a3'
3
+ metadata.gz: fe6d443393897ff1afd8170d5be1d363c4a81d0c173fb661f3e7aee01fb9f39c
4
+ data.tar.gz: d4b204f257a9bbfbf60d55ada9d3cd02efdaf56c13e3ef7edb141bf48c7d31a4
5
5
  SHA512:
6
- metadata.gz: 4c795d6e57064a1409d98b1d61603f3bdb24d1ae784bd598dd7e1a6fef07d581e76eeb2fb2a8a0c33bd1220b5585e6cfeb9a91d6385df6badec5cf71b0e9fee4
7
- data.tar.gz: 7ab2716a86eb341ba0cc4e73f236d29b3190b1e1e003ef74cdfbcf44c1afed4fbb1c6d487dada5b3cd30ff383564f0266ce3e3e3f98a06630ab2e203aa538fba
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch