associates 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/associates.rb +48 -26
- data/lib/associates/persistence.rb +7 -0
- data/lib/associates/version.rb +1 -1
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfcebdf85b7fd77eaf150b1c7dcb688f89c1ade3
|
4
|
+
data.tar.gz: 76ee2577e6119a1d6c8dc02d775b5a1f0c31ec94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a76d3020c9f42ffff29c6b3aa37fa15edb15f48bfa6eb7b67cffd0de32114b0775873629933b25b29217ac3d0a240a10699722a8aff2a6bc4b0d1ee4b01a3789
|
7
|
+
data.tar.gz: a37bdff086f8b109ee45287640e5a3e345079cb4aa34afeff1d999b45178446c8759416d9c7d011048f0b0db4d4dbdf9992baf735ee92e86f18694c488d8e321
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/associates.rb
CHANGED
@@ -69,45 +69,67 @@ module Associates
|
|
69
69
|
# @param options [Hash]
|
70
70
|
# @return [Item]
|
71
71
|
def build_associate(model, options = {})
|
72
|
-
model_name
|
73
|
-
model_klass
|
74
|
-
|
75
|
-
|
72
|
+
model_name = model.to_s.underscore
|
73
|
+
model_klass = (options[:class_name] || model).to_s.classify.constantize
|
74
|
+
dependent_associate_names = (extract_attributes(options[:depends_on]) || []).map(&:to_s)
|
75
|
+
attribute_names = extract_attribute_names(model_klass, options)
|
76
76
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
excluded = BLACKLISTED_ATTRIBUTES.to_a
|
81
|
-
|
82
|
-
if options[:except]
|
83
|
-
excluded += extract_attributes(options[:except]).map(&:to_s)
|
84
|
-
end
|
77
|
+
ensure_name_uniqueness(associates.map(&:name), model_name)
|
78
|
+
ensure_attribute_uniqueness(associates.map(&:attribute_names), attribute_names) if options[:delegate]
|
79
|
+
ensure_dependent_names_existence(associates.map(&:name), dependent_associate_names)
|
85
80
|
|
86
|
-
|
87
|
-
|
81
|
+
Item.new(model_name, model_klass, attribute_names, dependent_associate_names, options)
|
82
|
+
end
|
88
83
|
|
89
|
-
|
90
|
-
|
84
|
+
# Ensure associate name don't clash with already declared ones
|
85
|
+
#
|
86
|
+
# @param associates_names [Array]
|
87
|
+
# @param name [String]
|
88
|
+
def ensure_name_uniqueness(associates_names, name)
|
89
|
+
if associates_names.include?(name)
|
91
90
|
raise NameError, "already defined associate name '#{model_name}' for #{name}(#{object_id})"
|
92
91
|
end
|
92
|
+
end
|
93
93
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
94
|
+
# Ensure associate attribute names don't clash with already declared ones
|
95
|
+
#
|
96
|
+
# @param associates_attribute_names [Array]
|
97
|
+
# @param attribute_names [Array]
|
98
|
+
def ensure_attribute_uniqueness(associates_attribute_names, attribute_names)
|
99
|
+
attribute_names.each do |attribute_name|
|
100
|
+
if associates_attribute_names.include?(attribute_name)
|
101
|
+
raise NameError, "already defined attribute name '#{attribute_name}' for #{name}(#{object_id})"
|
100
102
|
end
|
101
103
|
end
|
104
|
+
end
|
102
105
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
+
# Ensure associate dependent names exists
|
107
|
+
#
|
108
|
+
# @param associates_names [Array]
|
109
|
+
# @param dependent_associate_names [Array]
|
110
|
+
def ensure_dependent_names_existence(associates_names, dependent_associate_names)
|
111
|
+
dependent_associate_names.each do |dependent_name|
|
112
|
+
unless associates_names.include?(dependent_name)
|
106
113
|
raise NameError, "undefined associated model '#{dependent_name}' for #{name}(#{object_id})"
|
107
114
|
end
|
108
115
|
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# @param model_klass [Class]
|
119
|
+
# @param options [Hash]
|
120
|
+
# @return [Array]
|
121
|
+
def extract_attribute_names(model_klass, options)
|
122
|
+
if options[:only]
|
123
|
+
extract_attributes(options[:only])
|
124
|
+
else
|
125
|
+
excluded = BLACKLISTED_ATTRIBUTES.to_a
|
126
|
+
|
127
|
+
if options[:except]
|
128
|
+
excluded += extract_attributes(options[:except]).map(&:to_s)
|
129
|
+
end
|
109
130
|
|
110
|
-
|
131
|
+
model_klass.attribute_names.reject { |name| excluded.include?(name) }
|
132
|
+
end
|
111
133
|
end
|
112
134
|
|
113
135
|
# Define associated model attribute methods delegation
|
@@ -27,6 +27,13 @@ module Associates
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
# @return [Boolean] Wether or not all models are persited
|
31
|
+
def persisted?
|
32
|
+
associates.all? do |associate|
|
33
|
+
send(associate.name).send(:persisted?)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
30
37
|
# @return [True, ActiveRecord::RecordInvalid]
|
31
38
|
def save!
|
32
39
|
save || raise(ActiveRecord::RecordInvalid)
|
data/lib/associates/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: associates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Philippe Dionne
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
DuNlzvjhWtPDwE5mYO5x5XquWQuENw78urt1aioNrIe0/15dHpDoIEDILa6zU46B
|
31
31
|
XDtp1YxdeVGIBuNoP1vjDSvNKYj0pMjRAEPrqK39jKE=
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2013-
|
33
|
+
date: 2013-11-05 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: activerecord
|
metadata.gz.sig
CHANGED
Binary file
|