module ActiveFacet::Serializer::Base
Public Class Methods
@return [Serializer::Base]
# File lib/active_facet/serializer/base.rb, line 265 def initialize config.compile! self rescue SystemStackError => e raise ActiveFacet::Errors::ConfigurationError.new(ActiveFacet::Errors::ConfigurationError::STACK_ERROR_MSG) end
Public Instance Methods
This method returns a JSON of hash values representing the resource(s)
- @param resource [ActiveRecord || Array] CollectionProxy object ::or
-
a collection of resources
@param options [Hash] collection of values required that are not available in lexical field_set @return [JSON] representing the resource
# File lib/active_facet/serializer/base.rb, line 190 def as_json(resources, options = {}) resource_itterator(resources) do |resource| facade = ActiveFacet::Serializer::Facade.new(self, resource, options) facade.as_json end end
Memoized instance getter @return [Config]
# File lib/active_facet/serializer/base.rb, line 199 def config @config ||= self.class.config end
Returns field_set serialized for dependant resources in custom attribute serializers & extensions @param field [Field] @return [Field Set]
# File lib/active_facet/serializer/base.rb, line 150 def custom_includes(field, options) attribute = resource_attribute_name(field) custom_serializer_name = config.serializers[attribute] if custom_serializer_name custom_serializer = get_custom_serializer_class(custom_serializer_name, options) if custom_serializer.respond_to? :custom_scope custom_serializer.custom_scope else nil end else nil end end
Gets flattened fields from a Field Set Alias @param field_set_alias [Symbol] to retrieve aliased field_sets for @param include_relations [Boolean] @param include_nested_field_sets [Boolean] @return [Array] of symbols
# File lib/active_facet/serializer/base.rb, line 171 def exposed_aliases(field_set_alias = :all, include_relations = false, include_nested_field_sets = false) return include_nested_field_sets ? field_set_alias : [field_set_alias] unless normalized_field_sets = config.normalized_field_sets[field_set_alias] result = normalized_field_sets[include_relations ? :fields : :attributes] return result if include_nested_field_sets result.keys.map(&:to_sym).sort end
This method returns a ActiveRecord model updated to match a JSON of hash values @param resource [ActiveRecord] to hydrate @param attribute [Hash] subset of the values returned by {resource.as_json} @return [ActiveRecord] resource
# File lib/active_facet/serializer/base.rb, line 182 def from_hash(resource, attributes, options = {}) ActiveFacet::Serializer::Facade.new(self, resource, options).from_hash(attributes) end
Constantizes an appropriate resource serializer class for relations @param field [Symbol] to find relation reflection for @return [Reflection | nil]
# File lib/active_facet/serializer/base.rb, line 215 def get_association_reflection(field) @association_reflections ||= {} @association_reflections[field] ||= resource_class.reflect_on_association(resource_attribute_name(field).to_sym) end
Constantizes an appropriate resource serializer class @param field [Symbol] to test as relation and find serializer class for @return [Class | nil]
# File lib/active_facet/serializer/base.rb, line 223 def get_association_serializer_class(field, options) @association_serializers ||= {} unless @association_serializers.key? field @association_serializers[field] = nil #return nil if field isn't an association if reflection = get_association_reflection(field) #return nil if association doesn't have a custom class @association_serializers[field] = ActiveFacet::Helper.serializer_for(reflection.klass, options) end end @association_serializers[field] end
Constantizes an appropriate attribute serializer class @param attribute [Symbol] base_name of attribute serializer class to find @param options [Hash] @return [Class | nil]
# File lib/active_facet/serializer/base.rb, line 240 def get_custom_serializer_class(attribute, options) @custom_serializers ||= {} @custom_serializers[attribute] ||= ActiveFacet::Helper.attribute_serializer_class_for(resource_class, attribute, options) end
Determines if public attribute maps to a private relation @param field [Symbol] public attribute name @return [Boolean]
# File lib/active_facet/serializer/base.rb, line 248 def is_association?(field) !!get_association_reflection(field) end
Renames attribute between resource.attribute_name and json.attribute_name @param field [Symbol] attribute name @param direction [Symbol] to apply translation @return [Symbol]
# File lib/active_facet/serializer/base.rb, line 256 def resource_attribute_name(field, direction = :from) (config.transforms(direction)[field] || field).to_sym end
Constantizes the appropriate resource serializer class @return [Class]
# File lib/active_facet/serializer/base.rb, line 208 def resource_class @resource_class ||= config.resource_class end
TODO – comment and move private
# File lib/active_facet/serializer/base.rb, line 132 def scoped_include(field, nested_field_set, options) if is_association? field attribute = resource_attribute_name(field) if nested_field_set serializer_class = get_association_serializer_class(field, options) attribute = { attribute => serializer_class.present? ? serializer_class.scoped_includes(nested_field_set, options) : nested_field_set } end attribute else custom_includes(field, options) end end
This method returns a hash suitable to pass into ActiveRecord.includes to avoid N+1 @param field_set [Field Set] collections of fields to be serialized from this resource later given:
:basic is a defined collection :extended is a defined collection :orders is a defined association :line_items is a defined association on OrderSerializer
examples:
:basic [:basic] {basic: nil} [:basic, :extended] [:basic, :extended, :orders] [:basic, :extended, {orders: :basic}] [:basic, :extended, {orders: [:basic, :extended]}] [:basic, :extended, {orders: [:basic, :line_items]}] [:basic, :extended, {orders: [:basic, {line_items: :extended}]}]
@return [Hash]
# File lib/active_facet/serializer/base.rb, line 116 def scoped_includes(field_set = nil, options = {}) result = {} config.field_set_itterator(field_set) do |field, nested_field_set| case value = scoped_include(field, nested_field_set, options) when nil when Hash result.deep_merge! value else result[value] ||= nil end end result end
Protected Instance Methods
Removes self.class_name from the end of self.module_name @return [String]
# File lib/active_facet/serializer/base.rb, line 286 def module_base_name @module_base_name ||= module_name.deconstantize end
Removes self.class_name from the end of self.class.name @return [String]
# File lib/active_facet/serializer/base.rb, line 292 def module_name @module_name ||= self.class.name.deconstantize end
Itterates a resource collection invoking block @param resource [ActiveRecord || Array] to traverse @param block [Block] to call for each resource
# File lib/active_facet/serializer/base.rb, line 274 def resource_itterator(resource) if resource.is_a?(Array) resource.map do |resource| yield resource end.compact else yield resource end end