Module: HasMetadataColumn::Extensions

Defined in:
lib/has_metadata_column.rb

Instance Method Summary collapse

Instance Method Details

#_attribute(attr) ⇒ Object



264
265
266
267
# File 'lib/has_metadata_column.rb', line 264

def _attribute(attr)
  return super unless self.class..include?(attr.to_sym)
  attribute attr
end

#attribute(attr) ⇒ Object

ATTRIBUTE MATCHER METHODS



249
250
251
252
253
254
255
# File 'lib/has_metadata_column.rb', line 249

def attribute(attr)
  return super unless self.class..include?(attr.to_sym)

  options = self.class.[attr.to_sym] || {}
  default = options.include?(:default) ? options[:default] : nil
  .include?(attr) ? HasMetadataColumn.([attr], options[:type]) : default
end

#attribute=(attr, value) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/has_metadata_column.rb', line 269

def attribute=(attr, value)
  return super unless self.class..include?(attr.to_sym)

  attribute_will_change! attr
  old = [attr.to_s]
  send :"#{self.class.}=", .merge(attr.to_s => value).to_json
  @_metadata_hash = nil
  value
end

#attribute_before_type_cast(attr) ⇒ Object



257
258
259
260
261
262
# File 'lib/has_metadata_column.rb', line 257

def attribute_before_type_cast(attr)
  return super unless self.class..include?(attr.to_sym)
  options = self.class.[attr.to_sym] || {}
  default = options.include?(:default) ? options[:default] : nil
  .include?(attr) ? [attr] : default
end

#attribute_method?(attr) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/has_metadata_column.rb', line 288

def attribute_method?(attr)
  self.class..include?(attr.to_sym) || super
end

#attribute_will_change!(attr) ⇒ Object



240
241
242
243
244
245
# File 'lib/has_metadata_column.rb', line 240

def attribute_will_change!(attr)
  unless attribute_names.include?(attr)
    send :"#{self.class.}_will_change!"
  end
  super
end

#keys_for_partial_writeObject



292
293
294
# File 'lib/has_metadata_column.rb', line 292

def keys_for_partial_write
  super - self.class..keys.map(&:to_s)
end

#query_attribute(attr) ⇒ Object



279
280
281
282
283
284
285
286
# File 'lib/has_metadata_column.rb', line 279

def query_attribute(attr)
  return super unless self.class..include?(attr.to_sym)
  return false unless (value = send(attr))
  options = self.class.[attr.to_sym] || {}
  type    = options[:type] || String
  return !value.to_i.zero? if type.ancestors.include?(Numeric)
  return !value.blank?
end