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.4"
Class Method Summary collapse
-
.all_entities_for(klass) ⇒ Array
All entity classes including “Default” that relate to a given class.
-
.all_transformation_entities_for(klass) ⇒ Array
All entity classes except for “Default” that relate to a given class.
-
.entity_for_transformation(klass, transformation) ⇒ Class
example: entity_for_transformation(User, :full) #=> MyApp::Entities::Users::Full.
-
.full_path_to_entities ⇒ String
The absolute path to entities directory.
-
.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.
-
.normalized_class_name(klass) ⇒ Object
return the class_name given a Class or :class or “class”…or anything like it.
-
.registered_entities ⇒ Object
returns entities registered from rails cache.
-
.registered_entity_for(klass) ⇒ Object
returns the appropite class for a class name or class.
-
.root_entity_namespace ⇒ String
example: “MyApp::Entities”.
-
.root_entity_namespace_hierarchy ⇒ Array
example: [‘MyApp’, ‘Entities’].
-
.setup {|_self| ... } ⇒ Object
defines wrapped accessor to grape-transformations configurator.
- .simbolized_entities_for(klass) ⇒ Object
-
.transformations_for(klass) ⇒ Array
All transformations listed as symbols (excludes :default).
Class Method Details
.all_entities_for(klass) ⇒ Array
Returns all entity classes including “Default” that relate to a given class.
83 84 85 |
# 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) ⇒ Array
Returns all entity classes except for “Default” that relate to a given class.
88 89 90 |
# 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) ⇒ Class
example: entity_for_transformation(User, :full) #=> MyApp::Entities::Users::Full
47 48 49 50 51 |
# 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 ⇒ String
Returns the absolute path to entities directory.
66 67 68 |
# 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) ⇒ Object
Uses Loader.load_entities method in order to load all valid entities located in relative path_to_entities
22 23 24 25 26 |
# 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) ⇒ Object
return the class_name given a Class or :class or “class”…or anything like it
93 94 95 |
# File 'lib/grape/transformations.rb', line 93 def self.normalized_class_name(klass) klass.to_s.classify end |
.registered_entities ⇒ Object
returns entities registered from rails cache
34 35 36 |
# File 'lib/grape/transformations.rb', line 34 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
39 40 41 |
# File 'lib/grape/transformations.rb', line 39 def self.registered_entity_for(klass) registered_entities[normalized_class_name(klass)] end |
.root_entity_namespace ⇒ String
example: “MyApp::Entities”
61 62 63 |
# File 'lib/grape/transformations.rb', line 61 def self.root_entity_namespace root_entity_namespace_hierarchy.join('::') end |
.root_entity_namespace_hierarchy ⇒ Array
example: [‘MyApp’, ‘Entities’]
55 56 57 |
# 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| ... } ⇒ Object
defines wrapped accessor to grape-transformations configurator
29 30 31 |
# File 'lib/grape/transformations.rb', line 29 def self.setup yield self end |
.simbolized_entities_for(klass) ⇒ Object
75 76 77 78 79 80 |
# 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) ⇒ Array
Returns all transformations listed as symbols (excludes :default).
71 72 73 |
# File 'lib/grape/transformations.rb', line 71 def self.transformations_for(klass) simbolized_entities_for(klass) - [:default] end |