Method: JSONAPI::BasicResource.attribute

Defined in:
lib/jsonapi/basic_resource.rb

.attribute(attribute_name, options = {}) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/jsonapi/basic_resource.rb', line 532

def attribute(attribute_name, options = {})
  _clear_cached_attribute_options
  _clear_fields_cache

  attr = attribute_name.to_sym

  check_reserved_attribute_name(attr)

  if (attr == :id) && (options[:format].nil?)
    ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
  end

  check_duplicate_attribute_name(attr) if options[:format].nil?

  @_attributes ||= {}
  @_attributes[attr] = options
  define_method attr do
    @model.public_send(options[:delegate] ? options[:delegate].to_sym : attr)
  end unless method_defined?(attr)

  define_method "#{attr}=" do |value|
    @model.public_send("#{options[:delegate] ? options[:delegate].to_sym : attr}=", value)
  end unless method_defined?("#{attr}=")

  if options.fetch(:sortable, true) && !_has_sort?(attr)
    sort attr
  end
end