class BrightSerializer::Entity::Base

Constants

DEFAULT_DEFINITION

Public Class Methods

new(definition) click to toggle source

swagger.io/specification/v2/?sbsearch=array%20response#schema-object

# File lib/bright_serializer/entity/base.rb, line 12
def initialize(definition)
  @definition = definition
end

Public Instance Methods

nested_hash(obj, key) click to toggle source
# File lib/bright_serializer/entity/base.rb, line 31
def nested_hash(obj, key)
  if obj.respond_to?(:key?) && obj.key?(key)
    obj
  elsif obj.respond_to?(:each)
    r = nil
    obj.find { |*a| r = nested_hash(a.last, key) }
    r
  end
end
parse_ref!() click to toggle source
# File lib/bright_serializer/entity/base.rb, line 22
def parse_ref!
  object = nested_hash(@definition, 'ref')
  return unless object

  ref_entity_name = Inflector.constantize(object.delete('ref')).entity_name
  relation = "#/definitions/#{ref_entity_name}"
  object['$ref'] = relation
end
to_h() click to toggle source
# File lib/bright_serializer/entity/base.rb, line 16
def to_h
  Inflector.deep_transform_keys_in_object!(@definition) { |k| Inflector.camel_lower k.to_s }
  parse_ref!
  @definition
end