mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Extract DSL methods (meta programming help)
This commit is contained in:
parent
838388dc03
commit
9c1a9e18c4
29
lib/ams/dsl_support.rb
Normal file
29
lib/ams/dsl_support.rb
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module AMS
|
||||||
|
module DSLSupport
|
||||||
|
# @api private
|
||||||
|
# Macro to add an instance method to the receiver
|
||||||
|
def add_instance_method(body, receiver)
|
||||||
|
cl = caller_locations[0]
|
||||||
|
silence_warnings { receiver.module_eval body, cl.absolute_path, cl.lineno }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
# Macro to add a class method to the receiver
|
||||||
|
def add_class_method(body, receiver)
|
||||||
|
cl = caller_locations[0]
|
||||||
|
silence_warnings { receiver.class_eval body, cl.absolute_path, cl.lineno }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
# Silence warnings, primarily when redefining methods
|
||||||
|
def silence_warnings
|
||||||
|
original_verbose = $VERBOSE
|
||||||
|
$VERBOSE = nil
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
$VERBOSE = original_verbose
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
require "json"
|
require "json"
|
||||||
require "ams/inflector"
|
require "ams/inflector"
|
||||||
|
require "ams/dsl_support"
|
||||||
module AMS
|
module AMS
|
||||||
# Lightweight mapping of a model to a JSON API resource object
|
# Lightweight mapping of a model to a JSON API resource object
|
||||||
# with attributes and relationships
|
# with attributes and relationships
|
||||||
@ -31,6 +32,7 @@ module AMS
|
|||||||
# ums = UserModelSerializer.new(user)
|
# ums = UserModelSerializer.new(user)
|
||||||
# ums.to_json
|
# ums.to_json
|
||||||
class Serializer < BasicObject
|
class Serializer < BasicObject
|
||||||
|
extend DSLSupport
|
||||||
# delegate constant lookup to Object
|
# delegate constant lookup to Object
|
||||||
def self.const_missing(name)
|
def self.const_missing(name)
|
||||||
::Object.const_get(name)
|
::Object.const_get(name)
|
||||||
@ -40,30 +42,6 @@ module AMS
|
|||||||
attr_accessor :_attributes, :_relations, :_id_field, :_type
|
attr_accessor :_attributes, :_relations, :_id_field, :_type
|
||||||
attr_accessor :_fields, :_query_params
|
attr_accessor :_fields, :_query_params
|
||||||
|
|
||||||
# @api private
|
|
||||||
# Macro to add an instance method to the receiver
|
|
||||||
def add_instance_method(body, receiver = self)
|
|
||||||
cl = caller_locations[0]
|
|
||||||
silence_warnings { receiver.module_eval body, cl.absolute_path, cl.lineno }
|
|
||||||
end
|
|
||||||
|
|
||||||
# @api private
|
|
||||||
# Macro to add a class method to the receiver
|
|
||||||
def add_class_method(body, receiver)
|
|
||||||
cl = caller_locations[0]
|
|
||||||
silence_warnings { receiver.class_eval body, cl.absolute_path, cl.lineno }
|
|
||||||
end
|
|
||||||
|
|
||||||
# @api private
|
|
||||||
# Silence warnings, primarily when redefining methods
|
|
||||||
def silence_warnings
|
|
||||||
original_verbose = $VERBOSE
|
|
||||||
$VERBOSE = nil
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
$VERBOSE = original_verbose
|
|
||||||
end
|
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def _infer_type(base)
|
def _infer_type(base)
|
||||||
Inflector.pluralize(
|
Inflector.pluralize(
|
||||||
@ -101,7 +79,7 @@ module AMS
|
|||||||
# id_field :user_id
|
# id_field :user_id
|
||||||
def id_field(id_field)
|
def id_field(id_field)
|
||||||
self._id_field = id_field
|
self._id_field = id_field
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def id
|
def id
|
||||||
object.#{id_field}
|
object.#{id_field}
|
||||||
end
|
end
|
||||||
@ -121,7 +99,8 @@ module AMS
|
|||||||
fail "ForbiddenKey" if attribute_name == :id
|
fail "ForbiddenKey" if attribute_name == :id
|
||||||
_fields << key
|
_fields << key
|
||||||
_attributes[attribute_name] = { key: key }
|
_attributes[attribute_name] = { key: key }
|
||||||
add_instance_method <<-METHOD
|
|
||||||
|
add_instance_method <<-METHOD, self
|
||||||
def #{attribute_name}
|
def #{attribute_name}
|
||||||
object.#{attribute_name}
|
object.#{attribute_name}
|
||||||
end
|
end
|
||||||
@ -197,22 +176,22 @@ module AMS
|
|||||||
ids_method = options.fetch(:ids) do
|
ids_method = options.fetch(:ids) do
|
||||||
"object.#{relation_name}.pluck(:id)"
|
"object.#{relation_name}.pluck(:id)"
|
||||||
end
|
end
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def related_#{relation_name}_ids
|
def related_#{relation_name}_ids
|
||||||
#{ids_method}
|
#{ids_method}
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def related_#{relation_name}_data
|
def related_#{relation_name}_data
|
||||||
related_#{relation_name}_ids.map { |id| relationship_data(id, "#{type}") }
|
related_#{relation_name}_ids.map { |id| relationship_data(id, "#{type}") }
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def related_#{relation_name}_links
|
def related_#{relation_name}_links
|
||||||
related_link_to_many("#{type}")
|
related_link_to_many("#{type}")
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def #{relation_name}
|
def #{relation_name}
|
||||||
{}.tap do |hash|
|
{}.tap do |hash|
|
||||||
hash[:data] = related_#{relation_name}_data
|
hash[:data] = related_#{relation_name}_data
|
||||||
@ -269,22 +248,22 @@ module AMS
|
|||||||
id_method = options.fetch(:id) do
|
id_method = options.fetch(:id) do
|
||||||
"object.#{relation_name}.id"
|
"object.#{relation_name}.id"
|
||||||
end
|
end
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def related_#{relation_name}_id
|
def related_#{relation_name}_id
|
||||||
#{id_method}
|
#{id_method}
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def related_#{relation_name}_data
|
def related_#{relation_name}_data
|
||||||
relationship_data(related_#{relation_name}_id, "#{type}")
|
relationship_data(related_#{relation_name}_id, "#{type}")
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def related_#{relation_name}_links
|
def related_#{relation_name}_links
|
||||||
related_link_to_one(related_#{relation_name}_id, "#{type}")
|
related_link_to_one(related_#{relation_name}_id, "#{type}")
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
add_instance_method <<-METHOD
|
add_instance_method <<-METHOD, self
|
||||||
def #{relation_name}
|
def #{relation_name}
|
||||||
{}.tap do |hash|
|
{}.tap do |hash|
|
||||||
hash[:data] = related_#{relation_name}_data
|
hash[:data] = related_#{relation_name}_data
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user