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
- #_conditionally_included?(field) ⇒ Boolean private
- #requested_attributes(fields) ⇒ Object private
- #requested_relationships(fields) ⇒ Object private
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.
75 76 77 78 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 75 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.
65 66 67 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 65 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.
70 71 72 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 70 def requested_relationships(fields) super.select { |k, _| _conditionally_included?(k) } end |