Created
November 5, 2017 13:08
-
-
Save Altech/d4286431632a03c6a1a5e26531e755bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Api | |
class PreloadRule | |
def initialize | |
@attributes, @associations = {}, {} | |
end | |
attr_accessor :attributes, :associations | |
def attribute(name, includes:) | |
unless name.is_a?(Symbol) | |
raise TypeError.new("Expected Symbol, but got #{name.class}") | |
end | |
unless includes.is_a?(Symbol) | |
raise TypeError.new("Expected Symbol, but got #{includes.class}") | |
end | |
attributes[name] = { includes: includes } | |
end | |
def association(name, includes: nil, serializer: nil) | |
unless name.is_a?(Symbol) | |
raise TypeError.new("Expected Symbol, but got #{name.class}") | |
end | |
unless includes.nil? || includes.is_a?(Symbol) | |
raise TypeError.new("Expected Symbol, but got #{includes.class}") | |
end | |
unless serializer.nil? || serializer.is_a?(String) || (serializer.is_a?(Class) && serializer <= ActiveModel::Serializer) | |
raise TypeError.new("Expected String of class name or an ancestor of ActiveModel::Serializer, but got #{serializer.class}") | |
end | |
# Assume association name to include | |
includes ||= name | |
associations[name.to_sym] = { includes: includes, serializer: serializer } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment