class BrightSerializer::Attribute

Attributes

block[R]
condition[R]
entity[R]
key[R]
transformed_key[RW]

Public Class Methods

new(key, condition, entity, &block) click to toggle source
# File lib/bright_serializer/attribute.rb, line 10
def initialize(key, condition, entity, &block)
  @key = key
  @condition = condition
  @block = block
  @entity = entity ? Entity::Base.new(entity) : nil
end

Public Instance Methods

condition?(object, params) click to toggle source
# File lib/bright_serializer/attribute.rb, line 32
def condition?(object, params)
  return true unless @condition

  @condition.call(object, params)
end
serialize(object, params) click to toggle source
# File lib/bright_serializer/attribute.rb, line 17
def serialize(object, params)
  return unless object

  value =
    if @block
      @block.arity.abs == 1 ? object.instance_eval(&@block) : object.instance_exec(object, params, &@block)
    elsif object.is_a?(Hash)
      object.key?(key) ? object[key] : object[key.to_s]
    else
      object.send(key)
    end

  value.respond_to?(:serializable_hash) ? value.serializable_hash : value
end