Module: HttpApiTools::JsonSerializerDsl

Defined in:
lib/http_api_tools/json_serializer_dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_to(serializer_class) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/http_api_tools/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

Instance Method Details

#attributes(*args) ⇒ Object



35
36
37
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 35

def attributes(*args)
  self._attributes = args
end

#has_many(has_many) ⇒ Object



43
44
45
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 43

def has_many(has_many)
  self.has_manys << has_many
end

#has_manysObject



31
32
33
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 31

def has_manys
  self._relationships[:has_manys]
end

#has_one(has_one) ⇒ Object



39
40
41
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 39

def has_one(has_one)
  self.has_ones << has_one
end

#has_onesObject



27
28
29
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 27

def has_ones
  self._relationships[:has_ones]
end

#includable(*includes) ⇒ Object



47
48
49
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 47

def includable(*includes)
  self._includable = RelationIncludes.new(*includes)
end

#serializable_typeObject



23
24
25
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 23

def serializable_type
  self._serializes
end

#serializer_typeObject



51
52
53
54
55
56
57
58
59
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 51

def serializer_type
  if self.ancestors.any? { |klass| klass == HttpApiTools::Sideloading::JsonSerializer }
    :sideloading
  elsif self.ancestors.any? { |klass| klass == HttpApiTools::Nesting::JsonSerializer }
    :nesting
  else
    raise "Unsupported serializer_type. Must be one of either 'sideloading' or 'nesting' serializer."
  end
end

#serializes(klass) ⇒ Object



18
19
20
21
# File 'lib/http_api_tools/json_serializer_dsl.rb', line 18

def serializes(klass)
  self._serializes = klass
  HttpApiTools::SerializerRegistry.instance.register(serializer_type, klass, self)
end