Module: JSONAPI::Serializable::Resource::ConditionalFields

Defined in:
lib/jsonapi/serializable/resource/conditional_fields.rb

Overview

Extension for handling conditional fields in serializable resources.

Defined Under Namespace

Modules: DSL

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 15

def self.prepended(klass)
  klass.class_eval do
    extend DSL
    class << self
      attr_accessor :condition_blocks
    end
    self.condition_blocks ||= {}
  end
end

Instance Method Details

#_conditionally_included?(field) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

81
82
83
84
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 81

def _conditionally_included?(field)
  condition = self.class.condition_blocks[field]
  condition.nil? || instance_exec(&condition)
end

#requested_attributes(fields) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


71
72
73
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 71

def requested_attributes(fields)
  super.select { |k, _| _conditionally_included?(k) }
end

#requested_relationships(fields) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


76
77
78
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 76

def requested_relationships(fields)
  super.select { |k, _| _conditionally_included?(k) }
end