Module: Grape::Transformations::Base::ClassMethods

Defined in:
lib/grape/transformations/base.rb

Instance Method Summary collapse

Instance Method Details

#add_endpointsObject

Invokes both transformable and non-transformable body endpoints



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/grape/transformations/base.rb', line 52

def add_endpoints
  # transformable endpoints
  Grape::Transformations.transformations_for(@grape_transformations_target_class).each do |transformation|
    entity = entity_for_transformation transformation
    namespace transformation do
      add_endpoints_with entity: entity
    end
  end
  # normal api
  add_endpoints_with entity: entity_for_transformation(:default) if Grape::Transformations.simbolized_entities_for(@grape_transformations_target_class).include? :default
  add_non_transformable_endpoints
end

#add_endpoints_with(options = {}) ⇒ Object

Invokes the block associated with transformable endpoints

Parameters:

  • , (Hash)

    options hash that contains entity transformation definition



34
35
36
37
38
# File 'lib/grape/transformations/base.rb', line 34

def add_endpoints_with(options = {})
  return unless @grape_transformations_endpoints.is_a? Proc
  entity = options[:entity] || entity_for_transformation(:default)
  @grape_transformations_endpoints.call(entity)
end

#add_non_transformable_endpointsObject

Invokes the block associated with non transformable endpoints



41
42
43
44
# File 'lib/grape/transformations/base.rb', line 41

def add_non_transformable_endpoints
  return unless @grape_transformations_non_transformable_endpoints.is_a? Proc
  @grape_transformations_non_transformable_endpoints.call
end

#define_endpoints(&block) ⇒ Object

Defines the endpoints that will use with the existing transformations

Parameters:

  • , (Proc)

    all code related with transformable endpoints definition



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

def define_endpoints(&block)
  @grape_transformations_endpoints = block.to_proc
end

#define_non_transformable_endpoints(&block) ⇒ Object

Defines the endpoints that will use with the existing transformations

Parameters:

  • , (Proc)

    all code related with non transformable endpoints definition



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

def define_non_transformable_endpoints(&block)
  @grape_transformations_non_transformable_endpoints = block.to_proc
end

#entity_for_transformation(transformation) ⇒ Object

Abstracts entity_for_transformation grape_transformations method using the grape_transformations_target_class variable



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

def entity_for_transformation(transformation)
  Grape::Transformations.entity_for_transformation(@grape_transformations_target_class, transformation)
end

#target_model(klass) ⇒ Object

Defines the target model associated to current transformations need to prefix the scope resolution operator at the beginning in order to point to the right model example:

target_model ::User

Parameters:

  • , (Class)

    Class as a reference of model, bear in mind that you



16
17
18
# File 'lib/grape/transformations/base.rb', line 16

def target_model(klass)
  @grape_transformations_target_class = klass
end