module Grape::Transformations
Constants
- VERSION
Public Class Methods
@return [Array] all entity classes including “Default” that relate to a given class
# File lib/grape/transformations.rb, line 83 def self.all_entities_for(klass) (transformations_for(klass) + [:default]).map{|transformation| entity_for_transformation(klass, transformation)} end
@return [Array] all entity classes except for “Default” that relate to a given class
# File lib/grape/transformations.rb, line 88 def self.all_transformation_entities_for(klass) all_entities_for(klass) - [entity_for_transformation(klass, :default)] end
@return [Class] the entity class for a given transformation example: entity_for_transformation
(User, :full) #=> MyApp::Entities::Users::Full
# File lib/grape/transformations.rb, line 47 def self.entity_for_transformation(klass, transformation) entity_hierarchy = root_entity_namespace_hierarchy << normalized_class_name(klass).pluralize entity_hierarchy << transformation.to_s.camelize entity_hierarchy.join('::').constantize end
@return [String] the absolute path to entities directory
# File lib/grape/transformations.rb, line 66 def self.full_path_to_entities File.join(root_api_path, relative_path_to_entities) end
Uses Loader.load_entities
method in order to load all valid entities located in relative path_to_entities
# File lib/grape/transformations.rb, line 22 def self.load_entities_from(relative_path_to_entities) self.root_api_path ||= File.join(Rails.root, 'app', 'api') self.relative_path_to_entities = relative_path_to_entities Loader.load_entities root_api_path, relative_path_to_entities end
return the class_name given a Class or :class or “class”…or anything like it
# File lib/grape/transformations.rb, line 93 def self.normalized_class_name(klass) klass.to_s.classify end
returns entities registered from rails cache
# File lib/grape/transformations.rb, line 34 def self.registered_entities Rails.cache.fetch(:registered_entities) end
returns the appropite class for a class name or class
# File lib/grape/transformations.rb, line 39 def self.registered_entity_for(klass) registered_entities[normalized_class_name(klass)] end
@return [String] the top level module namespacing to begin looking entities within example: “MyApp::Entities”
# File lib/grape/transformations.rb, line 61 def self.root_entity_namespace root_entity_namespace_hierarchy.join('::') end
@return [Array] the top level module entity namespace wrapping example: [‘MyApp’, ‘Entities’]
# File lib/grape/transformations.rb, line 55 def self.root_entity_namespace_hierarchy File.split(relative_path_to_entities).map{|namespace| namespace.camelize} end
defines wrapped accessor to grape-transformations configurator
# File lib/grape/transformations.rb, line 29 def self.setup yield self end
# File lib/grape/transformations.rb, line 75 def self.simbolized_entities_for(klass) class_name = normalized_class_name(klass) entities_directory = File.join(full_path_to_entities, class_name.pluralize.underscore) entity_files = Dir[File.join(entities_directory, '*')] entity_files.map{|filename| File.split(filename).last.sub('.rb', '').to_sym} end
@return [Array] all transformations listed as symbols (excludes :default)
# File lib/grape/transformations.rb, line 71 def self.transformations_for(klass) simbolized_entities_for(klass) - [:default] end