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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1de008a6d97d2dbf9f02295947c05d758d519191
4
- data.tar.gz: 53485eb8cab825705f024869bd53c78417c441c4
3
+ metadata.gz: bfcebdf85b7fd77eaf150b1c7dcb688f89c1ade3
4
+ data.tar.gz: 76ee2577e6119a1d6c8dc02d775b5a1f0c31ec94
5
5
  SHA512:
6
- metadata.gz: 0153eb8ef6935fc08ec4de4a53d126bf441d1500b1e32662d16203f806eb43cdf5f7a995d0763ef1fe09218fa9abeb1be9e23c1a0384217c02504f8b98749cd8
7
- data.tar.gz: 2a37522a4e24f6ca6c3971d147c8d02c4d736e6e930e611e7b74f1c10163440c3b92eb08564dbf81098402f9a8b5eebbbfbb76adbd5d33397c2afcc01c7b726d
6
+ metadata.gz: a76d3020c9f42ffff29c6b3aa37fa15edb15f48bfa6eb7b67cffd0de32114b0775873629933b25b29217ac3d0a240a10699722a8aff2a6bc4b0d1ee4b01a3789
7
+ data.tar.gz: a37bdff086f8b109ee45287640e5a3e345079cb4aa34afeff1d999b45178446c8759416d9c7d011048f0b0db4d4dbdf9992baf735ee92e86f18694c488d8e321
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -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 = model.to_s.underscore
73
- model_klass = (options[:class_name] || model).to_s.classify.constantize
74
- dependent_models_names = extract_attributes(options[:depends_on]) || []
75
- dependent_models_names = dependent_models_names.map(&:to_s)
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
- if options[:only]
78
- attribute_names = extract_attributes(options[:only])
79
- else
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
- attribute_names = model_klass.attribute_names.reject { |name| excluded.include?(name) }
87
- end
81
+ Item.new(model_name, model_klass, attribute_names, dependent_associate_names, options)
82
+ end
88
83
 
89
- # Ensure associate name don't clash with already declared ones
90
- if associates.map(&:name).include?(model_name)
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
- # Ensure associate attribute names don't clash with already declared ones
95
- if options[:delegate]
96
- attribute_names.each do |attribute_name|
97
- if associates.map(&:attribute_names).include?(attribute_name)
98
- raise NameError, "already defined attribute name '#{attribute_name}' for #{name}(#{object_id})"
99
- end
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
- # Ensure associate dependent names exists
104
- dependent_models_names.each do |dependent_name|
105
- unless associates.map(&:name).include?(dependent_name)
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
- Item.new(model_name, model_klass, attribute_names, dependent_models_names, options)
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)
@@ -1,3 +1,3 @@
1
1
  module Associates
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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.2
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-10-27 00:00:00.000000000 Z
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