module Hat::JsonSerializerDsl

Public Class Methods

apply_to(serializer_class) click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 6
def self.apply_to(serializer_class)
  serializer_class.class_attribute :_attributes
  serializer_class.class_attribute :_relationships
  serializer_class.class_attribute :_includable
  serializer_class.class_attribute :_serializes

  serializer_class._attributes = []
  serializer_class._relationships = { has_ones: [], has_manys: [] }

  serializer_class.extend(self)
end

Public Instance Methods

attributes(*args) click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 35
def attributes(*args)
  self._attributes = args
end
has_many(has_many) click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 43
def has_many(has_many)
  self.has_manys << has_many
end
has_manys() click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 31
def has_manys
  self._relationships[:has_manys]
end
has_one(has_one) click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 39
def has_one(has_one)
  self.has_ones << has_one
end
has_ones() click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 27
def has_ones
  self._relationships[:has_ones]
end
includable(*includes) click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 47
def includable(*includes)
  self._includable = RelationIncludes.new(*includes)
end
serializable_type() click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 23
def serializable_type
  self._serializes
end
serializer_type() click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 51
def serializer_type
  if self.ancestors.any? { |klass| klass == Hat::Sideloading::JsonSerializer }
    :sideloading
  elsif self.ancestors.any? { |klass| klass == Hat::Nesting::JsonSerializer }
    :nesting
  else
    raise "Unsupported serializer_type. Must be one of either 'sideloading' or 'nesting' serializer."
  end
end
serializes(klass) click to toggle source
# File lib/hat/json_serializer_dsl.rb, line 18
def serializes(klass)
  self._serializes = klass
  Hat::SerializerRegistry.instance.register(serializer_type, klass, self)
end