Module: JSONAPIonify::Api::Resource::Definitions::Attributes

Defined in:
lib/jsonapionify/api/resource/definitions/attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 4

def self.extended(klass)
  klass.class_eval do
    extend JSONAPIonify::InheritedAttributes
    extend JSONAPIonify::Types
    inherited_array_attribute :attributes
    delegate :id_attribute, :attributes, to: :class

    context(:fields, readonly: true) do |params:|
      params['fields']&.map do |type, fields|
        [type, fields.split(',')]
      end&.to_h
    end
  end
end

Instance Method Details

#attribute(name, type, description = '', **options, &block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 25

def attribute(name, type, description = '', **options, &block)
  Attribute.new(
    name, type, description, **options, &block
  ).tap do |new_attribute|
    attributes.delete(new_attribute)
    attributes << new_attribute
  end
end

#builder(&block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 38

def builder(&block)
  context :builder, readonly: true, persisted: true do |context|
    proc do |resource, instance|
      block.call resource, instance, context
    end
  end
end

#id(sym) ⇒ Object



19
20
21
22
23
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 19

def id(sym)
  define_singleton_method :id_attribute do
    sym
  end
end

#remove_attribute(name) ⇒ Object



34
35
36
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 34

def remove_attribute(name)
  attributes.delete_if { |attr| attr.name == name.to_sym }
end