Skip to content

Instantly share code, notes, and snippets.

@Altech
Created November 5, 2017 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Altech/d4286431632a03c6a1a5e26531e755bc to your computer and use it in GitHub Desktop.
Save Altech/d4286431632a03c6a1a5e26531e755bc to your computer and use it in GitHub Desktop.
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