Module: Grape::Transformations::Loader
- Defined in:
- lib/grape/transformations/loader.rb
Class Method Summary collapse
-
.class_by_name(name) ⇒ Object
Return a class by its name, id it is not valid it returns nil.
-
.load_entities(root_api_path, relative_path_to_entities) ⇒ Object
Loads all valid entities located in relative_path_to_entities dir, it verifies if entity is valid and if it is associated with valid class.
Class Method Details
.class_by_name(name) ⇒ Object
Return a class by its name, id it is not valid it returns nil
11 12 13 |
# File 'lib/grape/transformations/loader.rb', line 11 def self.class_by_name(name) name.safe_constantize end |
.load_entities(root_api_path, relative_path_to_entities) ⇒ Object
Loads all valid entities located in relative_path_to_entities dir, it verifies if entity is valid and if it is associated with valid class. When this process is accomplished is saved a hash with classname as a key and its associated entity as a value (using Rails.cache)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/grape/transformations/loader.rb', line 21 def self.load_entities(root_api_path, relative_path_to_entities) available_entities = {} path_to_entities = File.join(root_api_path, relative_path_to_entities) api_entities_namespace = relative_path_to_entities.split('/').map { |module_name| module_name.gsub(' ','_').camelcase }.join('::') Dir.glob(File.join(path_to_entities, '**', '*.rb')).each do |filename| relative_path = filename.sub("#{path_to_entities}/", '') class_hierarchy = relative_path.split("/").map {|filename| filename.gsub(' ','_').camelcase.sub('.rb','')} class_name = class_hierarchy.join('::') entity_name = "#{api_entities_namespace}::#{class_name}" klass = class_by_name class_name entity = class_by_name entity_name # If there is a one to one match from Entities::..::Entity to ..::Klass, use it if entity && klass available_entities[class_name] = entity_name else # If not, try to find a "Default" transformation entity class # namespaced in the pluralized klass # An entity has to exist if entity # example: ['Admin', 'Billing', 'Addresses'], 'Default' = ['Admin', 'Billing', 'Addresses', 'Default'] *transformation_hierarchy, transformation_name = class_hierarchy if transformation_name == 'Default' # example: ['Admin', 'Billing'], 'Addresses' = ['Admin', 'Billing', 'Addresses'] *class_name_hierarchy, pluralized_transformable_class_name = transformation_hierarchy # example: 'Address' = "Addresses".singularize transformable_class_name = pluralized_transformable_class_name.singularize # example: Admin::Billing::Address class_name_hierarchy << transformable_class_name class_name = class_name_hierarchy.join('::') klass = class_by_name class_name # example: Admin::Billing::Address # a matching class was found if klass available_entities[class_name] = entity_name else # The entity is just an entity, no matching class... however seems like # conventions were not properly followed end else # No one to one match or Default transformation found, no entity used end else puts "[WARN] - Conventions not followed? - Entity #{entity_name} was not found at #{filename}, please check" end end end register_entities available_entities end |