Module: HasMetadataColumn::Extensions

Defined in:
lib/has_metadata_column.rb

Instance Method Summary collapse

Instance Method Details

#_attribute(attr) ⇒ Object



266
267
268
269
# File 'lib/has_metadata_column.rb', line 266

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

#attribute(attr) ⇒ Object

ATTRIBUTE MATCHER METHODS



251
252
253
254
255
256
257
# File 'lib/has_metadata_column.rb', line 251

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



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

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



259
260
261
262
263
264
# File 'lib/has_metadata_column.rb', line 259

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)


290
291
292
# File 'lib/has_metadata_column.rb', line 290

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

#attribute_will_change!(attr) ⇒ Object



242
243
244
245
246
247
# File 'lib/has_metadata_column.rb', line 242

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

#keys_for_partial_writeObject



294
295
296
# File 'lib/has_metadata_column.rb', line 294

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

#query_attribute(attr) ⇒ Object



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

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