Module: JSONAPI::Serializable::Resource::ConditionalFields
- Defined in:
- lib/jsonapi/serializable/resource/conditional_fields.rb
Overview
Extension for handling conditional fields in serializable resources.
Class Method Summary collapse
Instance Method Summary collapse
- #_register_condition(condition_blocks, name, options) ⇒ Object private
-
#attribute(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for attributes.
- #inherited(klass) ⇒ Object
-
#link(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for links.
-
#relationship(name, options = {}, &block) ⇒ Object
(also: #has_many, #has_one, #belongs_to)
Handle the ‘if` and `unless` options for relationships (has_one, belongs_to, has_many).
Class Method Details
.extended(klass) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 24 def self.extended(klass) klass.class_eval do include InstanceMethods class << self attr_accessor :field_condition_blocks attr_accessor :link_condition_blocks end self.field_condition_blocks ||= {} self.link_condition_blocks ||= {} end end |
.prepended(klass) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 15 def self.prepended(klass) warn " DERPRECATION WARNING (called from \#{caller_locations(1...2).first}):\n Prepending `\#{name}' is deprecated and will be removed in future releases. Use `Object#extend' instead.\n EOT\n\n klass.extend self\nend\n" |
Instance Method Details
#_register_condition(condition_blocks, 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.
82 83 84 85 86 87 88 89 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 82 def _register_condition(condition_blocks, 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.
47 48 49 50 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 47 def attribute(name, = {}, &block) super _register_condition(field_condition_blocks, name, ) end |
#inherited(klass) ⇒ Object
36 37 38 39 40 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 36 def inherited(klass) super klass.field_condition_blocks = field_condition_blocks.dup klass.link_condition_blocks = link_condition_blocks.dup end |
#link(name, options = {}, &block) ⇒ Object
Handle the ‘if` and `unless` options for links.
70 71 72 73 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 70 def link(name, = {}, &block) super(name, &block) _register_condition(link_condition_blocks, name, ) end |
#relationship(name, options = {}, &block) ⇒ Object Also known as: has_many, has_one, belongs_to
Handle the ‘if` and `unless` options for relationships (has_one,
belongs_to, has_many).
58 59 60 61 |
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 58 def relationship(name, = {}, &block) super _register_condition(field_condition_blocks, name, ) end |