class ApiMaker::Serializer

Attributes

ability[R]
args[R]
model[R]

Public Class Methods

new(ability: nil, args: {}, model:, select: nil) click to toggle source
# File lib/api_maker/serializer.rb, line 14
def initialize(ability: nil, args: {}, model:, select: nil)
  @args = args
  @model = model
  @ability = ability
  @select = select
end
resource_for(klass) click to toggle source
# File lib/api_maker/serializer.rb, line 4
def self.resource_for(klass)
  ApiMaker::MemoryStorage.current.resource_for_model(klass)
rescue NameError
  nil
end
resource_for!(klass) click to toggle source
# File lib/api_maker/serializer.rb, line 10
def self.resource_for!(klass)
  ApiMaker::MemoryStorage.current.resource_for_model(klass)
end

Public Instance Methods

as_json(_options = nil) click to toggle source
# File lib/api_maker/serializer.rb, line 73
def as_json(_options = nil)
  result
end
attribute_value(attribute) click to toggle source
# File lib/api_maker/serializer.rb, line 41
def attribute_value(attribute)
  if resource_instance.respond_to?(attribute)
    resource_instance.__send__(attribute)
  else
    model.__send__(attribute)
  end
end
attributes() click to toggle source
# File lib/api_maker/serializer.rb, line 21
def attributes
  ApiMaker::Configuration.profile("attributes") do
    result = {}
    attributes_to_read.each do |attribute, data|
      if (if_name = data.dig(:args, :if))
        condition_result = attribute_value(if_name)
        next unless condition_result
      end

      result[attribute] = attribute_value(attribute)
    end

    result
  end
end
attributes_to_read() click to toggle source
# File lib/api_maker/serializer.rb, line 37
def attributes_to_read
  @attributes_to_read ||= @select || resource.default_select
end
fetch(*args, &blk) click to toggle source
# File lib/api_maker/serializer.rb, line 49
def fetch(*args, &blk)
  result.fetch(*args, &blk)
end
inspect() click to toggle source
# File lib/api_maker/serializer.rb, line 81
def inspect
  "<ApiMaker::Serializer id=\"#{model.id}\" model=\"#{model.class.name}\" relationships=\"#{relationships}\">"
end
Also aliased as: to_s
relationships() click to toggle source
# File lib/api_maker/serializer.rb, line 53
def relationships
  @relationships ||= {}
end
resource() click to toggle source
# File lib/api_maker/serializer.rb, line 57
def resource
  @resource ||= ApiMaker::MemoryStorage.current.resource_for_model(model.class)
end
resource_instance() click to toggle source
# File lib/api_maker/serializer.rb, line 61
def resource_instance
  @resource_instance ||= resource.new(ability: ability, args: args, model: model)
end
result() click to toggle source
# File lib/api_maker/serializer.rb, line 65
def result
  @result ||= begin
    result = {a: attributes}
    result[:r] = @relationships if @relationships # Only include relationships if set
    result
  end
end
to_json(_options = nil) click to toggle source
# File lib/api_maker/serializer.rb, line 77
def to_json(_options = nil)
  JSON.generate(as_json)
end
to_s()
Alias for: inspect