module HttpApiTools::BaseJsonSerializer

Attributes

identity_map[RW]
meta_data[RW]
relation_includes[RW]
result[R]
serializable[R]
serializer_map[RW]
type_key_resolver[R]

Public Class Methods

new(serializable, attrs = {}) click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 14
def initialize(serializable, attrs = {})
  @serializable = serializable
  @relation_includes = attrs[:relation_includes] || RelationIncludes.new
  @result = attrs[:result] || {}
  @meta_data = { type: root_key.to_s.singularize, root_key: root_key.to_s }
end

Public Instance Methods

attribute_hash() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 22
def attribute_hash

  attribute_hash = {}

  attributes.each do |attr_name|
    if self.respond_to?(attr_name)
      attribute_hash[attr_name] = self.send(attr_name)
    else
      attribute_hash[attr_name] = serializable.send(attr_name)
    end
  end

  attribute_hash

end
attributes() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 61
def attributes
  self.class._attributes
end
has_manys() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 69
def has_manys
  self.class.has_manys
end
has_ones() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 65
def has_ones
  self.class.has_ones
end
includable() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 73
def includable
  self.class._includable
end
includes(*includes) click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 43
def includes(*includes)

  if includable
    allowable_includes_to_add = RelationIncludes.new(*includes) & includable
  else
    allowable_includes_to_add = includes
  end

  relation_includes.include(allowable_includes_to_add)

  self
end
meta(data) click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 56
def meta(data)
  meta_data.merge!(data)
  self
end
to_json(*args) click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 39
def to_json(*args)
  JSON.fast_generate(as_json)
end

Private Instance Methods

assert_id_present(serializable_item) click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 98
def assert_id_present(serializable_item)
  raise "serializable items must have an id attribute" unless serializable_item.respond_to?(:id)
end
includes_meta_data() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 91
def includes_meta_data
  {
    includable: includable.to_s.blank? ? '*' : includable.to_s,
    included: relation_includes.to_s
  }
end
root_key() click to toggle source
# File lib/http_api_tools/base_json_serializer.rb, line 87
def root_key
  @_root_key ||= self.class._serializes.name.split("::").last.underscore.pluralize.to_sym
end