Module: JSONAPI::Serializable::Resource::ConditionalFields::DSL
- Defined in:
- lib/jsonapi/serializable/resource/conditional_fields.rb
Overview
DSL extensions for conditional fields.
Instance Method Summary collapse
- #_register_condition(name, options) ⇒ Object private
-
#attribute(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for attributes.
- #inherited(klass) ⇒ Object
-
#relationship(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for relationships (has_one, belongs_to, has_many).
Instance Method Details
#_register_condition(name, options) ⇒ 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.
54 55 56 57 58 59 60 61 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 54 def _register_condition(name, ) condition_blocks[name.to_sym] = if .key?(:if) [:if] elsif .key?(:unless) proc { !instance_exec(&[:unless]) } end end |
#attribute(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for attributes.
37 38 39 40 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 37 def attribute(name, = {}, &block) super _register_condition(name, ) end |
#inherited(klass) ⇒ Object
27 28 29 30 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 27 def inherited(klass) super klass.condition_blocks = condition_blocks.dup end |
#relationship(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for relationships (has_one,
belongs_to, has_many).
48 49 50 51 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 48 def relationship(name, = {}, &block) super _register_condition(name, ) end |