Module: Grape::Transformations

Defined in:
lib/grape/transformations.rb,
lib/grape/transformations/base.rb,
lib/grape/transformations/engine.rb,
lib/grape/transformations/loader.rb,
lib/grape/transformations/version.rb

Defined Under Namespace

Modules: Base, Loader Classes: Engine

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.all_entities_for(klass) ⇒ Array

Returns all entity classes including “Default” that relate to a given class.

Returns:

  • (Array)

    all entity classes including “Default” that relate to a given class



82
83
84
# File 'lib/grape/transformations.rb', line 82

def self.all_entities_for(klass)
  (transformations_for(klass) + [:default]).map{|transformation| entity_for_transformation(klass, transformation)}
end

.all_transformation_entities_for(klass) ⇒ Array

Returns all entity classes except for “Default” that relate to a given class.

Returns:

  • (Array)

    all entity classes except for “Default” that relate to a given class



87
88
89
# File 'lib/grape/transformations.rb', line 87

def self.all_transformation_entities_for(klass)
  all_entities_for(klass) - [entity_for_transformation(klass, :default)]
end

.entity_for_transformation(klass, transformation) ⇒ Class

example: entity_for_transformation(User, :full) #=> MyApp::Entities::Users::Full

Returns:

  • (Class)

    the entity class for a given transformation



46
47
48
49
50
# File 'lib/grape/transformations.rb', line 46

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_entitiesString

Returns the absolute path to entities directory.

Returns:

  • (String)

    the absolute path to entities directory



65
66
67
# File 'lib/grape/transformations.rb', line 65

def self.full_path_to_entities
  File.join(root_api_path, relative_path_to_entities)
end

.load_entities_from(relative_path_to_entities) ⇒ Object

Uses Loader.load_entities method in order to load all valid entities located in relative path_to_entities



21
22
23
24
25
# File 'lib/grape/transformations.rb', line 21

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) ⇒ Object

return the class_name given a Class or :class or “class”…or anything like it



92
93
94
# File 'lib/grape/transformations.rb', line 92

def self.normalized_class_name(klass)
  klass.to_s.classify
end

.registered_entitiesObject

returns entities registered from rails cache



33
34
35
# File 'lib/grape/transformations.rb', line 33

def self.registered_entities
  Rails.cache.fetch(:registered_entities)
end

.registered_entity_for(klass) ⇒ Object

returns the appropite class for a class name or class



38
39
40
# File 'lib/grape/transformations.rb', line 38

def self.registered_entity_for(klass)
  registered_entities[normalized_class_name(klass)]
end

.root_entity_namespaceString

example: “MyApp::Entities”

Returns:

  • (String)

    the top level module namespacing to begin looking entities within



60
61
62
# File 'lib/grape/transformations.rb', line 60

def self.root_entity_namespace
  root_entity_namespace_hierarchy.join('::')
end

.root_entity_namespace_hierarchyArray

example: [‘MyApp’, ‘Entities’]

Returns:

  • (Array)

    the top level module entity namespace wrapping



54
55
56
# File 'lib/grape/transformations.rb', line 54

def self.root_entity_namespace_hierarchy
  File.split(relative_path_to_entities).map{|namespace| namespace.camelize}
end

.setup {|_self| ... } ⇒ Object

defines wrapped accessor to grape-transformations configurator

Yields:

  • (_self)

Yield Parameters:



28
29
30
# File 'lib/grape/transformations.rb', line 28

def self.setup
  yield self
end

.simbolized_entities_for(klass) ⇒ Object



74
75
76
77
78
79
# File 'lib/grape/transformations.rb', line 74

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) ⇒ Array

Returns all transformations listed as symbols (excludes :default).

Returns:

  • (Array)

    all transformations listed as symbols (excludes :default)



70
71
72
# File 'lib/grape/transformations.rb', line 70

def self.transformations_for(klass)
  simbolized_entities_for(klass) - [:default] 
end