module Grape::Transformations

Constants

VERSION

Public Class Methods

all_entities_for(klass) click to toggle source

@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
all_transformation_entities_for(klass) click to toggle source

@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
entity_for_transformation(klass, transformation) click to toggle source

@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
full_path_to_entities() click to toggle source

@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
load_entities_from(relative_path_to_entities) click to toggle source

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
normalized_class_name(klass) click to toggle source

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
registered_entities() click to toggle source

returns entities registered from rails cache

# File lib/grape/transformations.rb, line 34
def self.registered_entities
  Rails.cache.fetch(:registered_entities)
end
registered_entity_for(klass) click to toggle source

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
root_entity_namespace() click to toggle source

@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
root_entity_namespace_hierarchy() click to toggle source

@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
setup() { |self| ... } click to toggle source

defines wrapped accessor to grape-transformations configurator

# File lib/grape/transformations.rb, line 29
def self.setup
  yield self
end
simbolized_entities_for(klass) click to toggle source
# 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
transformations_for(klass) click to toggle source

@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