has_metadata_column 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "has_metadata_column"
8
- s.version = "1.0.2"
8
+ s.version = "1.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tim Morgan"]
12
- s.date = "2012-10-04"
12
+ s.date = "2012-12-14"
13
13
  s.description = "Reduce your table width and migration overhead by moving non-indexed columns to a separate metadata column."
14
14
  s.email = "git@timothymorgan.info"
15
15
  s.extra_rdoc_files = [
@@ -164,15 +164,21 @@ module HasMetadataColumn
164
164
  # @private
165
165
  def as_json(options={})
166
166
  options ||= Hash.new # the JSON encoder can sometimes give us nil options?
167
- options[:except] = Array.wrap(options[:except]) + [ self.class.metadata_column ]
168
- options[:methods] = Array.wrap(options[:methods]) + self.class.metadata_column_fields.keys - options[:except].map(&:to_sym)
167
+ options[:except] = Array.wrap(options[:except]) + [self.class.metadata_column]
168
+ metadata = self.class.metadata_column_fields.keys
169
+ metadata &= Array.wrap(options[:only]) if options[:only]
170
+ metadata -= Array.wrap(options[:except])
171
+ options[:methods] = Array.wrap(options[:methods]) + metadata
169
172
  super options
170
173
  end
171
174
 
172
175
  # @private
173
176
  def to_xml(options={})
174
- options[:except] = Array.wrap(options[:except]) + [ self.class.metadata_column ]
175
- options[:methods] = Array.wrap(options[:methods]) + self.class.metadata_column_fields.keys - options[:except].map(&:to_sym)
177
+ options[:except] = Array.wrap(options[:except]) + [self.class.metadata_column]
178
+ metadata = self.class.metadata_column_fields.keys
179
+ metadata &= Array.wrap(options[:only]) if options[:only]
180
+ metadata -= Array.wrap(options[:except])
181
+ options[:methods] = Array.wrap(options[:methods]) + metadata
176
182
  super options
177
183
  end
178
184
 
@@ -210,11 +216,11 @@ module HasMetadataColumn
210
216
  def inspect
211
217
  "#<#{self.class.to_s} #{attributes.except(self.class.metadata_column.to_s).merge(_metadata_hash.try(:stringify_keys) || {}).map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
212
218
  end
213
-
219
+
214
220
  # @private
215
221
  def reload_with_metadata
216
- res = reload_without_metadata
217
- @_metadata_hash = nil
222
+ res = reload_without_metadata
223
+ @_metadata_hash = nil
218
224
  @_changed_metadata = nil
219
225
  res
220
226
  end
@@ -235,6 +241,8 @@ module HasMetadataColumn
235
241
  def _metadata_hash
236
242
  @_metadata_hash ||= begin
237
243
  send(self.class.metadata_column) ? JSON.parse(send(self.class.metadata_column)) : {}
244
+ rescue ActiveModel::MissingAttributeError
245
+ {}
238
246
  end
239
247
  end
240
248
 
@@ -270,9 +278,9 @@ module HasMetadataColumn
270
278
  options = self.class.metadata_column_fields[attr.to_sym] || {}
271
279
  attribute_will_change! attr
272
280
  @_metadata_hash ||= {}
273
- old = @_metadata_hash[attr.to_s]
281
+ old = @_metadata_hash[attr.to_s]
274
282
  send :"#{self.class.metadata_column}=", @_metadata_hash.merge(attr.to_s => value).to_json
275
- @_metadata_hash = nil
283
+ @_metadata_hash = nil
276
284
  @_changed_metadata[attr] = old
277
285
  value
278
286
  end
@@ -281,7 +289,7 @@ module HasMetadataColumn
281
289
  return query_attribute_without_metadata(attr) unless self.class.metadata_column_fields.include?(attr.to_sym)
282
290
  return false unless (value = send(attr))
283
291
  options = self.class.metadata_column_fields[attr.to_sym] || {}
284
- type = options[:type] || String
292
+ type = options[:type] || String
285
293
  return !value.to_i.zero? if type.ancestors.include?(Numeric)
286
294
  return !value.blank?
287
295
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_metadata_column
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-04 00:00:00.000000000 Z
12
+ date: 2012-12-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails